[轉]unity如何查詢某個指令碼掛在了哪些物體上
在開發中往往會遇到一個問題:不知道整個場景中究竟有哪些物體掛載了某一個指令碼。如果挨個查詢太麻煩了,下面有一種方法可以快速找到解決這個問題。
在unity的Window裡有一項Editor tests runner 選擇這個會出現一個視窗:如下圖:
然後點選建立指令碼會有指令碼自動建立在project裡的Editor下。之後我們要寫兩個指令碼(如下圖)
這兩個指令碼程式碼,一個是用來盛放要被找的那些物體另個是盛放你要來查詢被物體掛載的指令碼:
盛放物體的程式碼:
-
using UnityEngine;
-
using UnityEditor;
-
public class FindMissingScriptsRecursively : EditorWindow
-
{
-
static int go_count = 0, components_count = 0, missing_count = 0;
-
[MenuItem("Window/FindMissingScriptsRecursively")]
-
public static void ShowWindow()
-
{
-
EditorWindow.GetWindow(typeof(FindMissingScriptsRecursively));
-
}
-
public void OnGUI()
-
{
-
if (GUILayout.Button("Find Missing Scripts in selected GameObjects"))
-
{
-
FindInSelected();
-
}
-
}
-
private static void FindInSelected()
-
{
-
GameObject[] go = Selection.gameObjects;
-
go_count = 0;
-
components_count = 0;
-
missing_count = 0;
-
foreach (GameObject g in go)
-
{
-
FindInGO(g);
-
}
-
Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count));
-
}
-
private static void FindInGO(GameObject g)
-
{
-
go_count++;
-
Component[] components = g.GetComponents<Component>();
-
for (int i = 0; i < components.Length; i++)
-
{
-
components_count++;
-
if (components[i] == null)
-
{
-
missing_count++;
-
string s = g.name;
-
Transform t = g.transform;
-
while (t.parent != null)
-
{
-
s = t.parent.name + "/" + s;
-
t = t.parent;
-
}
-
Debug.Log(s + " has an empty script attached in position: " + i, g);
-
}
-
}
-
// Now recurse through each child GO (if there are any):
-
foreach (Transform childT in g.transform)
-
{
-
//Debug.Log("Searching " + childT.name + " " );
-
FindInGO(childT.gameObject);
-
}
-
}
-
}
盛放指令碼的程式碼:
-
using UnityEngine;
-
using System.Collections;
-
using System.Collections.Generic;
-
using UnityEditor;
-
/////////////////////////////////////////////////////////////////////////////
-
//查詢節點及所有子節點中,是否有指定的指令碼元件
-
/////////////////////////////////////////////////////////////////////////////
-
public class MonoFinder : EditorWindow
-
{
-
Transform root = null;
-
MonoScript scriptObj = null;
-
int loopCount = 0;
-
List<Transform> results = new List<Transform>();
-
[MenuItem("Level4/Finder/MonoFinder")]
-
static void Init()
-
{
-
EditorWindow.GetWindow(typeof(MonoFinder));
-
}
-
void OnGUI()
-
{
-
GUILayout.Label("節點:");
-
root = (Transform)EditorGUILayout.ObjectField(root, typeof(Transform), true);
-
GUILayout.Label("指令碼型別:");
-
scriptObj = (MonoScript)EditorGUILayout.ObjectField(scriptObj, typeof(MonoScript), true);
-
if (GUILayout.Button("Find"))
-
{
-
results.Clear();
-
loopCount = 0;
-
Debug.Log("開始查詢.");
-
FindScript(root);
-
}
-
if (results.Count > 0)
-
{
-
foreach (Transform t in results)
-
{
-
EditorGUILayout.ObjectField(t, typeof(Transform), false);
-
}
-
}
-
else
-
{
-
GUILayout.Label("無資料");
-
}
-
}
-
void FindScript(Transform root)
-
{
-
if (root != null && scriptObj != null)
-
{
-
loopCount++;
-
Debug.Log(".." + loopCount + ":" + root.gameObject.name);
-
if (root.GetComponent(scriptObj.GetClass()) != null)
-
{
-
results.Add(root);
-
}
-
foreach (Transform t in root)
-
{
-
FindScript(t);
-
}
-
}
-
}
-
}
有了這兩個指令碼,會發現unity的選單裡會多出一個level4(如圖)的選項,然後點選它,會出現一個彈窗。上面那個節點(如圖)就是盛放物體的,下面那個指令碼型別(如圖)就是放指令碼的。
例如下面的案例中,我要查詢一個名字叫AsyncImageDownloader的指令碼在panoramic這個物體裡有多少被掛載了。直接把對應的東西拖進去,然後點選find就會發現在userhead_portrait這個子物體裡有這個指令碼。
這種方法可以找到所有父物體下的子物體中所有的掛載。它會遍歷整個父物體中個的子物體;
從下圖中可以看出在panoramic這個父物體中有575個子物體,它們都被查找了一遍。可見剛才查到只有userhead_portrait上掛載了剛才要找的指令碼。