mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交GAS 打算做一个帧同步的GAS
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
#if UNITY_EDITOR
|
||||
namespace GAS.Editor
|
||||
{
|
||||
using System;
|
||||
using UnityEditor;
|
||||
using UnityEditorInternal;
|
||||
|
||||
[InitializeOnLoad]
|
||||
public static class GASSettingStatusWatcher
|
||||
{
|
||||
public static Action OnEditorFocused;
|
||||
static bool isFocused;
|
||||
static GASSettingStatusWatcher() => EditorApplication.update += Update;
|
||||
static void Update()
|
||||
{
|
||||
if (isFocused != InternalEditorUtility.isApplicationActive)
|
||||
{
|
||||
isFocused = InternalEditorUtility.isApplicationActive;
|
||||
if (isFocused)
|
||||
{
|
||||
GASSettingAsset.LoadOrCreate();
|
||||
GameplayTagsAsset.LoadOrCreate();
|
||||
AttributeAsset.LoadOrCreate();
|
||||
AttributeSetAsset.LoadOrCreate();
|
||||
OnEditorFocused?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: afa4715c0a2b4ffb8c717b919ede2c4e
|
||||
timeCreated: 1713192408
|
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEditorInternal;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace GAS.Editor
|
||||
{
|
||||
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 ? 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 (directoryName is null)
|
||||
{
|
||||
Debug.LogError($"save location of {nameof(ScriptableSingleton<T>)} is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Directory.Exists(directoryName))
|
||||
{
|
||||
Directory.CreateDirectory(directoryName);
|
||||
}
|
||||
|
||||
Object[] obj = { s_Instance };
|
||||
InternalEditorUtility.SaveToSerializedFileAndForget(obj, filePath, saveAsText);
|
||||
//Debug.Log($"Saved ScriptableSingleton to {filePath}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateAsset(T asset)
|
||||
{
|
||||
if (asset == null) return;
|
||||
s_Instance = asset;
|
||||
}
|
||||
|
||||
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[1..];
|
||||
}
|
||||
|
||||
filepath = path;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 34f6c2e7a19949269d27f8aae9ff5a5a
|
||||
timeCreated: 1713189758
|
Reference in New Issue
Block a user