com.alicizax.unity/Editor/Misc/ScriptableSingletonUtil.cs

40 lines
1.2 KiB
C#
Raw Normal View History

2025-02-07 16:04:12 +08:00
using System;
2025-03-24 13:16:51 +08:00
using AlicizaX;
2025-02-07 16:04:12 +08:00
using UnityEngine;
namespace AlicizaX.Editor
{
public static class ScriptableSingletonUtil
{
public static T Get<T>() where T : ScriptableObject
{
string assetType = typeof(T).Name;
T globalSetting = default;
string[] globalAssetPaths = UnityEditor.AssetDatabase.FindAssets($"t:{assetType}");
if (globalAssetPaths.Length > 1)
{
foreach (var assetPath in globalAssetPaths)
{
Debug.LogError($"Could not had Multiple {assetType}. Repeated Path: {UnityEditor.AssetDatabase.GUIDToAssetPath(assetPath)}");
}
throw new Exception($"Could not had Multiple {assetType}");
}
if (globalAssetPaths.Length == 1)
{
string path = UnityEditor.AssetDatabase.GUIDToAssetPath(globalAssetPaths[0]);
globalSetting = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
}
if (globalSetting == null)
{
Debug.LogError($"Could not found {assetType} asset");
return null;
}
return globalSetting;
}
}
}