修改
This commit is contained in:
parent
bdeb6f5ad1
commit
e7f5d778e1
98
Editor/Misc/ScriptableSingleton.cs
Normal file
98
Editor/Misc/ScriptableSingleton.cs
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using UnityEditorInternal;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace AlicizaX.Editor.Setting
|
||||||
|
{
|
||||||
|
public class ScriptableSingleton<T> : ScriptableObject where T : ScriptableObject
|
||||||
|
{
|
||||||
|
private static T s_Instance;
|
||||||
|
|
||||||
|
public static T Instance
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (!s_Instance)
|
||||||
|
{
|
||||||
|
LoadOrCreate();
|
||||||
|
}
|
||||||
|
|
||||||
|
return s_Instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T LoadOrCreate()
|
||||||
|
{
|
||||||
|
string filePath = GetFilePath();
|
||||||
|
if (!string.IsNullOrEmpty(filePath))
|
||||||
|
{
|
||||||
|
var arr = InternalEditorUtility.LoadSerializedFileAndForget(filePath);
|
||||||
|
s_Instance = arr.Length > 0 ? arr[0] as T : s_Instance ?? CreateInstance<T>();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Debug.LogError($"save location of {nameof(ScriptableSingleton<T>)} is invalid");
|
||||||
|
}
|
||||||
|
|
||||||
|
return s_Instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Save(bool saveAsText = true)
|
||||||
|
{
|
||||||
|
if (!s_Instance)
|
||||||
|
{
|
||||||
|
Debug.LogError("Cannot save ScriptableSingleton: no instance!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string filePath = GetFilePath();
|
||||||
|
if (!string.IsNullOrEmpty(filePath))
|
||||||
|
{
|
||||||
|
string directoryName = Path.GetDirectoryName(filePath);
|
||||||
|
if (!Directory.Exists(directoryName))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(directoryName);
|
||||||
|
}
|
||||||
|
|
||||||
|
UnityEngine.Object[] obj = new T[1] { s_Instance };
|
||||||
|
InternalEditorUtility.SaveToSerializedFileAndForget(obj, filePath, saveAsText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static string GetFilePath()
|
||||||
|
{
|
||||||
|
return typeof(T).GetCustomAttributes(inherit: true)
|
||||||
|
.Where(v => v is FilePathAttribute)
|
||||||
|
.Cast<FilePathAttribute>()
|
||||||
|
.FirstOrDefault()
|
||||||
|
?.filepath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Class)]
|
||||||
|
public class FilePathAttribute : Attribute
|
||||||
|
{
|
||||||
|
internal string filepath;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 单例存放路径
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">相对 Project 路径</param>
|
||||||
|
public FilePathAttribute(string path)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(path))
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid relative path (it is empty)");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path[0] == '/')
|
||||||
|
{
|
||||||
|
path = path.Substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
filepath = path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
3
Editor/Misc/ScriptableSingleton.cs.meta
Normal file
3
Editor/Misc/ScriptableSingleton.cs.meta
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 9da8cbe84b4045d093d326334bbf4c7f
|
||||||
|
timeCreated: 1739529382
|
@ -28,7 +28,7 @@ namespace AlicizaX.Runtime
|
|||||||
/// <param name="value">要检查的值。</param>
|
/// <param name="value">要检查的值。</param>
|
||||||
/// <param name="name">值的名称。</param>
|
/// <param name="name">值的名称。</param>
|
||||||
/// <exception cref="ArgumentNullException">当值为null时引发。</exception>
|
/// <exception cref="ArgumentNullException">当值为null时引发。</exception>
|
||||||
public static void NotNull<T>(T value, string name) where T : class
|
public static void NotNull<T>(T value, string name) where T : class
|
||||||
{
|
{
|
||||||
if (value == null)
|
if (value == null)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user