Skip to content

Commit

Permalink
修复源文件生成
Browse files Browse the repository at this point in the history
  • Loading branch information
LiShengYang-yiyi committed Oct 11, 2024
1 parent 5280979 commit ea8ef72
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
29 changes: 22 additions & 7 deletions Editor/MenuItem/MenuItemYIUIPanelToSource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using ET;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -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)
Expand All @@ -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();
}

Expand All @@ -128,7 +133,20 @@ private static void CorrelationViewByParent(string pkgName, List<RectTransform>

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)
Expand All @@ -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);
}
}
}
Expand Down
35 changes: 14 additions & 21 deletions Editor/MenuItem/UIPanelSourceSplit.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#if UNITY_EDITOR
using System.Collections.Generic;
using ET;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
Expand Down Expand Up @@ -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);

//老的重新关联 覆盖老数据
Expand Down Expand Up @@ -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;
Expand All @@ -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<Object>(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<UIBindCDETable>();
if (cde == null)
{
var cde = go.GetComponent<UIBindCDETable>();
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;
Expand Down

0 comments on commit ea8ef72

Please sign in to comment.