Files
Continentis/Assets/Scripts/MainGame/Base/BaseCollection.cs
SoulliesOfficial d09b58fd80 架构大更
2026-03-20 11:56:50 -04:00

44 lines
1.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}
}
}