From 0cf4bf060fcb3a600021a67112f5d46831dc4fda Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Thu, 19 Dec 2024 00:00:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=80=A7=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=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 | 8 + .../Script/AxiProjectTools/AxiPrefabCache.cs | 24 +++ .../AxiProjectTools/AxiPrefabCache.cs.meta | 2 + .../AxiProjectTools/PrefabPathLister.cs | 203 ++++++++++++++++++ .../AxiProjectTools/PrefabPathLister.cs.meta | 2 + 5 files changed, 239 insertions(+) create mode 100644 AxibugEmuOnline.Client/Assets/Script/AxiProjectTools.meta create mode 100644 AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs create mode 100644 AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs.meta create mode 100644 AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs create mode 100644 AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs.meta diff --git a/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools.meta b/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools.meta new file mode 100644 index 00000000..df46cdce --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e745b95dbce117b48b4bd17df06ed302 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs b/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs new file mode 100644 index 00000000..9f13930d --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + + + +[CreateAssetMenu(fileName = "CrossMapCfg", menuName = "Tools/AxiPrefabCachec", order = 0)] +public class AxiPrefabCache : ScriptableObject +{ + public List caches = new List(); +} + +[Serializable] +public class AxiPrefabCache_Com2GUID +{ + public string SrcFullName; + public string SrcName; + public string GUID; + public string ToName; + public string ToPATH; + public string ToGUID; + public MonoScript monoScript; +} \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs.meta b/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs.meta new file mode 100644 index 00000000..eea85e7a --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/AxiPrefabCache.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 47ae96bfd5393694da28c22ab2ccfb7c \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs b/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs new file mode 100644 index 00000000..f15c8bab --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs @@ -0,0 +1,203 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using UnityEditor; +using UnityEditorInternal; +using UnityEngine; + +public class PrefabComponentLister : EditorWindow +{ + static string cachecfgPath = "Assets/AxiComToolCache.asset"; + static string outCsDir = Application.dataPath + "/AxiCom/"; + static Dictionary ComType2GUID = new Dictionary(); + + [MenuItem("移植工具/[1]采集所有预制体下的UGUI组件")] + public static void Part1() + { + ComType2GUID.Clear(); + string[] prefabGuids = AssetDatabase.FindAssets("t:Prefab"); + foreach (string guid in prefabGuids) + { + string path = AssetDatabase.GUIDToAssetPath(guid); + GetPrefab(path); + } + + AxiPrefabCache cache = ScriptableObject.CreateInstance(); + foreach (var data in ComType2GUID) + cache.caches.Add(data.Value); + AssetDatabase.CreateAsset(cache, cachecfgPath); + AssetDatabase.SaveAssets(); + AssetDatabase.Refresh(); + } + static void GetPrefab(string path) + { + GameObject prefab = AssetDatabase.LoadAssetAtPath(path); + LoopPrefabNode(path, prefab.gameObject, 0); + } + static void LoopPrefabNode(string rootPath, GameObject trans, int depth) + { + string nodename = $"{rootPath}>{trans.name}"; + + GameObject prefabRoot = trans.gameObject; + int comCount = prefabRoot.GetComponentCount(); + for (int i = 0; i < comCount; i++) + { + var com = prefabRoot.GetComponentAtIndex(i); + MonoBehaviour monoCom = com as MonoBehaviour; + if (monoCom == null) + continue; + Type monoType = monoCom.GetType(); + if (!monoType.FullName.Contains("UnityEngine.UI")) + continue; + // 获取MonoScript资源 + MonoScript monoScript = MonoScript.FromMonoBehaviour(monoCom); + if (monoScript != null) + { + // 获取MonoScript资源的GUID + string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(monoScript)); + Debug.Log($"{nodename} | [{monoType.Name}] {guid}({monoType.FullName})"); + ComType2GUID[monoType.FullName] = + new AxiPrefabCache_Com2GUID() + { + SrcFullName = monoType.FullName, + SrcName = monoType.Name, + GUID = guid, + }; + } + else + { + Debug.LogError("!!!! 没得"); + } + } + + + + //遍历 + foreach (Transform child in trans.transform) + LoopPrefabNode(nodename, child.gameObject, depth + 1); + } + + [MenuItem("移植工具/[2]生成中间脚本代码")] + public static void Part2() + { + if (Directory.Exists(outCsDir)) + Directory.Delete(outCsDir); + Directory.CreateDirectory(outCsDir); + AxiPrefabCache cache = AssetDatabase.LoadAssetAtPath(cachecfgPath); + foreach (var data in cache.caches) + { + string toName = "Axi" + data.SrcName; + string toPath = outCsDir + toName + ".cs"; + string codeStr = "using UnityEngine.UI; public class " + toName + " : " + data.SrcName + " {}"; + try + { + System.IO.File.WriteAllText(toPath, codeStr); + data.ToName = toName; + data.ToPATH = toPath; + } + catch (Exception ex) + { + Debug.LogError("写入失败" + ex.ToString()); + } + } + Debug.Log("写入完毕"); + AssetDatabase.SaveAssets(); + AssetDatabase.Refresh(); + } + + [MenuItem("移植工具/[3]收集生成的脚本")] + public static void Part3() + { + AxiPrefabCache cache = AssetDatabase.LoadAssetAtPath(cachecfgPath); + List allMonoScripts = FindAllAssetsOfType(); + foreach (var data in cache.caches) + { + MonoScript monoScript = allMonoScripts.FirstOrDefault(w => w.name == data.ToName); + if (monoScript == null) + { + Debug.LogError("没找到" + data.ToName); + continue; + } + string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(monoScript)); + data.ToGUID = guid; + data.monoScript = monoScript; + } + Debug.Log("写入完毕"); + AssetDatabase.SaveAssets(); + AssetDatabase.Refresh(); + } + + static List FindAllAssetsOfType() where T : UnityEngine.Object + { + List assets = new List(); + + string[] allGuids = AssetDatabase.FindAssets(""); + foreach (string guid in allGuids) + { + string path = AssetDatabase.GUIDToAssetPath(guid); + if (path.EndsWith(".cs") || path.EndsWith(".js") || path.EndsWith(".boo")) // Unity支持多种脚本语言,但现代Unity主要使用C# + { + T asset = AssetDatabase.LoadAssetAtPath(path); + if (asset != null) + { + assets.Add(asset); + } + } + } + + return assets; + } + + + [MenuItem("移植工具/[4]替换所有预制体")] + public static void Part4() + { + AxiPrefabCache cache = AssetDatabase.LoadAssetAtPath(cachecfgPath); + Dictionary tempReplaceDict = new Dictionary(); + foreach(var data in cache.caches) + { + tempReplaceDict[data.GUID] = data.ToGUID; + } + ProcessAllPrefabs("*.prefab", tempReplaceDict); + AssetDatabase.SaveAssets(); + AssetDatabase.Refresh(); + } + + static void ProcessAllPrefabs(string form, Dictionary tempReplaceDict, bool reverse = false) + { + List prefabs = new List(); + var resourcesPath = Application.dataPath; + var absolutePaths = Directory.GetFiles(resourcesPath, form, SearchOption.AllDirectories); + for (int i = 0; i < absolutePaths.Length; i++) + { + Debug.Log("prefab name: " + absolutePaths[i]); + foreach (var VARIABLE in tempReplaceDict) + { + string oldValue = reverse ? VARIABLE.Value : VARIABLE.Key; + string newValue = reverse ? VARIABLE.Key : VARIABLE.Value; + ReplaceValue(absolutePaths[i], oldValue, newValue); + } + EditorUtility.DisplayProgressBar("处理预制体……", "处理预制体中……", (float)i / absolutePaths.Length); + } + EditorUtility.ClearProgressBar(); + } + + /// + /// 替换值 + /// + /// 文件路径 + static void ReplaceValue(string strFilePath, string oldLine, string newLine) + { + if (File.Exists(strFilePath)) + { + string[] lines = File.ReadAllLines(strFilePath); + for (int i = 0; i < lines.Length; i++) + { + lines[i] = lines[i].Replace(oldLine, newLine); + } + File.WriteAllLines(strFilePath, lines); + } + } + +} \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs.meta b/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs.meta new file mode 100644 index 00000000..764d3fac --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AxiProjectTools/PrefabPathLister.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 01c95f5e03a338749b54784eb6420d04 \ No newline at end of file