Unity-Find-Script-References 查找脚本的引用
生活随笔
收集整理的這篇文章主要介紹了
Unity-Find-Script-References 查找脚本的引用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
背景:
有時候我們需要找出項目中所有的引用到某個腳本的地方(比如Prefabs/Scene GameObjects等)。當項目比較大時,手工尋找還是非常費時費力的事情。本文嘗試通過插件自動搜索。
分析:
基本的思路是:首先篩選出項目中全部Prefab,加載每個Prefab并判斷是否有掛載目標腳本,然后載入每個場景,判斷場景中每個物體是否有掛載目標腳本,最后列出結果。
實現:
1,在右鍵菜單項中添加菜單:
新建一個類,命名為 FindScriptRef ,并繼承自 EditorWindow 。添加如下方法:
[MenuItem(“Assets/Find All Reference”)] public static void ShowWindow() { //Show existing window instance. If one doesn’t exist, make one. EditorWindow.GetWindow(typeof(FindScriptRef)); }
這段代碼會在菜單中添加一個名為“Find All Reference”的菜單項。選中菜單項會打開一個FindScriptRef窗口實例。當然此時窗口中沒有任何內容。
2,窗口基本顯示邏輯:
void OnGUI() { if (Selection.activeObject == null) { GUILayout.Label(“select a script file from Project Window.”); return; } //判斷選中項是否為腳本 var name = Selection.activeObject.name; System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); var dict = System.IO.Path.GetDirectoryName(assembly.Location); assembly = System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(dict, "Assembly-CSharp.dll")); var selectType = assembly.GetType(name); if (string.IsNullOrEmpty(name) || selectType == null) {GUILayout.Label("select a script file from Project Window.");return; }GUILayout.BeginVertical(); GUILayout.BeginHorizontal();//列出腳本名稱和“Find”按鈕 GUILayout.Label(name); bool click = GUILayout.Button("Find"); GUILayout.EndHorizontal(); GUILayout.Space(10);//列出搜索結果 if (findResult != null && findResult.Count > 0) {GUILayout.BeginScrollView(Vector2.zero, GUIStyle.none);foreach (string path in findResult){GUILayout.Label(path);}GUILayout.EndScrollView(); }if (click) {Find(selectType); } GUILayout.EndVertical();
}
然后,實現Find方法,搜索指定Type的全部引用:
void Find(System.Type type){ //step 1:find ref in assets//filter all GameObject from assets(so-called 'Prefab') var guids = AssetDatabase.FindAssets("t:GameObject");findResult = new List<string>();var tp = typeof(GameObject);foreach (var guid in guids) {var path = AssetDatabase.GUIDToAssetPath(guid);//load Prefabvar obj = AssetDatabase.LoadAssetAtPath(path, tp) as GameObject;//check whether prefab contains script with type 'type'if (obj != null){var cmp = obj.GetComponent(type);if (cmp == null){cmp = obj.GetComponentInChildren(type);}if (cmp != null){findResult.Add(path);}} }//step 2: find ref in scenes//save current scene string curScene = EditorApplication.currentScene; EditorApplication.SaveScene();//find all scenes from dataPath string[] scenes = Directory.GetFiles(Application.dataPath, "*.unity", SearchOption.AllDirectories);//iterates all scenes foreach (var scene in scenes) {EditorApplication.OpenScene(scene);//iterates all gameObjectsforeach (GameObject obj in FindObjectsOfType<GameObject>()){var cmp = obj.GetComponent(type);if (cmp == null){cmp = obj.GetComponentInChildren(type);}if (cmp != null){findResult.Add(scene.Substring(Application.dataPath.Length) + "Assets:" + obj.name);}} }//reopen current scene EditorApplication.OpenScene(curScene); Debug.Log ("finish");
}
此時切換到Unity,在Project窗口選中一個scrpit,右鍵選擇『Find All Reference』,在打開的窗口選擇『Find』按鈕,即可看到下面列出了所有引用了這個腳本的位置(如果項目過于龐大,可能需要等待一會兒)。
代碼下載:[FindScriptRef.cs]({{ site.url }}/files/FindScriptRef.cs “FindScriptRef.cs”)
總結
以上是生活随笔為你收集整理的Unity-Find-Script-References 查找脚本的引用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 农业银行信用卡附属卡怎么办理?申请条件有
- 下一篇: 浦发银行信用卡激活有效期是多久?不激活有