diff --git a/Editor/MenuItem/MenuItemYIUIPanelToSource.cs b/Editor/MenuItem/MenuItemYIUIPanelToSource.cs index feb4387..09d6970 100644 --- a/Editor/MenuItem/MenuItemYIUIPanelToSource.cs +++ b/Editor/MenuItem/MenuItemYIUIPanelToSource.cs @@ -1,6 +1,7 @@ #if UNITY_EDITOR using System; using System.Collections.Generic; +using System.IO; using ET; using UnityEditor; using UnityEngine; @@ -89,7 +90,7 @@ static void CreateYIUIPanelByFolder() } } - private static void CreateNewSource(string loadPath, string savePath) + public static void CreateNewSource(string loadPath, string savePath, bool showTips = true) { var loadPanel = (GameObject)AssetDatabase.LoadAssetAtPath(loadPath, typeof(Object)); if (loadPanel == null) @@ -108,7 +109,11 @@ private static void CreateNewSource(string loadPath, string savePath) PrefabUtility.SaveAsPrefabAsset(newSource, savePath); Object.DestroyImmediate(newSource); - UnityTipsHelper.Show($"Panel 逆向 源数据 完毕"); + if (showTips) + { + UnityTipsHelper.Show($"Panel 逆向 源数据 完毕"); + } + AssetDatabase.Refresh(); } @@ -128,7 +133,20 @@ private static void CorrelationViewByParent(string pkgName, List var viewName = viewParent.name.Replace(YIUIConstHelper.Const.UIParentName, ""); - var viewPath = $"{m_PanelPath}/{YIUIConstHelper.Const.UIPrefabs}/{viewName}.prefab"; + //防止修改了生成的view路径 所以改为全局查找 + //var viewPath = $"{m_PanelPath}/{YIUIConstHelper.Const.UIPrefabs}/{viewName}.prefab"; + + var viewPath = ""; + foreach (string guid in AssetDatabase.FindAssets($"{viewName} t:Prefab", null)) + { + viewPath = AssetDatabase.GUIDToAssetPath(guid); + } + + if (string.IsNullOrEmpty(viewPath)) + { + Log.Error($"未找到 {viewName} 预制件"); + continue; + } var childView = viewParent.FindChildByName(viewName); if (childView != null) @@ -150,14 +168,11 @@ private static void AddView(string loadPath, Transform parent) return; } - var prefabInstance = PrefabUtility.InstantiatePrefab(loadView) as GameObject; + var prefabInstance = PrefabUtility.InstantiatePrefab(loadView, parent) as GameObject; if (prefabInstance == null) { Debug.LogError($"{loadView.name} 未知错误 得到一个null 对象"); - return; } - - prefabInstance.transform.SetParent(parent, false); } } } diff --git a/Editor/MenuItem/UIPanelSourceSplit.cs b/Editor/MenuItem/UIPanelSourceSplit.cs index d330540..54a4f96 100644 --- a/Editor/MenuItem/UIPanelSourceSplit.cs +++ b/Editor/MenuItem/UIPanelSourceSplit.cs @@ -1,6 +1,5 @@ #if UNITY_EDITOR using System.Collections.Generic; -using ET; using UnityEditor; using UnityEngine; using Object = UnityEngine.Object; @@ -63,7 +62,8 @@ public static void Do(UIBindCDETable source) AllViewSaveAsPrefabAsset(oldSplitData.AllPopupView, splitData.AllPopupView, savePath); //拆分后新的 - SaveAsPrefabAsset(newSource, $"{savePath}/{newSource.name}.prefab"); + var newPath = $"{savePath}/{newSource.name}.prefab"; + SaveAsPrefabAsset(newSource, newPath); Object.DestroyImmediate(newSource); //老的重新关联 覆盖老数据 @@ -134,26 +134,22 @@ private static bool SaveAsPrefabAssetViewParent(RectTransform oldViewParent, Object.DestroyImmediate(oldView.gameObject); //old 是每个下面都有一个关联上 - var oldPrefabInstance = PrefabUtility.InstantiatePrefab(viewPrefab) as GameObject; + var oldPrefabInstance = PrefabUtility.InstantiatePrefab(viewPrefab, oldViewParent) as GameObject; if (oldPrefabInstance == null) { Debug.LogError($"{oldViewParent.name} Old未知错误 得到一个null 对象"); return false; } - oldPrefabInstance.transform.SetParent(oldViewParent, false); - //新要根据情况保留的才关联 if (nest) { - var prefabInstance = PrefabUtility.InstantiatePrefab(viewPrefab) as GameObject; + var prefabInstance = PrefabUtility.InstantiatePrefab(viewPrefab, viewParent) as GameObject; if (prefabInstance == null) { Debug.LogError($"{viewParent.name} 未知错误 得到一个null 对象"); return false; } - - prefabInstance.transform.SetParent(viewParent, false); } return true; @@ -165,27 +161,24 @@ private static GameObject SaveAsPrefabAsset(GameObject obj, string path) AssetDatabase.SaveAssets(); EditorApplication.ExecuteMenuItem("Assets/Refresh"); var selectPath = path.Replace("Assets/../", ""); - var prefab = AssetDatabase.LoadAssetAtPath(selectPath); + var prefab = (GameObject)AssetDatabase.LoadAssetAtPath(selectPath, typeof(Object)); if (prefab == null) { Debug.LogError($"生成完毕 {obj.name} 请手动检查所有"); } else { - if (prefab is GameObject go) + var cde = prefab.GetComponent(); + if (cde == null) { - var cde = go.GetComponent(); - if (cde == null) - { - Debug.LogError($"{obj.name} cde == null"); - } - else - { - cde.AutoCheck(); - } - - return go; + Debug.LogError($"{obj.name} cde == null"); } + else + { + cde.AutoCheck(); + } + + return prefab; } return null;