02 位置工具优化
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
|
using System;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEditor.SceneManagement;
|
using UnityEditor.SceneManagement;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using Object = UnityEngine.Object;
|
||||||
|
|
||||||
namespace Stary.Evo.InformationSave
|
namespace Stary.Evo.InformationSave
|
||||||
{
|
{
|
||||||
@@ -9,16 +11,33 @@ namespace Stary.Evo.InformationSave
|
|||||||
{
|
{
|
||||||
private CustomEditorBase _customEditorBase;
|
private CustomEditorBase _customEditorBase;
|
||||||
|
|
||||||
|
|
||||||
public override void OnInspectorGUI()
|
public override void OnInspectorGUI()
|
||||||
{
|
{
|
||||||
base.OnInspectorGUI();
|
base.OnInspectorGUI();
|
||||||
serializedObject.Update();
|
serializedObject.Update();
|
||||||
_customEditorBase = target as CustomEditorBase;
|
_customEditorBase = target as CustomEditorBase;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(_customEditorBase.mationID))
|
||||||
|
{
|
||||||
|
_customEditorBase.mationID = _customEditorBase.gameObject.GetInstanceID().ToString();
|
||||||
|
}
|
||||||
|
GUILayout.BeginHorizontal();
|
||||||
|
GUILayout.Box("MationID:", GUILayout.MaxWidth(500));
|
||||||
|
GUILayout.Box(_customEditorBase.mationID, GUILayout.MaxWidth(500));
|
||||||
|
GUILayout.EndHorizontal();
|
||||||
|
|
||||||
_customEditorBase.Draw();
|
_customEditorBase.Draw();
|
||||||
//这个函数告诉引擎,相关对象所属于的Prefab已经发生了更改。方便,当我们更改了自定义对象的属性的时候,自动更新到所属的Prefab中
|
//这个函数告诉引擎,相关对象所属于的Prefab已经发生了更改。方便,当我们更改了自定义对象的属性的时候,自动更新到所属的Prefab中
|
||||||
if (GUI.changed && EditorApplication.isPlaying == false)
|
if (GUI.changed && EditorApplication.isPlaying == false)
|
||||||
{
|
{
|
||||||
serializedObject.ApplyModifiedProperties();
|
serializedObject.ApplyModifiedProperties();
|
||||||
|
|
||||||
|
var prefabType = PrefabUtility.GetPrefabInstanceStatus(_customEditorBase);
|
||||||
|
if (prefabType != PrefabInstanceStatus.NotAPrefab)
|
||||||
|
{
|
||||||
|
PrefabUtility.RecordPrefabInstancePropertyModifications(_customEditorBase);
|
||||||
|
}
|
||||||
EditorUtility.SetDirty(_customEditorBase);
|
EditorUtility.SetDirty(_customEditorBase);
|
||||||
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
|
EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,17 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using UnityEditor;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Stary.Evo.InformationSave
|
namespace Stary.Evo.InformationSave
|
||||||
{
|
{
|
||||||
public abstract class AbstractInformation<T> : CustomEditorBase where T : InformationBase, new()
|
public abstract class AbstractInformation<T> : CustomEditorBase where T : InformationBase, new()
|
||||||
{
|
{
|
||||||
|
|
||||||
[HideInInspector] public List<T> _list = new List<T>();
|
[HideInInspector] public List<T> _list = new List<T>();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 配置文件存储路径
|
|
||||||
/// </summary>
|
|
||||||
string path = "InformationSaveData/ScriptObjectSaveData";
|
|
||||||
|
|
||||||
public virtual void Add()
|
public virtual void Add()
|
||||||
{
|
{
|
||||||
@@ -27,20 +22,6 @@ namespace Stary.Evo.InformationSave
|
|||||||
public abstract void Save(int index);
|
public abstract void Save(int index);
|
||||||
public abstract void Switch(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($"UnityEvo:{typeof(T)}:不存在该信息:" + desc);
|
|
||||||
return default(T);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual void Delete(int index)
|
public virtual void Delete(int index)
|
||||||
{
|
{
|
||||||
_list.RemoveAt(index);
|
_list.RemoveAt(index);
|
||||||
@@ -55,117 +36,49 @@ namespace Stary.Evo.InformationSave
|
|||||||
#region Editor
|
#region Editor
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
|
private string _path;
|
||||||
|
private string _directoryPath;
|
||||||
|
|
||||||
|
private void SetPath()
|
||||||
|
{
|
||||||
|
_directoryPath = Application.persistentDataPath + "/Ignore";
|
||||||
|
_path = _directoryPath + "/" + mationID;
|
||||||
|
}
|
||||||
|
|
||||||
//更新
|
//更新
|
||||||
public void UpdateInformation()
|
public void UpdateInformation()
|
||||||
{
|
{
|
||||||
string guid = GetPrefabGUID(this.gameObject)+GetPrefabHierarchyPath(this.gameObject);
|
SetPath();
|
||||||
if (guid == "")
|
if (File.Exists(_path) == false)
|
||||||
{
|
{
|
||||||
UnityEditor.EditorUtility.DisplayDialog("错误", "可能存在错误\n1、无法在prefab预览中修改数据\n2、新增加物体需先同步为prefab", "确定");
|
Debug.LogError("不存在更新文件:" + _path);
|
||||||
return;
|
|
||||||
}
|
|
||||||
ScriptObjectSave scriptObjectSaveData = Resources.Load<ScriptObjectSave>(path);
|
|
||||||
if (scriptObjectSaveData == null)
|
|
||||||
{
|
|
||||||
Debug.LogError("UnityEvo:ScriptObjectSaveData配置文件丢失");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scriptObjectSaveData.dic.ContainsKey(guid))
|
var json = File.ReadAllText(_path);
|
||||||
|
List<T> tempList = JsonConvert.DeserializeObject<List<T>>(json);
|
||||||
|
if (tempList != null && tempList.Count > 0)
|
||||||
{
|
{
|
||||||
_list.Clear();
|
_list.Clear();
|
||||||
_list = scriptObjectSaveData.dic[guid].OfType<T>().ToList();
|
_list.AddRange(tempList);
|
||||||
}
|
Debug.Log("更新成功!");
|
||||||
else
|
|
||||||
{
|
|
||||||
Debug.LogError($"UnityEvo:ScriptObjectSaveData中未存储物体名为:{this.gameObject}\n guid为:{guid}的数据!!!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//动态保存
|
//动态保存
|
||||||
public bool PlayingSave()
|
public bool PlayingSave()
|
||||||
{
|
{
|
||||||
//文件保存
|
if (UnityEditor.EditorApplication.isPlaying)
|
||||||
|
|
||||||
string guid = GetPrefabGUID(this.gameObject)+GetPrefabHierarchyPath(this.gameObject);
|
|
||||||
if (guid == "")
|
|
||||||
{
|
{
|
||||||
UnityEditor.EditorUtility.DisplayDialog("错误", "可能存在错误\n1、无法在prefab预览中修改数据\n2、新增加物体需先同步为prefab", "确定");
|
SetPath();
|
||||||
return false;
|
if (Directory.Exists(_directoryPath) == false) Directory.CreateDirectory(_directoryPath);
|
||||||
}
|
var json = JsonConvert.SerializeObject(_list);
|
||||||
// 加载 ScriptObjectSaveData
|
File.WriteAllText(_path,json);
|
||||||
ScriptObjectSave scriptObjectSaveData = Resources.Load<ScriptObjectSave>(path);
|
Debug.Log("动态保存成功!");
|
||||||
if (scriptObjectSaveData == null)
|
|
||||||
{
|
|
||||||
scriptObjectSaveData = CheckAndCreateFoldersAndAsset();
|
|
||||||
//Debug.Log("创建了ScriptObjectSaveData文件");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (scriptObjectSaveData.dic == null)
|
|
||||||
{
|
|
||||||
Debug.LogError("UnityEvo:ScriptObjectSaveData的dic为空");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查是否已经存在相同的键
|
|
||||||
if (scriptObjectSaveData.dic.ContainsKey(guid))
|
|
||||||
{
|
|
||||||
// 如果存在,选择是否替换原有的 List
|
|
||||||
if (UnityEditor.EditorUtility.DisplayDialog("提示",
|
|
||||||
$"数据集下已有相同名称的物体数据\n是否覆盖更新!!!\n 物体名为:{this.gameObject}\n guid为:{guid}", "确定",
|
|
||||||
"取消"))
|
|
||||||
{
|
|
||||||
scriptObjectSaveData.dic[guid] = new List<InformationBase>(_list);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// 如果不存在,添加新的键值对
|
|
||||||
scriptObjectSaveData.dic.Add(guid, new List<InformationBase>(_list));
|
|
||||||
}
|
|
||||||
|
|
||||||
AssetDatabase.SaveAssets();
|
|
||||||
EditorUtility.SetDirty(scriptObjectSaveData);
|
|
||||||
AssetDatabase.Refresh();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ScriptObjectSave CheckAndCreateFoldersAndAsset()
|
return false;
|
||||||
{
|
|
||||||
string ResourcesFolderName = "Resources";
|
|
||||||
string InformationSaveDataFolderName = "InformationSaveData";
|
|
||||||
string ScriptObjectSaveDataFileName = "ScriptObjectSaveData";
|
|
||||||
ScriptObjectSave scriptableObject;
|
|
||||||
|
|
||||||
// 判断是否创建Resources文件夹
|
|
||||||
string resourcesPath = Path.Combine(Application.dataPath, ResourcesFolderName);
|
|
||||||
if (!Directory.Exists(resourcesPath))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(resourcesPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 判断是否创建InformationSaveData文件夹
|
|
||||||
string informationSaveDataPath = Path.Combine(resourcesPath, InformationSaveDataFolderName);
|
|
||||||
if (!Directory.Exists(informationSaveDataPath))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(informationSaveDataPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建ScriptObjectSaveData.asset文件
|
|
||||||
scriptableObject = ScriptableObject.CreateInstance<ScriptObjectSave>();
|
|
||||||
AssetDatabase.CreateAsset(scriptableObject,
|
|
||||||
"Assets/" + ResourcesFolderName + "/" + InformationSaveDataFolderName + "/" +
|
|
||||||
ScriptObjectSaveDataFileName + ".asset");
|
|
||||||
AssetDatabase.SaveAssets();
|
|
||||||
AssetDatabase.Refresh();
|
|
||||||
Debug.Log("UnityEvo:创建了: " + "Assets/" + ResourcesFolderName + "/" + InformationSaveDataFolderName + "/" +
|
|
||||||
ScriptObjectSaveDataFileName + ".asset 资产");
|
|
||||||
return scriptableObject;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//绘制
|
//绘制
|
||||||
@@ -181,7 +94,7 @@ namespace Stary.Evo.InformationSave
|
|||||||
if (GUILayout.Button("保存"))
|
if (GUILayout.Button("保存"))
|
||||||
{
|
{
|
||||||
Save(i);
|
Save(i);
|
||||||
if (PlayingSave()) Debug.Log("UnityEvo:保存成功!");
|
if (PlayingSave() == false) Debug.Log("保存成功!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GUILayout.Button("上移") && i > 0)
|
if (GUILayout.Button("上移") && i > 0)
|
||||||
@@ -220,54 +133,6 @@ namespace Stary.Evo.InformationSave
|
|||||||
|
|
||||||
GUILayout.EndHorizontal();
|
GUILayout.EndHorizontal();
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetCurrentGUID()
|
|
||||||
{
|
|
||||||
string assetPath = AssetDatabase.GetAssetPath(this);
|
|
||||||
if (!string.IsNullOrEmpty(assetPath))
|
|
||||||
{
|
|
||||||
return AssetDatabase.AssetPathToGUID(assetPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetPrefabGUID(GameObject obj)
|
|
||||||
{
|
|
||||||
string assetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(obj);
|
|
||||||
Debug.Log("UnityEvo:GetPrefabHierarchyPath:" + GetPrefabHierarchyPath(obj));
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(assetPath))
|
|
||||||
{
|
|
||||||
return AssetDatabase.AssetPathToGUID(assetPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private string GetPrefabHierarchyPath(GameObject obj)
|
|
||||||
{
|
|
||||||
var path = new System.Text.StringBuilder();
|
|
||||||
var current = obj.transform;
|
|
||||||
//检测该预制体是否是实例
|
|
||||||
var root = PrefabUtility.GetNearestPrefabInstanceRoot(obj);
|
|
||||||
if (root != null)
|
|
||||||
{
|
|
||||||
while (current != null && current != root.transform)
|
|
||||||
{
|
|
||||||
path.Insert(0, current.name);
|
|
||||||
path.Insert(0, "/");
|
|
||||||
current = current.parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return path.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
#else
|
||||||
public override void Draw(){}
|
public override void Draw(){}
|
||||||
#endif
|
#endif
|
||||||
@@ -275,7 +140,6 @@ namespace Stary.Evo.InformationSave
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class InformationBase
|
public class InformationBase
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
|
using Unity.Collections;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Stary.Evo.InformationSave
|
namespace Stary.Evo.InformationSave
|
||||||
{
|
{
|
||||||
public abstract class CustomEditorBase : MonoBehaviour
|
public abstract class CustomEditorBase : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
[HideInInspector]
|
||||||
|
public string mationID;
|
||||||
public abstract void Draw();
|
public abstract void Draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 6543baeed3e940268fc41b7eb6fa31a7
|
|
||||||
timeCreated: 1735626715
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Stary.Evo.InformationSave;
|
|
||||||
using UnityEditor;
|
|
||||||
using UnityEngine;
|
|
||||||
using Sirenix.OdinInspector;
|
|
||||||
|
|
||||||
namespace Stary.Evo.InformationSave
|
|
||||||
{
|
|
||||||
[CreateAssetMenu()]
|
|
||||||
public class ScriptObjectSave : SerializedScriptableObject
|
|
||||||
{
|
|
||||||
public Dictionary<string, List<InformationBase>> dic = new Dictionary<string, List<InformationBase>>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: a08c59ee0e6844441825e5b5427d1e4c
|
|
||||||
MonoImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
%YAML 1.1
|
|
||||||
%TAG !u! tag:unity3d.com,2011:
|
|
||||||
--- !u!114 &11400000
|
|
||||||
MonoBehaviour:
|
|
||||||
m_ObjectHideFlags: 0
|
|
||||||
m_CorrespondingSourceObject: {fileID: 0}
|
|
||||||
m_PrefabInstance: {fileID: 0}
|
|
||||||
m_PrefabAsset: {fileID: 0}
|
|
||||||
m_GameObject: {fileID: 0}
|
|
||||||
m_Enabled: 1
|
|
||||||
m_EditorHideFlags: 0
|
|
||||||
m_Script: {fileID: 11500000, guid: a08c59ee0e6844441825e5b5427d1e4c, type: 3}
|
|
||||||
m_Name: ScriptObjectSaveData
|
|
||||||
m_EditorClassIdentifier:
|
|
||||||
serializationData:
|
|
||||||
SerializedFormat: 2
|
|
||||||
SerializedBytes:
|
|
||||||
ReferencedUnityObjects: []
|
|
||||||
SerializedBytesString:
|
|
||||||
Prefab: {fileID: 0}
|
|
||||||
PrefabModificationsReferencedUnityObjects: []
|
|
||||||
PrefabModifications: []
|
|
||||||
SerializationNodes:
|
|
||||||
- Name: dic
|
|
||||||
Entry: 7
|
|
||||||
Data: 0|System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Collections.Generic.List`1[[Stary.Evo.InformationSave.InformationBase,
|
|
||||||
Assembly-CSharp]], mscorlib]], mscorlib
|
|
||||||
- Name: comparer
|
|
||||||
Entry: 7
|
|
||||||
Data: 1|System.Collections.Generic.GenericEqualityComparer`1[[System.String,
|
|
||||||
mscorlib]], mscorlib
|
|
||||||
- Name:
|
|
||||||
Entry: 8
|
|
||||||
Data:
|
|
||||||
- Name:
|
|
||||||
Entry: 12
|
|
||||||
Data: 0
|
|
||||||
- Name:
|
|
||||||
Entry: 13
|
|
||||||
Data:
|
|
||||||
- Name:
|
|
||||||
Entry: 8
|
|
||||||
Data:
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 8e2aa5a2cf392c145a1055e82ed08b6f
|
|
||||||
NativeFormatImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
mainObjectFileID: 11400000
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "com.staryevo.informationsave",
|
"name": "com.staryevo.informationsave",
|
||||||
"displayName": "02.InformationSave",
|
"displayName": "02.InformationSave",
|
||||||
"version": "1.0.4",
|
"version": "1.0.5",
|
||||||
"description": "位置配置工具",
|
"description": "位置配置工具",
|
||||||
"unity": "2021.3",
|
"unity": "2021.3",
|
||||||
"unityRelease": "30f1",
|
"unityRelease": "30f1",
|
||||||
|
|||||||
Reference in New Issue
Block a user