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

148 lines
4.1 KiB
C#
Raw Normal View History

2025-03-26 09:34:52 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEngine;
namespace Stary.Evo.InformationSave
{
public abstract class AbstractInformation<T> : CustomEditorBase where T : InformationBase, new()
{
2025-05-08 15:57:40 +08:00
2025-03-26 09:34:52 +08:00
[HideInInspector] public List<T> _list = new List<T>();
2025-05-08 15:57:40 +08:00
2025-03-26 09:34:52 +08:00
public virtual void Add()
{
_list.Add(new T());
Save(_list.Count - 1);
}
public abstract void Save(int index);
public abstract void Switch(int index);
2025-04-30 16:15:11 +08:00
2025-03-26 09:34:52 +08:00
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);
}
2025-03-27 16:56:33 +08:00
2025-03-26 09:34:52 +08:00
#region Editor
#if UNITY_EDITOR
2025-05-08 15:57:40 +08:00
private string _path;
private string _directoryPath;
private void SetPath()
{
_directoryPath = Application.persistentDataPath + "/Ignore";
_path = _directoryPath + "/" + mationID;
}
2025-03-26 09:34:52 +08:00
//更新
public void UpdateInformation()
{
2025-05-08 15:57:40 +08:00
SetPath();
if (File.Exists(_path) == false)
2025-04-30 16:15:11 +08:00
{
2025-05-08 15:57:40 +08:00
Debug.LogError("不存在更新文件:" + _path);
2025-03-26 09:34:52 +08:00
return;
}
2025-05-08 15:57:40 +08:00
var json = File.ReadAllText(_path);
List<T> tempList = JsonConvert.DeserializeObject<List<T>>(json);
if (tempList != null && tempList.Count > 0)
2025-03-26 09:34:52 +08:00
{
_list.Clear();
2025-05-08 15:57:40 +08:00
_list.AddRange(tempList);
Debug.Log("更新成功!");
2025-03-26 09:34:52 +08:00
}
}
//动态保存
public bool PlayingSave()
{
2025-05-08 15:57:40 +08:00
if (UnityEditor.EditorApplication.isPlaying)
2025-03-27 16:56:33 +08:00
{
2025-05-08 15:57:40 +08:00
SetPath();
if (Directory.Exists(_directoryPath) == false) Directory.CreateDirectory(_directoryPath);
var json = JsonConvert.SerializeObject(_list);
File.WriteAllText(_path,json);
Debug.Log("动态保存成功!");
return true;
2025-03-27 16:56:33 +08:00
}
2025-03-26 09:34:52 +08:00
2025-05-08 15:57:40 +08:00
return false;
2025-03-27 16:56:33 +08:00
}
2025-03-26 09:34:52 +08:00
//绘制
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);
2025-05-08 15:57:40 +08:00
if (PlayingSave() == false) Debug.Log("保存成功!");
2025-03-26 09:34:52 +08:00
}
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 = "初始";
}
}