44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using Sirenix.OdinInspector;
|
||
using UnityEngine;
|
||
|
||
namespace Continentis.MainGame.Base
|
||
{
|
||
public partial class BaseCollection<T> : ScriptableObject where T : BaseCollection<T>
|
||
{
|
||
private static T _instance;
|
||
|
||
public static T Instance
|
||
{
|
||
get
|
||
{
|
||
string type = typeof(T).Name;
|
||
if (_instance == null)
|
||
{
|
||
// 默认尝试从 Resources 文件夹加载
|
||
string path = $"BaseCollections/{type}";
|
||
_instance = Resources.Load<T>(path);
|
||
}
|
||
#if UNITY_EDITOR
|
||
if (_instance == null)
|
||
{
|
||
// 如果 Resources 里没有,尝试在整个 AssetDatabase 寻找
|
||
string[] guids = UnityEditor.AssetDatabase.FindAssets($"t:{type}");
|
||
if (guids.Length > 0)
|
||
{
|
||
string path = UnityEditor.AssetDatabase.GUIDToAssetPath(guids[0]);
|
||
_instance = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
|
||
}
|
||
}
|
||
#endif
|
||
if (_instance == null)
|
||
{
|
||
Debug.LogError($"[BaseCollection] 项目中没有找到类型为 {typeof(T).Name} 的 BaseCollection 资产。" +
|
||
$"请确保已在项目任意位置(建议:Resources/BaseCollections)创建该 ScriptableObject。");
|
||
}
|
||
|
||
return _instance;
|
||
}
|
||
}
|
||
}
|
||
}
|