Files
2026-05-26 16:15:54 +08:00

60 lines
2.0 KiB
C#

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<FolderInfo>(str);
}
/// <summary>
/// Remember that the folder must be located in Resources.
/// </summary>
public static T[] LoadFolder<T>(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<T>(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<T>(folder.path);
}
}
return resources;
}
}
}