Unity 查找所有带有某脚本的预制体

在Editor下创建脚本写下:(我的所有预制体都放在 Assets/Prefabs(包含文件夹)里了)
 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEditor;
 4 using UnityEngine;
 5 using System.Threading;
 6 using System;
 7 using System.IO;
 8 
 9 public class Shortcuts : EditorWindow
10 {
11     static Shortcuts window;
12 
13     public string fullPath = "Assets/Prefabs/";
14     MonoScript scriptObj = null;
15     int loopCount = 0;
16     List<Transform> results = new List<Transform>();
17 
18     [MenuItem("Shortcuts/Test")]
19     static void Execute()
20     {
21         if (window == null)
22         {
23             window = (Shortcuts)GetWindow(typeof(Shortcuts));
24         }
25         window.Show();
26     }
27     private void OnGUI()
28     {
29         GUILayout.Label("查找所有带此脚本的预制体。拖入脚本:");
30         scriptObj = (MonoScript)EditorGUILayout.ObjectField(scriptObj, typeof(MonoScript), true);
31         if (GUILayout.Button("Find"))
32         {
33             results.Clear();
34             loopCount = 0;
35             Debug.Log("开始查找.");
36             FindScript();
37         }
38     }
39     void FindScript()
40     {
41         List<string> prefabs_names = new List<string>();
42 
43         //获取指定路径下面的所有资源文件
44         if (Directory.Exists(fullPath))
45         {
46             DirectoryInfo direction = new DirectoryInfo(fullPath);
47             FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
48             for (int i = 0; i < files.Length; i++)
49             {
50                 if (files[i].Name.EndsWith(".prefab"))
51                 {
52                     string ab_name = "ui/";
53                     if(files[i].Directory.Name != "ui")
54                     {
55                         ab_name += files[i].Directory.Name + "/";
56                     }
57                     prefabs_names.Add(ab_name + files[i].Name.Split('.')[0]);
58                 }
59             }
60         }
61 
62         if (scriptObj != null)
63         {
64             for (int i = 0; i < prefabs_names.Count; i++)
65             {
66                 loopCount++;
67                 //Debug.Log("第" + loopCount + "个预制: " + prefabs_names[i]);
68 
69                 string[] str1 = AssetDatabase.GetAssetPathsFromAssetBundle(prefabs_names[i]);
70                 if (str1.Length < 1)
71                 {
72                     //Debug.LogError("Null Prefab AssetBundle: " + name);
73                     continue;
74                 }
75                 GameObject go = AssetDatabase.LoadAssetAtPath(str1[0], typeof(System.Object)) as GameObject;
76                 if (go != null)
77                 {
78                     foreach (Transform _child in go.transform)
79                     {
80                         if (_child.GetComponent(scriptObj.GetClass()) != null)
81                         {
82                             if (!results.Contains(go.transform))
83                             {
84                                 results.Add(go.transform);
85                             }
86                             Debug.Log("<color=darkblue>Find it: " + go.name + "/" + _child.name + "</color>");
87                         }
88                     }
89                 }
90             }
91         }
92         if(results.Count <= 0)
93         {
94             Debug.LogError("Oops: Cant find that you want !");
95         }
96     }
97 }

 

 

posted @ 2018-10-26 17:51  huanp  阅读(4978)  评论(0编辑  收藏  举报