40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using System;
|
|
using AlicizaX.Runtime;
|
|
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;
|
|
}
|
|
}
|
|
} |