From f609feace5f399e722a0aacda8d6ef4556fb7425 Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Thu, 19 Dec 2024 00:46:14 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E6=A4=8D=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/{Script => }/AxiProjectTools.meta | 0 .../AxiProjectTools/AxiPrefabCache.cs | 0 .../AxiProjectTools/AxiPrefabCache.cs.meta | 0 .../AxiProjectTools/PrefabPathLister.cs | 54 ++++++++++++++++++- .../AxiProjectTools/PrefabPathLister.cs.meta | 0 5 files changed, 52 insertions(+), 2 deletions(-) rename AxibugEmuOnline.Client/Assets/{Script => }/AxiProjectTools.meta (100%) rename AxibugEmuOnline.Client/Assets/{Script => }/AxiProjectTools/AxiPrefabCache.cs (100%) rename AxibugEmuOnline.Client/Assets/{Script => }/AxiProjectTools/AxiPrefabCache.cs.meta (100%) rename AxibugEmuOnline.Client/Assets/{Script => }/AxiProjectTools/PrefabPathLister.cs (81%) rename AxibugEmuOnline.Client/Assets/{Script => }/AxiProjectTools/PrefabPathLister.cs.meta (100%) diff --git a/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools.meta b/AxibugEmuOnline.Client/Assets/AxiProjectTools.meta similarity index 100% rename from AxibugEmuOnline.Client/Assets/Script/AxiProjectTools.meta rename to AxibugEmuOnline.Client/Assets/AxiProjectTools.meta diff --git a/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs b/AxibugEmuOnline.Client/Assets/AxiProjectTools/AxiPrefabCache.cs similarity index 100% rename from AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs rename to AxibugEmuOnline.Client/Assets/AxiProjectTools/AxiPrefabCache.cs diff --git a/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs.meta b/AxibugEmuOnline.Client/Assets/AxiProjectTools/AxiPrefabCache.cs.meta similarity index 100% rename from AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs.meta rename to AxibugEmuOnline.Client/Assets/AxiProjectTools/AxiPrefabCache.cs.meta diff --git a/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs b/AxibugEmuOnline.Client/Assets/AxiProjectTools/PrefabPathLister.cs similarity index 81% rename from AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs rename to AxibugEmuOnline.Client/Assets/AxiProjectTools/PrefabPathLister.cs index f15c8ba..3797f07 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs +++ b/AxibugEmuOnline.Client/Assets/AxiProjectTools/PrefabPathLister.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; -using UnityEditorInternal; using UnityEngine; public class PrefabComponentLister : EditorWindow @@ -155,7 +154,7 @@ public class PrefabComponentLister : EditorWindow { AxiPrefabCache cache = AssetDatabase.LoadAssetAtPath(cachecfgPath); Dictionary tempReplaceDict = new Dictionary(); - foreach(var data in cache.caches) + foreach (var data in cache.caches) { tempReplaceDict[data.GUID] = data.ToGUID; } @@ -200,4 +199,55 @@ public class PrefabComponentLister : EditorWindow } } + + [MenuItem("移植工具/[5]UnPack")] + public static void UnpackPrefabs() + { + string[] allAssetPaths = AssetDatabase.GetAllAssetPaths(); + int prefabCount = 0; + + foreach (string path in allAssetPaths) + { + if (Path.GetExtension(path).Equals(".prefab")) + { + Debug.Log($"Unpacking {path}"); + UnpackPrefab(path); + prefabCount++; + } + } + + Debug.Log($"Unpacked {prefabCount} prefabs."); + } + + static void UnpackPrefab(string prefabPath) + { + GameObject prefabInstance = AssetDatabase.LoadAssetAtPath(prefabPath); + if (prefabInstance == null) + { + Debug.LogError($"Failed to load prefab at path: {prefabPath}"); + return; + } + + var obj = GameObject.Instantiate(prefabInstance, null); + TraverseHierarchy(obj); + PrefabUtility.SaveAsPrefabAsset(obj, prefabPath); + GameObject.DestroyImmediate(obj); + } + + static void TraverseHierarchy(GameObject obj) + { + // 检查该对象是否是预制体的实例 + if (PrefabUtility.IsPartOfPrefabInstance(obj)) + { + // 将预制体实例转换为普通游戏对象 + PrefabUtility.UnpackPrefabInstance(obj, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction); + Debug.Log("Prefab instance converted to game object: " + obj.name); + } + + // 递归遍历子对象 + for (int i = 0; i < obj.transform.childCount; i++) + { + TraverseHierarchy(obj.transform.GetChild(i).gameObject); + } + } } \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs.meta b/AxibugEmuOnline.Client/Assets/AxiProjectTools/PrefabPathLister.cs.meta similarity index 100% rename from AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs.meta rename to AxibugEmuOnline.Client/Assets/AxiProjectTools/PrefabPathLister.cs.meta