新存储
This commit is contained in:
@@ -513,19 +513,15 @@ MonoBehaviour:
|
||||
m_PostInfinity: 2
|
||||
m_RotationOrder: 4
|
||||
_filter:
|
||||
- 0.0048309183
|
||||
- 0.016401261
|
||||
- 0.04532712
|
||||
- 0.08293076
|
||||
- 0.12053438
|
||||
- 0.14946026
|
||||
- 0.1610306
|
||||
- 0.14946026
|
||||
- 0.12053438
|
||||
- 0.08293076
|
||||
- 0.04532712
|
||||
- 0.016401261
|
||||
- 0.0048309183
|
||||
- 0.007228916
|
||||
- 0.043749984
|
||||
- 0.12409639
|
||||
- 0.20444278
|
||||
- 0.24096388
|
||||
- 0.20444278
|
||||
- 0.12409639
|
||||
- 0.043749984
|
||||
- 0.007228916
|
||||
--- !u!114 &-6086986437317696584
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 3
|
||||
|
||||
@@ -155,8 +155,6 @@ namespace AibisDream.FixSystem
|
||||
// 功能信息
|
||||
[Header("功能参数")] public bool cantStopRunning;
|
||||
|
||||
public string showPicPath;
|
||||
|
||||
// 只读
|
||||
[JsonIgnore] public string PlugInNodeName => string.Format(PlugInNodeTemplate, moduleName);
|
||||
[JsonIgnore] public string DeepInNodeName => string.Format(DeepInTemplate, moduleName);
|
||||
@@ -167,10 +165,6 @@ namespace AibisDream.FixSystem
|
||||
get => !string.IsNullOrEmpty(modulePicPath) ? ResourceKit.LoadSpriteFromPsd(modulePicPath) : null;
|
||||
set => modulePicPath = ResourceKit.SaveSprite(value);
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public Sprite ScreenPic =>
|
||||
!string.IsNullOrEmpty(showPicPath) ? ResourceKit.LoadSpriteFromPsd(showPicPath) : null;
|
||||
|
||||
[JsonIgnore]
|
||||
public Vector3 ModulePos
|
||||
|
||||
@@ -71,4 +71,9 @@ namespace AibisDream
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public enum StorageEvent
|
||||
{
|
||||
VariableSet
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using AibisDream.Framework;
|
||||
using UnityEngine;
|
||||
using Yarn.Unity;
|
||||
|
||||
namespace AibisDream
|
||||
{
|
||||
public class StorageSystem : VariableStorageBehaviour
|
||||
{
|
||||
private const string VariablePrefix = "$";
|
||||
private const string GlobalPrefix = "$global.";
|
||||
private const string DataPrefix = "$data.";
|
||||
|
||||
#region 存储部分
|
||||
|
||||
private readonly Dictionary<string, object> _variableDict = new();
|
||||
private readonly DataContainer _dataContainer = new();
|
||||
|
||||
#endregion
|
||||
|
||||
#region 数据类注册和注销
|
||||
|
||||
public void RegisterData(IData data)
|
||||
{
|
||||
_dataContainer.Register(data);
|
||||
}
|
||||
|
||||
public void UnregisterData<T>() where T : class, IData
|
||||
{
|
||||
_dataContainer.Unregister<T>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 变量设置
|
||||
|
||||
public override void SetValue(string variableName, bool boolValue)
|
||||
{
|
||||
SetValue(variableName, boolValue);
|
||||
}
|
||||
|
||||
public override void SetValue(string variableName, string stringValue)
|
||||
{
|
||||
SetValue(variableName, stringValue);
|
||||
}
|
||||
|
||||
public override void SetValue(string variableName, float floatValue)
|
||||
{
|
||||
SetValue(variableName, floatValue);
|
||||
}
|
||||
|
||||
private void SetValue<T>(string variableName, T value)
|
||||
{
|
||||
var variableType = ValidateVariableName(variableName);
|
||||
|
||||
if (variableType == VariableType.Illegal)
|
||||
{
|
||||
Debug.LogError($"{variableName}: 缺少$前缀");
|
||||
return;
|
||||
}
|
||||
|
||||
if (variableType == VariableType.DataField)
|
||||
{
|
||||
Debug.LogError($"{variableName}: 暂时不支持直接设置系统数据");
|
||||
return;
|
||||
}
|
||||
|
||||
// Global和Local直接设置
|
||||
_variableDict[variableName] = value;
|
||||
|
||||
// 数据设置事件广播
|
||||
var variableItem = new VariableItem
|
||||
{
|
||||
variableName = variableName,
|
||||
value = value
|
||||
};
|
||||
EnumEventSystem.Global.Send(StorageEvent.VariableSet, variableItem);
|
||||
}
|
||||
|
||||
public override void Clear()
|
||||
{
|
||||
_variableDict.Clear();
|
||||
}
|
||||
|
||||
public void ClearLocal()
|
||||
{
|
||||
// 收集所有Local类型的变量
|
||||
var locals = _variableDict.Keys.Where(item => ValidateVariableName(item) == VariableType.Local).ToArray();
|
||||
// 清除
|
||||
foreach (var localKey in locals)
|
||||
{
|
||||
_variableDict.Remove(localKey);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 变量获取
|
||||
|
||||
public override bool Contains(string variableName)
|
||||
{
|
||||
return _variableDict.ContainsKey(variableName);
|
||||
}
|
||||
|
||||
public override bool TryGetValue<T>(string variableName, out T result)
|
||||
{
|
||||
var variableType = ValidateVariableName(variableName);
|
||||
// 非法的直接pass
|
||||
if (variableType == VariableType.Illegal)
|
||||
{
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
// 在系统数据中查找
|
||||
if (variableType == VariableType.DataField)
|
||||
{
|
||||
Debug.LogWarning("查找Data数据功能待实现");
|
||||
result = default;
|
||||
return true;
|
||||
}
|
||||
// 直接查找数据
|
||||
if (_variableDict.TryGetValue(variableName, out var value))
|
||||
{
|
||||
if (value is T target)
|
||||
{
|
||||
result = target;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
result = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 序列化
|
||||
|
||||
public override void SetAllVariables(Dictionary<string, float> floats, Dictionary<string, string> strings,
|
||||
Dictionary<string, bool> bools, bool clear = true)
|
||||
{
|
||||
if (clear)
|
||||
{
|
||||
_dataContainer.Clear();
|
||||
}
|
||||
|
||||
foreach (var value in floats)
|
||||
{
|
||||
SetValue(value.Key, value.Value);
|
||||
}
|
||||
foreach (var value in strings)
|
||||
{
|
||||
SetValue(value.Key, value.Value);
|
||||
}
|
||||
foreach (var value in bools)
|
||||
{
|
||||
SetValue(value.Key, value.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public override (Dictionary<string, float> FloatVariables, Dictionary<string, string> StringVariables,
|
||||
Dictionary<string, bool> BoolVariables) GetAllVariables()
|
||||
{
|
||||
var floatDict = new Dictionary<string, float>();
|
||||
var stringDict = new Dictionary<string, string>();
|
||||
var boolDict = new Dictionary<string, bool>();
|
||||
|
||||
foreach (var variablePair in _variableDict)
|
||||
{
|
||||
switch (variablePair.Value)
|
||||
{
|
||||
case float:
|
||||
{
|
||||
var value = System.Convert.ToSingle(variablePair.Value);
|
||||
floatDict.Add(variablePair.Key, value);
|
||||
break;
|
||||
}
|
||||
case string:
|
||||
{
|
||||
var value = System.Convert.ToString(variablePair.Value);
|
||||
stringDict.Add(variablePair.Key, value);
|
||||
break;
|
||||
}
|
||||
case bool:
|
||||
{
|
||||
var value = System.Convert.ToBoolean(variablePair.Value);
|
||||
boolDict.Add(variablePair.Key, value);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
Debug.Log($"{variablePair.Key} is not a valid type");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return (floatDict, stringDict, boolDict);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static VariableType ValidateVariableName(string variableName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(variableName) || !variableName.StartsWith(VariablePrefix))
|
||||
{
|
||||
return VariableType.Illegal;
|
||||
}
|
||||
|
||||
if (variableName.StartsWith(GlobalPrefix))
|
||||
{
|
||||
return VariableType.Global;
|
||||
}
|
||||
|
||||
if (variableName.StartsWith(DataPrefix))
|
||||
{
|
||||
return VariableType.DataField;
|
||||
}
|
||||
|
||||
return VariableType.Local;
|
||||
}
|
||||
}
|
||||
|
||||
public enum VariableType
|
||||
{
|
||||
Local,
|
||||
Global,
|
||||
DataField,
|
||||
Illegal
|
||||
}
|
||||
|
||||
public struct VariableItem
|
||||
{
|
||||
public string variableName;
|
||||
public object value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bd2f44edfbd942b49bb7a37f80d7a5eb
|
||||
timeCreated: 1742802488
|
||||
Reference in New Issue
Block a user