Unity模糊查找MonoBehaviour
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
namespace Assets.Editor
{
class FindMonoBehaviour : ScriptableWizard
{
public string text;
[MenuItem("Temp/Find MonoBehaviour")]
public static void CreateAssetBundle()
{
var findMonoBehaviour = DisplayWizard<FindMonoBehaviour>("Find MonoBehaviour", "Create");
}
public void OnWizardCreate()
{
string currentScenePath = EditorApplication.currentScene;
if (!string.IsNullOrEmpty(currentScenePath))
EditorApplication.OpenScene(currentScenePath);
else
return;
foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject)))
{
if (obj.transform.parent == null)
{
FindScripts(obj, obj.name);
TraceGameObject(obj, obj.name);
}
}
}
public void FindScripts(GameObject obj, String gameObjPath)
{
List<MonoBehaviour> scripts = new List<MonoBehaviour>();
obj.GetComponents<MonoBehaviour>(scripts);
foreach (MonoBehaviour script in scripts)
{
if (script == null)
continue;
if (script.GetType().ToString().Contains(text))
Debug.Log("Script=" + script.GetType() + " GameObject=" + gameObjPath);
}
}
public void TraceGameObject(GameObject obj, String prefix)
{
List<Transform> trs = new List<Transform>();
obj.GetComponentsInChildren<Transform>(true, trs);
foreach (Transform tr in trs)
{
if (tr.parent == obj.transform)
{
String temp = prefix + "/" + tr.gameObject.name;
//Debug.Log(temp);
FindScripts(tr.gameObject, temp);
TraceGameObject(tr.gameObject, temp);
}
}
}
}
}