【m】修改位置组件存储信息
This commit is contained in:
@@ -26,6 +26,7 @@ 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)
|
public virtual T GetTransform(string desc)
|
||||||
{
|
{
|
||||||
int index = _list.FindIndex(n => n.desc == desc);
|
int index = _list.FindIndex(n => n.desc == desc);
|
||||||
@@ -57,7 +58,12 @@ namespace Stary.Evo.InformationSave
|
|||||||
//更新
|
//更新
|
||||||
public void UpdateInformation()
|
public void UpdateInformation()
|
||||||
{
|
{
|
||||||
String sceneNmae = gameObject.scene.name;
|
string guid = GetPrefabGUID(this.gameObject)+GetPrefabHierarchyPath(this.gameObject);
|
||||||
|
if (guid == "")
|
||||||
|
{
|
||||||
|
UnityEditor.EditorUtility.DisplayDialog("错误", "可能存在错误\n1、无法在prefab预览中修改数据\n2、新增加物体需先同步为prefab", "确定");
|
||||||
|
return;
|
||||||
|
}
|
||||||
ScriptObjectSave scriptObjectSaveData = Resources.Load<ScriptObjectSave>(path);
|
ScriptObjectSave scriptObjectSaveData = Resources.Load<ScriptObjectSave>(path);
|
||||||
if (scriptObjectSaveData == null)
|
if (scriptObjectSaveData == null)
|
||||||
{
|
{
|
||||||
@@ -65,14 +71,14 @@ namespace Stary.Evo.InformationSave
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scriptObjectSaveData.dic.ContainsKey(sceneNmae + gameObject.name))
|
if (scriptObjectSaveData.dic.ContainsKey(guid))
|
||||||
{
|
{
|
||||||
_list.Clear();
|
_list.Clear();
|
||||||
_list = scriptObjectSaveData.dic[sceneNmae + gameObject.name].OfType<T>().ToList();
|
_list = scriptObjectSaveData.dic[guid].OfType<T>().ToList();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.LogError("UnityEvo:ScriptObjectSaveData中未存储场景:"+ sceneNmae + " 中物体:" + gameObject.name + "的数据!!!");
|
Debug.LogError($"UnityEvo:ScriptObjectSaveData中未存储物体名为:{this.gameObject}\n guid为:{guid}的数据!!!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,49 +86,53 @@ namespace Stary.Evo.InformationSave
|
|||||||
public bool PlayingSave()
|
public bool PlayingSave()
|
||||||
{
|
{
|
||||||
//文件保存
|
//文件保存
|
||||||
if (Application.isEditor)
|
|
||||||
|
string guid = GetPrefabGUID(this.gameObject)+GetPrefabHierarchyPath(this.gameObject);
|
||||||
|
if (guid == "")
|
||||||
{
|
{
|
||||||
String sceneNmae = gameObject.scene.name;
|
UnityEditor.EditorUtility.DisplayDialog("错误", "可能存在错误\n1、无法在prefab预览中修改数据\n2、新增加物体需先同步为prefab", "确定");
|
||||||
// 加载 ScriptObjectSaveData
|
return false;
|
||||||
ScriptObjectSave scriptObjectSaveData = Resources.Load<ScriptObjectSave>(path);
|
}
|
||||||
if(scriptObjectSaveData == null)
|
// 加载 ScriptObjectSaveData
|
||||||
{
|
ScriptObjectSave scriptObjectSaveData = Resources.Load<ScriptObjectSave>(path);
|
||||||
scriptObjectSaveData = CheckAndCreateFoldersAndAsset();
|
if (scriptObjectSaveData == null)
|
||||||
//Debug.Log("创建了ScriptObjectSaveData文件");
|
{
|
||||||
}
|
scriptObjectSaveData = CheckAndCreateFoldersAndAsset();
|
||||||
|
//Debug.Log("创建了ScriptObjectSaveData文件");
|
||||||
|
}
|
||||||
|
|
||||||
if (scriptObjectSaveData.dic == null)
|
if (scriptObjectSaveData.dic == null)
|
||||||
{
|
{
|
||||||
Debug.LogError("UnityEvo:ScriptObjectSaveData的dic为空");
|
Debug.LogError("UnityEvo:ScriptObjectSaveData的dic为空");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否已经存在相同的键
|
// 检查是否已经存在相同的键
|
||||||
if (scriptObjectSaveData.dic.ContainsKey(sceneNmae + gameObject.name))
|
if (scriptObjectSaveData.dic.ContainsKey(guid))
|
||||||
|
{
|
||||||
|
// 如果存在,选择是否替换原有的 List
|
||||||
|
if (UnityEditor.EditorUtility.DisplayDialog("提示",
|
||||||
|
$"数据集下已有相同名称的物体数据\n是否覆盖更新!!!\n 物体名为:{this.gameObject}\n guid为:{guid}", "确定",
|
||||||
|
"取消"))
|
||||||
{
|
{
|
||||||
// 如果存在,选择是否替换原有的 List
|
scriptObjectSaveData.dic[guid] = new List<InformationBase>(_list);
|
||||||
if (UnityEditor.EditorUtility.DisplayDialog("提示", "数据集下已有相同名称的物体数据\n是否覆盖更新!!!\n" + "场景名:" + sceneNmae + "\n物体名:" + gameObject.name, "确定", "取消"))
|
|
||||||
{
|
|
||||||
scriptObjectSaveData.dic[sceneNmae + gameObject.name] = new List<InformationBase>(_list);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 如果不存在,添加新的键值对
|
return false;
|
||||||
scriptObjectSaveData.dic.Add(sceneNmae + gameObject.name, new List<InformationBase>(_list));
|
|
||||||
}
|
}
|
||||||
AssetDatabase.SaveAssets();
|
}
|
||||||
EditorUtility.SetDirty(scriptObjectSaveData);
|
else
|
||||||
AssetDatabase.Refresh();
|
{
|
||||||
|
// 如果不存在,添加新的键值对
|
||||||
return true;
|
scriptObjectSaveData.dic.Add(guid, new List<InformationBase>(_list));
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
AssetDatabase.SaveAssets();
|
||||||
|
EditorUtility.SetDirty(scriptObjectSaveData);
|
||||||
|
AssetDatabase.Refresh();
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ScriptObjectSave CheckAndCreateFoldersAndAsset()
|
private ScriptObjectSave CheckAndCreateFoldersAndAsset()
|
||||||
@@ -148,17 +158,19 @@ namespace Stary.Evo.InformationSave
|
|||||||
|
|
||||||
// 创建ScriptObjectSaveData.asset文件
|
// 创建ScriptObjectSaveData.asset文件
|
||||||
scriptableObject = ScriptableObject.CreateInstance<ScriptObjectSave>();
|
scriptableObject = ScriptableObject.CreateInstance<ScriptObjectSave>();
|
||||||
AssetDatabase.CreateAsset(scriptableObject, "Assets/" + ResourcesFolderName + "/" + InformationSaveDataFolderName + "/" + ScriptObjectSaveDataFileName + ".asset");
|
AssetDatabase.CreateAsset(scriptableObject,
|
||||||
|
"Assets/" + ResourcesFolderName + "/" + InformationSaveDataFolderName + "/" +
|
||||||
|
ScriptObjectSaveDataFileName + ".asset");
|
||||||
AssetDatabase.SaveAssets();
|
AssetDatabase.SaveAssets();
|
||||||
AssetDatabase.Refresh();
|
AssetDatabase.Refresh();
|
||||||
Debug.Log("UnityEvo:创建了: " + "Assets/" + ResourcesFolderName + "/" + InformationSaveDataFolderName + "/" + ScriptObjectSaveDataFileName + ".asset 资产");
|
Debug.Log("UnityEvo:创建了: " + "Assets/" + ResourcesFolderName + "/" + InformationSaveDataFolderName + "/" +
|
||||||
|
ScriptObjectSaveDataFileName + ".asset 资产");
|
||||||
return scriptableObject;
|
return scriptableObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
//绘制
|
//绘制
|
||||||
public override void Draw()
|
public override void Draw()
|
||||||
{
|
{
|
||||||
|
|
||||||
int length = _list.Count;
|
int length = _list.Count;
|
||||||
int deleteIndex = -1;
|
int deleteIndex = -1;
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
@@ -209,7 +221,53 @@ 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
|
||||||
@@ -217,6 +275,7 @@ namespace Stary.Evo.InformationSave
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class InformationBase
|
public class InformationBase
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "com.staryevo.informationsave",
|
"name": "com.staryevo.informationsave",
|
||||||
"displayName": "02.InformationSave",
|
"displayName": "02.InformationSave",
|
||||||
"version": "1.0.3",
|
"version": "1.0.4",
|
||||||
"description": "位置配置工具",
|
"description": "位置配置工具",
|
||||||
"unity": "2021.3",
|
"unity": "2021.3",
|
||||||
"unityRelease": "30f1",
|
"unityRelease": "30f1",
|
||||||
|
|||||||
Reference in New Issue
Block a user