mirror of
https://github.com/focus-creative-games/hybridclr_unity.git
synced 2026-11-21 08:02:40 +08:00
[update] 对 Hybrid Global Settings 工作流稍作完善
1. 将 hybrid settings 转移到 ProjectSettings 文件夹 2. 将 hybrid Settings 绘制到 编辑器 ProjectSettings 面板 3. 添加 文档 、Presets、Reset 功能 4. 在 installer 添加 settings 入口 5. 在 菜单栏添加 setting 入口,关于我们、以及文档入口
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEditorInternal;
|
||||
using UnityEngine;
|
||||
|
||||
namespace HybridCLR.Editor
|
||||
{
|
||||
public class ScriptableSingleton<T> : ScriptableObject where T : ScriptableObject
|
||||
{
|
||||
private static T s_Instance;
|
||||
public static T Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!s_Instance)
|
||||
{
|
||||
CreateAndLoad();
|
||||
}
|
||||
return s_Instance;
|
||||
}
|
||||
}
|
||||
private static void CreateAndLoad()
|
||||
{
|
||||
string filePath = GetFilePath();
|
||||
if (!string.IsNullOrEmpty(filePath))
|
||||
{
|
||||
var arr = InternalEditorUtility.LoadSerializedFileAndForget(filePath);
|
||||
s_Instance = arr.Length > 0 ? arr[0] as T : CreateInstance<T>();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"{nameof(ScriptableSingleton<T>)}: 请指定单例存档路径! ");
|
||||
}
|
||||
}
|
||||
|
||||
public 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()
|
||||
{
|
||||
Type typeFromHandle = typeof(T);
|
||||
object[] customAttributes = typeFromHandle.GetCustomAttributes(inherit: true);
|
||||
object[] array = customAttributes;
|
||||
foreach (object obj in array)
|
||||
{
|
||||
if (obj is FilePathAttribute)
|
||||
{
|
||||
FilePathAttribute filePathAttribute = obj as FilePathAttribute;
|
||||
return filePathAttribute.filepath;
|
||||
}
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
[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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user