using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ComponentsManager : EditorWindow
{
[MenuItem("Tools/Components Manager")]
static void CreateAndShow()
{
EditorWindow window = EditorWindow.GetWindow<ComponentsManager>("Components Manager");
window.Show();
}
private void OnSelectionChange()
{
Repaint();
}
void OnGUI()
{
SetSearchFilter("Base", 1);
}
public const int FILTERMODE_ALL = 0;
public const int FILTERMODE_NAME = 1;
public const int FILTERMODE_TYPE = 2;
public static SearchableEditorWindow hierarchy { get; private set; }
public static void SetSearchFilter(string filter, int filterMode)
{
SearchableEditorWindow[] windows = (SearchableEditorWindow[])Resources.FindObjectsOfTypeAll(typeof(SearchableEditorWindow));
foreach (SearchableEditorWindow window in windows)
{
if (window.GetType().ToString() == "UnityEditor.SceneHierarchyWindow")
{
hierarchy = window;
break;
}
}
if (hierarchy == null)
return;
MethodInfo setSearchType = typeof(SearchableEditorWindow).GetMethod("SetSearchFilter", BindingFlags.NonPublic | BindingFlags.Instance);
object[] parameters = new object[] { filter, filterMode, false };
setSearchType.Invoke(hierarchy, parameters);
}
void DrawName(Transform transform)
{
GUIStyle style = EditorStyles.label;
if (transform == Selection.activeGameObject.transform)
{
style = new GUIStyle(style);
style.normal.textColor = Color.blue;
}
Rect rect = EditorGUILayout.GetControlRect();
rect = EditorGUI.IndentedRect(rect);
if (GUI.Button(rect, transform.name, style))
{
Selection.activeTransform = transform;
}
}
void RecurseChildren(Transform parent)
{
EditorGUI.indentLevel++;
foreach (Transform childTransform in parent)
{
DrawName(childTransform);
if (childTransform.childCount > 0)
RecurseChildren(childTransform);
}
EditorGUI.indentLevel--;
}
}
现在我通过在OnGUI中调用方法SetSearchFilter来使用
:SetSearchFilter("Base", 1);
但是要输入脚本的名称,例如“ Base”或“ Main Camera”,我希望当我在编辑器“层次结构”过滤器框中键入时,它将是方法中的名称。这是有关层次过滤器搜索框的屏幕快照的链接:
标有蓝色椭圆的层次过滤器。
我在此处输入的内容想实时传递到SetSearchFilter("Base", 1);
。而是使用“基本”从层次结构过滤器获取字符串。