diff --git a/Assets/Scenes/Persistence.unity b/Assets/Scenes/Persistence.unity index f440320a0..86eaf10e4 100644 --- a/Assets/Scenes/Persistence.unity +++ b/Assets/Scenes/Persistence.unity @@ -9456,52 +9456,6 @@ CanvasRenderer: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 651726652} m_CullTransparentMesh: 1 ---- !u!1 &667130561 -GameObject: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - serializedVersion: 6 - m_Component: - - component: {fileID: 667130562} - - component: {fileID: 667130563} - m_Layer: 0 - m_Name: Variable Storage - m_TagString: Untagged - m_Icon: {fileID: 0} - m_NavMeshLayer: 0 - m_StaticEditorFlags: 0 - m_IsActive: 0 ---- !u!4 &667130562 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 667130561} - serializedVersion: 2 - m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 - m_Children: [] - m_Father: {fileID: 1936621685} - m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &667130563 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 667130561} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 610c541718d694070a21327d1ae62e75, type: 3} - m_Name: - m_EditorClassIdentifier: - showDebug: 1 - debugTextView: {fileID: 0} --- !u!1 &673669944 stripped GameObject: m_CorrespondingSourceObject: {fileID: 8653192077935634818, guid: 8447f1c934ed28243b9924fb4d106a57, type: 3} @@ -24258,7 +24212,6 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: - {fileID: 331068879} - - {fileID: 667130562} - {fileID: 314243162} m_Father: {fileID: 72512886} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/Assets/Scripts/FixSystemNew/FixSystemCenter.cs b/Assets/Scripts/FixSystemNew/FixSystemCenter.cs index 6b757fd1c..3c163e4be 100644 --- a/Assets/Scripts/FixSystemNew/FixSystemCenter.cs +++ b/Assets/Scripts/FixSystemNew/FixSystemCenter.cs @@ -47,7 +47,7 @@ namespace AibisDream.FixSystem } [Serializable] - [LoadIndex(1)] + [LoadIndex(2)] public class FixSystemData : IData { private FixState _curState; diff --git a/Assets/Scripts/Framework/Core/Core.cs b/Assets/Scripts/Framework/Core/Core.cs index a75579a5c..0cc2ef9e4 100644 --- a/Assets/Scripts/Framework/Core/Core.cs +++ b/Assets/Scripts/Framework/Core/Core.cs @@ -78,10 +78,13 @@ namespace AibisDream.Framework { private Dictionary _instances = new(); + public int Count => _instances.Count; + public void Register(T instance) where T : IData { var key = typeof(T).AssemblyQualifiedName; if (key != null) _instances[key] = instance; + Debug.Log(_instances.Count); } public T Get() where T : class, IData @@ -119,7 +122,10 @@ namespace AibisDream.Framework return null; } - public void Clear() => _instances.Clear(); + public void Clear() + { + _instances.Clear(); + } public JObject SaveAsJson() { @@ -128,35 +134,36 @@ namespace AibisDream.Framework public IEnumerator LoadByJson(JObject dataJson) { - // 数据回填 - foreach (var fixDataItemPair in _instances) + Debug.Log(_instances.Count); + // 从加载序号1到10加载数据 + for (var i = 1; i <= 10; i++) { - if (!dataJson.TryGetValue(fixDataItemPair.Key, out var fixData)) + // 先筛选对应序号 + var curIdx = i; + var idxInstances = _instances.Where(item => LoadIndex.GetLoadIdx(item.Value) == curIdx).ToArray(); + // 回填数据并加载 + foreach (var instancePair in idxInstances) { - Debug.Log(fixDataItemPair.Key + "未注册"); - continue; + if (!dataJson.TryGetValue(instancePair.Key, out var data)) + { + Debug.Log(instancePair.Key + "未注册"); + continue; + } + + var fixDataType = Type.GetType(instancePair.Key); + if (fixDataType == null) + { + Debug.Log(instancePair.Key + "类型不存在"); + continue; + } + + // 把Json中读到的参数填回Data + var tempObj = data.ToObject(fixDataType); + CommonUtil.CopyFields(tempObj, instancePair.Value); + + // 回填完成后进行加载 + yield return instancePair.Value.Load(); } - - var fixDataType = Type.GetType(fixDataItemPair.Key); - if (fixDataType == null) - { - Debug.Log(fixDataItemPair.Key + "类型不存在"); - continue; - } - - // 把Json中读到的参数填回Data - var tempObj = fixData.ToObject(fixDataType); - CommonUtil.CopyFields(tempObj, fixDataItemPair.Value); - } - - // 执行加载过程 - // 1. 先按照LoadIdx排序 - var orderedInsArray = _instances.Values.OrderBy(LoadIndex.GetLoadIdx); - - // 2. 顺序加载,使用协程 - foreach (var data in orderedInsArray) - { - yield return data.Load(); } } @@ -195,7 +202,8 @@ namespace AibisDream.Framework fieldValue = (T)(object)(value?.ToString() ?? string.Empty); return true; } - else if (typeof(T) == typeof(float)) + + if (typeof(T) == typeof(float)) { // 处理数字类型转换为 float if (value is IConvertible) @@ -211,13 +219,12 @@ namespace AibisDream.Framework return false; } } - else - { - Debug.LogError("字段值不是可转换为 float 的类型"); - return false; - } + + Debug.LogError("字段值不是可转换为 float 的类型"); + return false; } - else if (typeof(T) == typeof(bool)) + + if (typeof(T) == typeof(bool)) { // 处理布尔类型转换为 bool if (value is IConvertible) @@ -225,27 +232,25 @@ namespace AibisDream.Framework fieldValue = (T)(object)Convert.ToBoolean(value); return true; } - else - { - Debug.LogError("字段值不是可转换为 bool 的类型"); - return false; - } + + Debug.LogError("字段值不是可转换为 bool 的类型"); + return false; } - else if (value is T typedValue) + + if (value is T typedValue) { // 处理其他类型 fieldValue = typedValue; return true; } - else - { - Debug.LogError($"未找到类型为 {typeof(T).Name} 的转换方法"); - return false; - } + + Debug.LogError($"未找到类型为 {typeof(T).Name} 的转换方法"); + return false; } } #endregion + #region 事件相关 public interface IEasyEvent diff --git a/Assets/Scripts/Game Loop/SceneLoader.cs b/Assets/Scripts/Game Loop/SceneLoader.cs index e6ae4a7cd..3e7f376d0 100644 --- a/Assets/Scripts/Game Loop/SceneLoader.cs +++ b/Assets/Scripts/Game Loop/SceneLoader.cs @@ -87,7 +87,7 @@ namespace AibisDream } [Serializable] - [LoadIndex(0)] + [LoadIndex(1)] public class SceneData : IData { public string sceneName; diff --git a/Assets/Scripts/Game Loop/StorageSystem.cs b/Assets/Scripts/Game Loop/StorageSystem.cs index eaac52c7a..d89e11aa3 100644 --- a/Assets/Scripts/Game Loop/StorageSystem.cs +++ b/Assets/Scripts/Game Loop/StorageSystem.cs @@ -15,7 +15,7 @@ namespace AibisDream private const string VariablePrefix = "$"; private const string GlobalPrefix = "$global."; private const string DataPrefix = "$data."; - + private static readonly string SaveFilePath = Application.streamingAssetsPath + "/SaveFiles"; #region 存储部分 @@ -27,7 +27,7 @@ namespace AibisDream #region 数据类注册和注销 - public void RegisterData(IData data) + public void RegisterData(T data) where T : IData { _dataContainer.Register(data); } @@ -37,6 +37,14 @@ namespace AibisDream _dataContainer.Unregister(); } + private void Update() + { + if (Input.GetKeyDown(KeyCode.K)) + { + Debug.Log(_dataContainer.Count); + } + } + #endregion #region 变量设置 @@ -111,8 +119,10 @@ namespace AibisDream { return false; } - return variableType == VariableType.DataField ? - _dataContainer.TryGetFieldValue(variableName, out _) : _variableDict.ContainsKey(variableName); + + return variableType == VariableType.DataField + ? _dataContainer.TryGetFieldValue(variableName, out _) + : _variableDict.ContainsKey(variableName); } public override bool TryGetValue(string variableName, out T result) @@ -124,11 +134,13 @@ namespace AibisDream result = default; return false; } + // 在系统数据中查找 if (variableType == VariableType.DataField) { return _dataContainer.TryGetFieldValue(variableName, out result); } + // 直接查找数据 if (_variableDict.TryGetValue(variableName, out var value)) { @@ -152,17 +164,19 @@ namespace AibisDream { if (clear) { - _dataContainer.Clear(); + 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); @@ -181,23 +195,23 @@ namespace AibisDream switch (variablePair.Value) { case float: - { - var value = System.Convert.ToSingle(variablePair.Value); - floatDict.Add(variablePair.Key, value); - break; - } + { + 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; - } + { + 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; - } + { + var value = System.Convert.ToBoolean(variablePair.Value); + boolDict.Add(variablePair.Key, value); + break; + } default: Debug.Log($"{variablePair.Key} is not a valid type"); break; @@ -218,16 +232,16 @@ namespace AibisDream // 保存系统数据 saveFile["systemData"] = _dataContainer.SaveAsJson(); - + // 保存对话变量 var (floatDict, stringDict, boolDict) = GetAllVariables(); saveFile["floatDict"] = JObject.FromObject(floatDict); saveFile["stringDict"] = JObject.FromObject(stringDict); saveFile["boolDict"] = JObject.FromObject(boolDict); - + // 存为Json JsonUtil.SaveJObject(saveFile, GetSavePath()); -#if !UNITY_EDITOR +#if !UNITY_EDITOR DeleteOldSave(); #endif } @@ -241,34 +255,34 @@ namespace AibisDream var stringDict = saveData["stringDict"]?.ToObject>(); var boolDict = saveData["boolDict"]?.ToObject>(); SetAllVariables(floatDict, stringDict, boolDict); - + // 加载系统数据 if (saveData["systemData"] is not JObject systemDataJson) { Debug.Log("维修数据读取问题"); return; } + StartCoroutine(_dataContainer.LoadByJson(systemDataJson)); } - + private string GetSavePath() { - string gameStageStr; - try + string gameStage; + if (TryGetValue("$gameStage", out float gameStageFloat)) { - TryGetValue("$gameStage", out float gameStage); - gameStageStr = gameStage.ToString(CultureInfo.InvariantCulture); + gameStage = gameStageFloat.ToString(CultureInfo.InvariantCulture); } - catch (Exception) + else { - TryGetValue("$gameStage", out string gameStage); - gameStageStr = gameStage; + TryGetValue("$gameStage", out string gameStageStr); + gameStage = gameStageStr; } - return $"{SaveFilePath}/{DateTime.Now:yyyyMMdd_HHmmss}_SOName_{gameStageStr}"; + return + $"{SaveFilePath}/{DateTime.Now:yyyyMMdd_HHmmss}_{GameLoopManager.Instance.GetSceneSoName()}_{gameStage}"; } - #endregion private static VariableType ValidateVariableName(string variableName) @@ -301,7 +315,7 @@ namespace AibisDream { Destroy(gameObject); } - + Instance = this; }