102 lines
3.8 KiB
C#
102 lines
3.8 KiB
C#
using System.Collections.Generic;
|
|
using Sirenix.OdinInspector;
|
|
using SLSUtilities.General;
|
|
using UnityEngine;
|
|
|
|
namespace SLSUtilities.Narrative
|
|
{
|
|
public partial class StorySystem : Singleton<StorySystem>
|
|
{
|
|
public StoryProjectDatabase database;
|
|
|
|
public static VariableCollection Variables;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
LoadVariables();
|
|
}
|
|
}
|
|
|
|
public partial class StorySystem
|
|
{
|
|
public static StoryProjectDatabase Database => instance.database;
|
|
}
|
|
|
|
public partial class StorySystem
|
|
{
|
|
private static string SavePath => Application.persistentDataPath + "/Story/";
|
|
|
|
public class VariableCollection
|
|
{
|
|
public Dictionary<string, bool> boolVariables = new Dictionary<string, bool>();
|
|
|
|
public Dictionary<string, int> intVariables = new Dictionary<string, int>();
|
|
|
|
public Dictionary<string, float> floatVariables = new Dictionary<string, float>();
|
|
|
|
public Dictionary<string, string> stringVariables = new Dictionary<string, string>();
|
|
|
|
public void LoadFromData()
|
|
{
|
|
List<VariableData> variableDataList = Database.variables;
|
|
foreach (VariableData variableData in variableDataList)
|
|
{
|
|
foreach (KeyValuePair<string, bool> boolVar in variableData.boolVariables)
|
|
{
|
|
if(!boolVariables.TryAdd(boolVar.Key, boolVar.Value))
|
|
{
|
|
Debug.LogWarning($"[StorySystem] 变量加载警告:布尔变量 '{boolVar.Key}' 已存在,跳过重复项。");
|
|
}
|
|
}
|
|
|
|
foreach (KeyValuePair<string, int> intVar in variableData.intVariables)
|
|
{
|
|
if(!intVariables.TryAdd(intVar.Key, intVar.Value))
|
|
{
|
|
Debug.LogWarning($"[StorySystem] 变量加载警告:整数变量 '{intVar.Key}' 已存在,跳过重复项。");
|
|
}
|
|
}
|
|
|
|
foreach (KeyValuePair<string, float> floatVar in variableData.floatVariables)
|
|
{
|
|
if(!floatVariables.TryAdd(floatVar.Key, floatVar.Value))
|
|
{
|
|
Debug.LogWarning($"[StorySystem] 变量加载警告:浮点变量 '{floatVar.Key}' 已存在,跳过重复项。");
|
|
}
|
|
}
|
|
|
|
foreach (KeyValuePair<string, string> stringVar in variableData.stringVariables)
|
|
{
|
|
if (!stringVariables.TryAdd(stringVar.Key, stringVar.Value))
|
|
{
|
|
Debug.LogWarning($"[StorySystem] 变量加载警告:字符串变量 '{stringVar.Key}' 已存在,跳过重复项。");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[Button("保存变量数据 (Save Variables)", ButtonSizes.Small, Icon = SdfIconType.Save)]
|
|
public void SaveVariables()
|
|
{
|
|
string variablesSavePath = SavePath + "variables.json";
|
|
ES3.Save("Variables", Variables, variablesSavePath);
|
|
}
|
|
|
|
public void LoadVariables()
|
|
{
|
|
string variablesSavePath = SavePath + "variables.json";
|
|
if (!ES3.FileExists(variablesSavePath))
|
|
{
|
|
Variables = new VariableCollection();
|
|
Variables.LoadFromData();
|
|
ES3.Save("Variables", Variables, variablesSavePath);
|
|
}
|
|
else
|
|
{
|
|
Variables = ES3.Load<VariableCollection>("Variables", variablesSavePath);
|
|
}
|
|
}
|
|
}
|
|
} |