Created
November 13, 2024 10:48
-
-
Save der-hugo/494c968e5efec26f06729583cc1da1ae to your computer and use it in GitHub Desktop.
For listing all available paths to be used in Resources.Load - stores and loads a simple text file containing all resources paths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
using System; | |
using System.IO; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
namespace derHugo | |
{ | |
public static class ResourcesInfo | |
{ | |
private const string Name = nameof(ResourcesInfo); | |
private const string ResourcesFolderName = "Resources"; | |
private const string AssetPath = ResourcesFolderName + "/" + Name + ".txt"; | |
private static string[] paths; | |
public static IReadOnlyList<string> AvailableResources | |
{ | |
get | |
{ | |
if (paths != null) | |
{ | |
return paths; | |
} | |
var textAsset = Resources.Load<TextAsset>(Name); | |
if (textAsset == null) | |
{ | |
#if UNITY_EDITOR | |
RefreshResources(); | |
textAsset = Resources.Load<TextAsset>(Name); | |
#else | |
throw new FileNotFoundException("Failed to load ResourcesInfo.txt from Resources!"); | |
#endif | |
} | |
var content = textAsset.text; | |
Resources.UnloadAsset(textAsset); | |
paths = content.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); | |
return paths; | |
} | |
} | |
#if UNITY_EDITOR | |
[InitializeOnLoadMethod] | |
private static void EditorInit() | |
{ | |
RefreshResources(); | |
} | |
private static void RefreshResources() | |
{ | |
EditorApplication.delayCall -= RefreshResources; | |
if (!Directory.Exists(ResourcesFolderName)) | |
{ | |
Directory.CreateDirectory(ResourcesFolderName); | |
} | |
using (var writer = new StreamWriter(Path.Combine(Application.dataPath, AssetPath))) | |
{ | |
// find all folders called Resources | |
var resourcesFolders = Directory.EnumerateDirectories(Application.dataPath, ResourcesFolderName, SearchOption.AllDirectories); | |
foreach (var path in resourcesFolders) | |
{ | |
var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories); | |
foreach (var file in files) | |
{ | |
// exclude meta files | |
if (file.EndsWith(".meta")) | |
{ | |
continue; | |
} | |
// strip leading path until Resources folder | |
var index = file.IndexOf(ResourcesFolderName) + ResourcesFolderName.Length + 1; | |
var resourcesPath = file.Substring(index); | |
var folder = Path.GetDirectoryName(resourcesPath); | |
if (folder.Length > 0) | |
{ | |
folder += "/"; | |
} | |
resourcesPath = folder + Path.GetFileNameWithoutExtension(resourcesPath); | |
writer.WriteLine(resourcesPath); | |
} | |
} | |
} | |
AssetDatabase.Refresh(); | |
} | |
/// <summary> | |
/// Tracks any asset modifications and refreshes the resources info | |
/// </summary> | |
private class AssetModificationProcessor : UnityEditor.AssetModificationProcessor | |
{ | |
static void OnWillCreateAsset(string assetName) | |
{ | |
EditorApplication.delayCall += RefreshResources; | |
} | |
static AssetDeleteResult OnWillDeleteAsset(string assetName, RemoveAssetOptions options) | |
{ | |
EditorApplication.delayCall += RefreshResources; | |
return AssetDeleteResult.DidNotDelete; | |
} | |
static void FileModeChanged(string[] paths, FileMode mode) | |
{ | |
EditorApplication.delayCall += RefreshResources; | |
} | |
static string[] OnWillSaveAssets(string[] paths) | |
{ | |
EditorApplication.delayCall += RefreshResources; | |
return paths; | |
} | |
private static AssetMoveResult OnWillMoveAsset(string sourcePath, string destinationPath) | |
{ | |
EditorApplication.delayCall += RefreshResources; | |
return AssetMoveResult.DidNotMove; | |
} | |
} | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment