├── README.md └── FindUnUnUsedUITexture.cs /README.md: -------------------------------------------------------------------------------- 1 | # FindUnUsedUITexture 2 | Unity Script To Find Unused UI Texture which include tag , png and jpg 3 | 4 | #usage: 5 | 1.Copy FindUnUsedUITexture.cs to you editor directory. 6 | 7 | 2.Click the menu item "Tools/UI冗余图片扫描". 8 | 9 | 3.Any unused ui texutre will be list in Assets/unsedpic.txt 10 | -------------------------------------------------------------------------------- /FindUnUnUsedUITexture.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEditor; 3 | 4 | using System.IO; 5 | using System.Linq; 6 | using System.Text.RegularExpressions; 7 | using System.Collections.Generic; 8 | using System.Text; 9 | using System; 10 | 11 | public static class LinqHelper { 12 | public static TSource Fold(this IEnumerable source, Func func, TSource id) 13 | { 14 | TSource r = id; 15 | foreach (var s in source) 16 | { 17 | r = func(r, s); 18 | } 19 | return r; 20 | } 21 | public static void ForEach(this IEnumerable source, Action action) 22 | { 23 | foreach (T element in source) 24 | action(element); 25 | } 26 | public static IEnumerable SelectI(this IEnumerable source, Func action) 27 | { 28 | int i = 0; 29 | foreach (var s in source) 30 | { 31 | yield return action(s, i); 32 | i += 1; 33 | } 34 | } 35 | public static TSource Reduce(this IEnumerable source, Func func) where TSource : new() 36 | { 37 | return Fold(source, func, new TSource()); 38 | } 39 | public static void ForEachI(this IEnumerable source, Action action) 40 | { 41 | int i = 0; 42 | foreach (T element in source) 43 | { 44 | action(element, i); 45 | i += 1; 46 | } 47 | 48 | } 49 | } 50 | public static class FindUnUnUsedUITexture 51 | { 52 | 53 | 54 | static List getUUIDsInFile(string path) 55 | { 56 | StreamReader file = new StreamReader(path); 57 | List uuids = new List(); 58 | string line; 59 | while ((line = file.ReadLine()) != null) 60 | { 61 | var reg = new Regex(@"([a-f0-9]{32})"); 62 | var m = reg.Match(line); 63 | if (m.Success) 64 | { 65 | uuids.Add(m.Groups[0].Value); 66 | } 67 | } 68 | file.Close(); 69 | return uuids; 70 | } 71 | // Use this for initialization 72 | [MenuItem("Tools/UI冗余图片扫描")] 73 | public static void Scan() 74 | { 75 | 76 | var uiPrefabRootDir = EditorUtility.OpenFolderPanel("选择UIPrefab目录", "Assets",""); 77 | if (string.IsNullOrEmpty(uiPrefabRootDir)) 78 | { 79 | return; 80 | } 81 | 82 | var uiPicRootDir = EditorUtility.OpenFolderPanel("选择UIPrefab目录", "Assets", ""); 83 | if (string.IsNullOrEmpty(uiPicRootDir)) 84 | { 85 | return; 86 | } 87 | 88 | //find all meta and pic path 89 | var uuidReg = new Regex(@"guid: ([a-f0-9]{32})"); 90 | var pngs = Directory.GetFiles(uiPicRootDir, "*.meta", SearchOption.AllDirectories) 91 | .Select(p => "Assets/" + p.Replace('\\','/').Substring(Application.dataPath.Length+1)) 92 | .Where(p => 93 | { 94 | return p.EndsWith(".png.meta") || p.EndsWith(".jpg.meta") || p.EndsWith(".tag.meta"); 95 | }).ToList(); 96 | var uuid2path = new Dictionary(); 97 | pngs.ForEachI((png, i) => 98 | { 99 | var matcher = uuidReg.Match(File.ReadAllText(png)); 100 | var uuid = matcher.Groups[1].Value; 101 | if (uuid2path.ContainsKey(uuid)) 102 | { 103 | Debug.LogError("uuid dup" + uuid + " \n" + png + "\n" + uuid2path[uuid]); 104 | } 105 | else 106 | { 107 | uuid2path.Add(uuid, png.Substring(0,png.Length-5)); 108 | } 109 | EditorUtility.DisplayProgressBar("扫描图片中", png, (float)i / pngs.Count); 110 | 111 | }); 112 | 113 | //find all prefab and search pic uuid 114 | var prefabs = Directory.GetFiles(uiPrefabRootDir, "*.prefab", SearchOption.AllDirectories); 115 | var anims = Directory.GetFiles("Assets/", "*.anim", SearchOption.AllDirectories).Where(p => !p.Replace('\\', '/').Contains("Characters/")); 116 | var allFiles = prefabs.Concat(anims).ToList(); 117 | var alluuids = allFiles 118 | .SelectI((f, i) => { 119 | EditorUtility.DisplayProgressBar("获取引用关系", f, (float)i / allFiles.Count); 120 | return getUUIDsInFile(f); 121 | }).ToList().Aggregate((a, b) => a.Concat(b).ToList()).ToList(); 122 | EditorUtility.ClearProgressBar(); 123 | //rm used pic uuid 124 | var uuidshashset = new HashSet(alluuids); 125 | var em = uuidshashset.GetEnumerator(); 126 | while(em.MoveNext()) 127 | { 128 | var uuid = em.Current; 129 | uuid2path.Remove(uuid); 130 | } 131 | 132 | StringBuilder sb = new StringBuilder(); 133 | sb.Append("UnUsedFiles: "); 134 | sb.Append(uuid2path.Count); 135 | sb.Append("\n"); 136 | uuid2path.ForEach(kv => sb.Append(kv.Value +"\n")); 137 | 138 | File.WriteAllText("Assets/unusedpic.txt", sb.ToString()); 139 | EditorUtility.DisplayDialog("扫描成功", string.Format("共找到{0}个冗余图片\n请在Assets/unsedpic.txt查看结果",uuid2path.Count), "ok"); 140 | } 141 | } 142 | --------------------------------------------------------------------------------