Files
plugin-library/Assets/02.InformationSave/RunTime/Abstract/AbstractInformation.cs

201 lines
6.4 KiB
C#
Raw Normal View History

2025-03-26 09:34:52 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Stary.Evo.InformationSave.Data;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine;
using System.Linq;
namespace Stary.Evo.InformationSave
{
public abstract class AbstractInformation<T> : CustomEditorBase where T : InformationBase, new()
{
[HideInInspector] public List<T> _list = new List<T>();
/// <summary>
/// 配置文件存储路径
/// </summary>
string path = "Assets/02.InformationSave/RunTime/Data/ScriptObjectSaveData.asset";
public virtual void Add()
{
_list.Add(new T());
Save(_list.Count - 1);
}
public abstract void Save(int index);
public abstract void Switch(int index);
public virtual T GetTransform(string desc)
{
int index = _list.FindIndex(n => n.desc == desc);
if (index != -1)
{
return _list[index];
}
else
{
Debug.LogError($"{typeof(T)}:不存在该信息:" + desc);
return default(T);
}
}
public virtual void Delete(int index)
{
_list.RemoveAt(index);
}
public virtual void Set(string desc)
{
int index = _list.FindIndex(n => n.desc == desc);
if (index != -1) Switch(index);
}
#region Editor
#if UNITY_EDITOR
//更新
public void UpdateInformation()
{
String sceneNmae = gameObject.scene.name;
ScriptObjectSave scriptObjectSaveData = AssetDatabase.LoadAssetAtPath<ScriptObjectSave>(path);
if(scriptObjectSaveData == null)
{
Debug.LogError("ScriptObjectSaveData配置文件丢失");
return;
}
if (scriptObjectSaveData.dic.ContainsKey(sceneNmae + gameObject.name))
{
_list.Clear();
_list = scriptObjectSaveData.dic[sceneNmae + gameObject.name].OfType<T>().ToList();
}
else
{
Debug.LogError("ScriptObjectSaveData中未存储场景"+ sceneNmae + " 中物体:" + gameObject.name + "的数据!!!");
}
}
//动态保存
public bool PlayingSave()
{
//文件保存
if (Application.isEditor)
{
String sceneNmae = gameObject.scene.name;
// 加载 ScriptObjectSaveData
ScriptObjectSave scriptObjectSaveData = AssetDatabase.LoadAssetAtPath<ScriptObjectSave>(path);
if(scriptObjectSaveData != null)
{
if(scriptObjectSaveData.dic == null)
{
Debug.LogError("ScriptObjectSaveData的dic为空");
return false;
}
// 检查是否已经存在相同的键
if (scriptObjectSaveData.dic.ContainsKey(sceneNmae + gameObject.name))
{
// 如果存在,选择是否替换原有的 List
if(UnityEditor.EditorUtility.DisplayDialog("提示", "数据集下已有相同名称的物体数据\n是否覆盖更新\n"+ "场景名:"+ sceneNmae + "\n物体名"+gameObject.name, "确定", "取消"))
{
scriptObjectSaveData.dic[sceneNmae + gameObject.name] = new List<InformationBase>(_list);
}
else
{
return false;
}
}
else
{
// 如果不存在,添加新的键值对
scriptObjectSaveData.dic.Add(sceneNmae + gameObject.name, new List<InformationBase>(_list));
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
else
{
Debug.LogError("ScriptObjectSaveData配置文件丢失");
return false;
}
return true;
}
return false;
}
//private int guidIndex;
//private List<string> strings = new List<string>();
//绘制
public override void Draw()
{
int length = _list.Count;
int deleteIndex = -1;
for (int i = 0; i < length; i++)
{
GUILayout.BeginHorizontal();
_list[i].desc = UnityEditor.EditorGUILayout.TextField(_list[i].desc);
if (GUILayout.Button("切换")) Switch(i);
if (GUILayout.Button("保存"))
{
Save(i);
if (PlayingSave()) Debug.Log("保存成功!");
}
if (GUILayout.Button("上移") && i > 0)
{
T temp = _list[i - 1];
_list[i - 1] = _list[i];
_list[i] = temp;
}
if (GUILayout.Button("下移") && i < _list.Count - 1)
{
T temp = _list[i + 1];
_list[i + 1] = _list[i];
_list[i] = temp;
}
if (GUILayout.Button("删除") && UnityEditor.EditorUtility.DisplayDialog("警告", "你确定要删除吗?", "确定", "取消"))
deleteIndex = i;
GUILayout.EndHorizontal();
if (i != length - 1) GUILayout.Space(20);
}
if (deleteIndex != -1) Delete(deleteIndex);
GUILayout.Space(30);
GUILayout.BeginHorizontal();
if (GUILayout.Button("添加"))
{
Add();
}
if (GUILayout.Button("更新") && UnityEditor.EditorUtility.DisplayDialog("提示", "你确定要更新吗?", "确定", "取消"))
{
UpdateInformation();
GUILayout.BeginHorizontal();
}
GUILayout.EndHorizontal();
}
#else
public override void Draw(){}
#endif
}
#endregion
[System.Serializable]
public class InformationBase
{
public string desc = "初始";
}
}