今天给大家用Unity编辑器实现一个查询NGUI图集Atlas在资源中的引用关系
工具图:
使用方法:
一、先修改下面脚本14行,指定好图集所在的目录
二、点击“Tools/查找NGUI图集引用”,打开面板
三、点击选择图集,或者将指定图集拖在UIAtlas位置
四、在“Hierarchy”或“Project”面板选择要查找的目标预设体(可以多选)
五、点击查找引用,就会显示出选择的预设体有哪些UI使用了选择的图集了
上脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEngine; public class FindAtlasUseUtil : EditorWindow { UIAtlas atlas; Transform[] selectTrans; List<UISprite> spriteList; Vector2 scrollPosition; bool selectedAtlas = false; List<UIAtlas> atlasList; string[] atlasPath = new string[] { "Assets/Arts/Atlas", "Assets/Resources/Atlas" }; // TODO 指定项目里图集的父节点 需修改 static FindAtlasUseUtil window; [MenuItem("Tools/查找NGUI图集引用")] static void CreateWindow() { window = (FindAtlasUseUtil)EditorWindow.GetWindow(typeof(FindAtlasUseUtil), false, "查找图集引用"); window.Show(); } private void OnEnable() { spriteList = new List<UISprite>(); } private void OnGUI() { EditorGUILayout.Space(); EditorGUILayout.Space(); atlas = EditorGUILayout.ObjectField("需要查询的图集:", atlas, typeof(UIAtlas)) as UIAtlas; if (GUILayout.Button("选择图集")) { ChooseAtlas(); } EditorGUILayout.Space(); EditorGUILayout.Space(); GUILayout.Label("可多选“Hierarchy”、“Project”面板的预设体"); if (GUILayout.Button("查找引用")) { FindUse(); } EditorGUILayout.Space(); EditorGUILayout.Space(); if (selectedAtlas) { ShowChooseAtlas(); } if (spriteList.Count > 0 && atlas != null && !selectedAtlas) { ShowFindUse(); } } void ChooseAtlas() { selectedAtlas = true; if (atlasList == null) { atlasList = new List<UIAtlas>(); // 第二个参数为图集的目录位置数组,可以指定项目里存放图集的父节点。切记不要在Assets节点下查找,这样会遍历所有物体,会很卡的~~ string[] guids = AssetDatabase.FindAssets("t:GameObject", atlasPath); List<string> paths = new List<string>(); guids.ToList().ForEach(m => paths.Add(AssetDatabase.GUIDToAssetPath(m))); paths.ForEach(p => atlasList.Add(AssetDatabase.LoadAssetAtPath(p, typeof(UIAtlas)) as UIAtlas)); // 移除Null值 for (int i = 0; i < atlasList.Count; i++) { if (i < atlasList.Count && atlasList[i] == null) { atlasList.Remove(atlasList[i]); i--; } } } } void FindUse() { selectTrans = Selection.GetTransforms(SelectionMode.TopLevel); spriteList = new List<UISprite>(); for (int i = 0; i < selectTrans.Length; i++) { UISprite[] sprites = selectTrans[i].GetComponentsInChildren<UISprite>(true); for (int j = 0; j < sprites.Length; j++) { if (sprites[j] != null && sprites[j].atlas == atlas) { spriteList.Add(sprites[j]); } } } } void ShowChooseAtlas() { scrollPosition = GUI.BeginScrollView(new Rect(0, 150, Screen.width, 500), scrollPosition, new Rect(0, 150, Screen.width, atlasList.Count * 20)); for (int i = 0; i < atlasList.Count; i++) { atlasList[i] = EditorGUI.ObjectField(new Rect(50, 150 + i * 20, Screen.width, 20), name, atlasList[i], typeof(UIAtlas)) as UIAtlas; if (GUI.Button(new Rect(5, 150 + i * 20, 40, 20), "选择")) { atlas = atlasList[i]; selectedAtlas = false; } } GUI.EndScrollView(); } void ShowFindUse() { scrollPosition = GUI.BeginScrollView(new Rect(0, 150, Screen.width, 500), scrollPosition, new Rect(0, 150, Screen.width, spriteList.Count * 20)); for (int i = 0; i < spriteList.Count; i++) { string name = spriteList[i].atlas != null ? spriteList[i].atlas.name : "空图集"; name += " " + spriteList[i].spriteName; spriteList[i] = EditorGUI.ObjectField(new Rect(0, 150 + i * 20, Screen.width, 20), name, spriteList[i], typeof(UISprite)) as UISprite; } GUI.EndScrollView(); } } |
- 本文固定链接: http://www.u3d8.com/?p=1289
- 转载请注明: 网虫虫 在 u3d8.com 发表过