unity中如果你要确认一堆节点中,哪个节点有你指定的脚本,你该怎么办?
手动一个个的查看...
现在整理一个工具,来自动找出这些用于该脚本的节点.
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);
}
}
}
}
不要忘记,需要放到 Assets/Editor/下!