From eca086eabcaddf435e63a8fbdf22b0e2767be79b Mon Sep 17 00:00:00 2001 From: gaoxin Date: Fri, 13 Nov 2020 11:30:01 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=B7=A5=E5=85=B7=E3=80=91=E7=89=B9?= =?UTF-8?q?=E6=95=88=E8=B5=84=E6=BA=90=E6=95=B4=E7=90=86=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Editor/EffectResTools.cs | 138 +++++++++++++++++++ Assets/Scripts/Editor/EffectResTools.cs.meta | 11 ++ Assets/Scripts/Editor/FindReferences.cs | 136 ++++++++++++++++-- 3 files changed, 276 insertions(+), 9 deletions(-) create mode 100644 Assets/Scripts/Editor/EffectResTools.cs create mode 100644 Assets/Scripts/Editor/EffectResTools.cs.meta diff --git a/Assets/Scripts/Editor/EffectResTools.cs b/Assets/Scripts/Editor/EffectResTools.cs new file mode 100644 index 0000000000..64c941f605 --- /dev/null +++ b/Assets/Scripts/Editor/EffectResTools.cs @@ -0,0 +1,138 @@ +using System.Collections; +using System.Collections.Generic; +using System.IO; +using UnityEditor; +using UnityEngine; + +public class EffectResTools +{ + + + private static Dictionary Counter = new Dictionary(); + + // 资源在这些路径下不做处理 + private static string[] _ExceptPath = new string[]{ + "Assets/ManagedResources/Atlas", + "Assets/ManagedResources/BG", + "Assets/ManagedResources/Effects", + "Assets/ManagedResources/PublicArtRes", + "Assets/ManagedResources/DynamicAtlas" + }; + + // 判断资源是否不做处理 + private static bool CheckIsExcept(string path) + { + for (int i = 0; i < _ExceptPath.Length; i++) + { + if (path.StartsWith(_ExceptPath[i])) + return true; + } + return false; + } + + // 获取资源类型文件夹名称 + private static string GetSubFolder(string ext) + { + ext = ext.ToLower(); + if(ext.Equals(".png")||ext.Equals(".tga")||ext.Equals(".jpg")) + { + return "Texture"; + } + else if (ext.Equals(".mat")) + { + return "Material"; + } + else if (ext.Equals(".controller")|| ext.Equals(".anim")) + { + return "Animation"; + } + else if (ext.Equals(".shader")) + { + return "Shader"; + } + else if (ext.Equals(".fbx")||ext.Equals(".obj")) + { + return "Models"; + } + + + return ""; + } + + + + [MenuItem("Tools/Effect/检测重复引用的特效资源")] + private static void CheckEffectRes() + { + List assetPaths = new List(); + assetPaths.Add("Assets/ManagedResources/EffectResSkill"); + assetPaths.Add("Assets/ManagedResources/EffectResUI"); + + // 开始时清除计数器 + Counter.Clear(); + + string[] allPath = AssetDatabase.FindAssets("t:Prefab", assetPaths.ToArray()); + for (int i = 0; i < allPath.Length; i++) + { + string path = AssetDatabase.GUIDToAssetPath(allPath[i]); + EditorUtility.DisplayProgressBar(string.Format("正在检查:({0}/{1})", i, allPath.Length), "路径:" + path, (float)i / allPath.Length); + CheckSingleRes(path); + } + + // 结束时再次清除计数器 + Counter.Clear(); + } + + // 单个资源检测 + private static void CheckSingleRes(string path) + { + var dependencies = AssetDatabase.GetDependencies(path); + for (int j = 0; j < dependencies.Length; j++) + { + string ps = dependencies[j]; + if (CheckIsExcept(ps)) + continue; + int outValue = 0; + if (Counter.TryGetValue(ps, out outValue)) + { + Counter[ps] = outValue + 1; + } + else + { + Counter.Add(ps, 1); + } + + if (outValue > 0) + { + MoveToCommon(ps); + // 动画要把引用的anim也一起移动 + string ext = Path.GetExtension(ps).ToLower(); + if (ext.Equals(".controller")) + { + CheckSingleRes(ps); + } + } + + } + } + + // 移动到公用文件夹 + private static void MoveToCommon(string assetPath) + { + + if (!AssetDatabase.IsValidFolder("Assets/ManagedResources/EffectResCommon")) + { + AssetDatabase.CreateFolder("Assets/ManagedResources", "EffectResCommon"); + } + string subFolderName = GetSubFolder(Path.GetExtension(assetPath)); + if (!AssetDatabase.IsValidFolder("Assets/ManagedResources/EffectResCommon/" + subFolderName)) + { + AssetDatabase.CreateFolder("Assets/ManagedResources/EffectResCommon", subFolderName); + } + + string fileName = Path.GetFileName(assetPath); + string tarPath = "Assets/ManagedResources/EffectResCommon/"+ subFolderName+ "/" + fileName; + AssetDatabase.MoveAsset(assetPath, tarPath); + Debug.LogWarning("MoveAsset " + assetPath + " to " + tarPath); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Editor/EffectResTools.cs.meta b/Assets/Scripts/Editor/EffectResTools.cs.meta new file mode 100644 index 0000000000..4eb7771228 --- /dev/null +++ b/Assets/Scripts/Editor/EffectResTools.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b1cd19bdc18a255489e2f8df5450e0c8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Editor/FindReferences.cs b/Assets/Scripts/Editor/FindReferences.cs index 63cbdf56ee..34250bddea 100644 --- a/Assets/Scripts/Editor/FindReferences.cs +++ b/Assets/Scripts/Editor/FindReferences.cs @@ -98,8 +98,8 @@ public class FindReferences count++; FilePath = ffs[index]; } - }); - } + }); + } } else { @@ -139,8 +139,8 @@ public class FindReferences var gameObj = obj as GameObject; CheckResRef(gameObj.transform, gameObj.transform, Selection.activeObject.name); DebugChildName(gameObj.transform, gameObj.transform, Selection.activeObject.name); - } - } + } + } EditorUtility.ClearProgressBar(); Debug.Log("匹配结束"); @@ -207,7 +207,7 @@ public class FindReferences var spine1 = tran.GetComponent(); isShowPath |= spine1 != null && spine1.skeletonDataAsset != null && spine1.skeletonDataAsset.name == targetName; - + var spine2 = tran.GetComponent(); isShowPath |= spine2 != null && spine2.skeletonDataAsset != null && spine2.skeletonDataAsset.name == targetName; @@ -398,15 +398,15 @@ public class FindReferences if (Selection.activeObject is GameObject) { action(Selection.activeObject as GameObject); - } + } else if (Selection.activeObject is DefaultAsset) { string[] files = Directory.GetFiles(AssetDatabase.GetAssetPath(Selection.activeObject)).Where(s => !s.EndsWith(".meta")).ToArray(); - for(int i = 0; i < files.Length; i++) + for (int i = 0; i < files.Length; i++) { - Debug.LogError("查找路径:"+ files[i]); + Debug.LogError("查找路径:" + files[i]); var obj = AssetDatabase.LoadAssetAtPath(GetRelativeAssetsPath(files[i])); - if(obj is GameObject) + if (obj is GameObject) { action(obj as GameObject); } @@ -463,5 +463,123 @@ public class FindReferences } return path; } + + + /// + /// + /// + private static string[] _CommonAtlas = new string[]{ + "CommonAtlas", + "PopupAtlas", + "PublicAtlas", + "TagAtlas", + "TagButtonAtlas" + }; + + private static bool CheckIsCommonAtlas(string atlasName) + { + for(int i = 0; i < _CommonAtlas.Length; i++) + { + if (atlasName.Equals(_CommonAtlas[i])) + return true; + } + return false; + } + + [MenuItem("Tools/检查并移动ui预设中不规范引用的资源")] + private static void SearchCommonDependencies() + { + List assetPaths = new List(); + assetPaths.Add("Assets/ManagedResources/Prefabs/UI"); + + // + Dictionary Counter = new Dictionary(); + string[] allPath = AssetDatabase.FindAssets("t:Prefab", assetPaths.ToArray()); + for (int i = 0; i < allPath.Length; i++) + { + + EditorUtility.DisplayCancelableProgressBar("正在查找", allPath[i], (float)i / allPath.Length); + + string path = AssetDatabase.GUIDToAssetPath(allPath[i]); + Debug.LogError(path); + + string tarFolder = path.Split('/')[4] + "Atlas"; + + var dependencies = AssetDatabase.GetDependencies(path); + for (int j = 0; j < dependencies.Length; j++) + { + Debug.Log("Dependency: " + dependencies[j]); + if (!dependencies[j].StartsWith("Assets/ManagedResources/Atlas")) + continue; + // 是公用文件 + string[] depArray = dependencies[j].Split('/'); + string depFolder = depArray[3]; + string subFolder= depArray[4];// 有可能是子文件夹 也有可能是文件名 + + if (CheckIsCommonAtlas(depFolder)) + continue; + // 就在自己对应的文件夹内 + if (depFolder.Equals(tarFolder)) + continue; + + string resName = depArray[depArray.Length - 1]; + int outValue = 0; + if(Counter.TryGetValue(resName, out outValue)) + { + tarFolder = "CommonAtlas"; + Counter[resName] = outValue + 1; + } + else{ + Counter.Add(resName, 1); + } + if (!AssetDatabase.IsValidFolder("Assets/ManagedResources/Atlas/" + tarFolder)) + { + AssetDatabase.CreateFolder("Assets/ManagedResources/Atlas", tarFolder); + } + if (!AssetDatabase.IsValidFolder("Assets/ManagedResources/Atlas/" + tarFolder + "/ArtFont")) + { + AssetDatabase.CreateFolder("Assets/ManagedResources/Atlas/" + tarFolder, "ArtFont"); + } + + string tarPath = "Assets/ManagedResources/Atlas/" + tarFolder; + if (subFolder == "ArtFont") + { + tarPath += "/ArtFont"; + } + tarPath += "/" + resName; + AssetDatabase.MoveAsset(dependencies[j], tarPath); + Debug.LogWarning("MoveAsset "+ dependencies[j] + " to "+ tarPath); + } + + } + + EditorUtility.ClearProgressBar(); + } + + // 检查大图资源 + [MenuItem("Tools/检查并移动Atlas中过大的资源")] + private static void FindBigAtlas() + { + List assetPaths = new List(); + assetPaths.Add("Assets/ManagedResources/Atlas"); + + string[] allPath = AssetDatabase.FindAssets("t:Texture", assetPaths.ToArray()); + for (int i = 0; i < allPath.Length; i++) + { + EditorUtility.DisplayCancelableProgressBar("正在查找", allPath[i], (float)i / allPath.Length); + + string path = AssetDatabase.GUIDToAssetPath(allPath[i]); + var tex = AssetDatabase.LoadAssetAtPath(path); + if(tex.width >= 512 || tex.height > 512) + { + Debug.LogError(path); + string[] depArray = path.Split('/'); + string resName = depArray[depArray.Length - 1]; + AssetDatabase.MoveAsset(path, "Assets/ManagedResources/DynamicAtlas/"+ resName); + } + } + EditorUtility.ClearProgressBar(); + } + }