using System.Collections; using System.Collections.Generic; using UnityEngine; namespace inspector.ext { public static class StringExtensions { public static string WithoutFileName(this string str) { return WithoutLastFolder(str); } public static string WithoutLastFolder(this string str) { return str.Substring(0, str.LastIndexOf("/") + 1); } public static FolderInfo ToFolderInfo(this string str) { return JsonUtility.FromJson(str); } /// /// Remember that the folder must be located in Resources. /// public static T[] LoadFolder(this string str) where T : Object { string resourcesFolder = "Resources/"; FolderInfo folder = str.ToFolderInfo(); if (folder.path.IndexOf(resourcesFolder) == -1) // Ensure the path is in Resources. { Debug.LogError("The folder at path " + folder.path + " must be located in Resources if you want to load it."); return null; } // We remove the part of the path that is before Resources folder.path = folder.path.Substring(folder.path.LastIndexOf(resourcesFolder) + resourcesFolder.Length); T[] resources = Resources.LoadAll(folder.path); if (resources == null || resources.Length == 0) // Maybe the folder was renamed or deleted? { FolderInfo folderInfo = Utils.GetFolderWithGUID(folder.guid); if (folderInfo != null) // Indeed, we found it in the file changes! { folder.path = folderInfo.path; // Update with new path folder.path = folder.path.Substring(folder.path.LastIndexOf(resourcesFolder) + resourcesFolder.Length); resources = Resources.LoadAll(folder.path); } } return resources; } } }