using UnityEngine;
using System.Collections.Generic;
using Object = UnityEngine.Object;
using System.Reflection;
using UnityEditor;
using System.Linq;
public class SearchableWindow : EditorWindow
{
string searchString = "";
static List<GameObject> gameobjecttest = new List<GameObject>();
Vector2 scrollPos;
[MenuItem("Tools/Searching")]
private static void CreateReplaceWithPrefab()
{
const int width = 340;
const int height = 420;
var x = (Screen.currentResolution.width - width) / 2;
var y = (Screen.currentResolution.height - height) / 2;
GetWindow<SearchableWindow>().position = new Rect(x, y, width, height);
}
private void OnGUI()
{
EditorGUILayout.BeginHorizontal(EditorStyles.toolbar);
GUILayout.FlexibleSpace();
searchString = EditorGUILayout.TextField(searchString, EditorStyles.toolbarTextField);
EditorGUILayout.EndHorizontal();
if (GUILayout.Button("Search"))
{
var items = Selection.gameObjects;
// Do comparison here. For example
var selected = GetChildrenRecursive(items);
gameobjecttest.AddRange(selected);
}
EditorGUILayout.BeginHorizontal();
scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(300), GUILayout.Height(400));
foreach (GameObject go in gameobjecttest)
{
EditorGUILayout.LabelField(go.name);
}
EditorGUILayout.EndScrollView();
EditorGUILayout.EndHorizontal();
}
private void OnSelectionChange()
{
Repaint();
}
private static IEnumerable<GameObject> GetChildrenRecursive(GameObject root)
{
var output = new List<GameObject>();
//add the root object itself
output.Add(root);
// iterate over direct children
foreach (Transform child in root.transform)
{
// add the children themselves
output.Add(child.gameObject);
var childsOfchild = GetChildrenRecursive(child.gameObject);
output.AddRange(childsOfchild);
}
return output;
}
private static IEnumerable<GameObject> GetChildrenRecursive(IEnumerable<GameObject> rootObjects)
{
var output = new List<GameObject>();
foreach (var root in rootObjects)
{
output.AddRange(GetChildrenRecursive(root));
}
// remove any duplicates that would e.g. appear if you select a parent and its child
return output.Distinct();
}
}
我要确保在单击按钮时会进行搜索,并且一旦将项目添加到EditorGUILayout.LabelField,但如果它位于if (GUILayout.Button("Search"))
内,则需要将它添加到OnGUI内不会将项目添加到EditorGUILayout.LabelField
现在,它会继续向EditorGUILayout.LabelField添加不间断。
答案 0 :(得分:1)
您的问题有点不清楚。
如果您的问题仅在于是否可能:
否!(至少不是您的期望)
此区块中的任何内容:
if (GUILayout.Button("Search"))
{
...
}
仅在实际按下按钮的那一刻执行。
因此,您必须按照已经执行的方式进行操作。如果列表不为空,也许添加仅显示这些字段的附加检查:
// only show the button while the list is empty
if(gameobjecttest.Count == 0)
{
if (GUILayout.Button("Search"))
{
...
}
// skip the rest
return;
}
// otherwise show the list
EditorGUILayout.BeginHorizontal();
{
scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(300), GUILayout.Height(400));
{
foreach (GameObject go in gameobjecttest)
{
EditorGUILayout.LabelField(go.name);
}
}
EditorGUILayout.EndScrollView();
}
EditorGUILayout.EndHorizontal();
(我通常会添加{
}
来清理代码)
或保留该按钮但将其禁用
EditorGUI.BeginDisabledGroup(gameobjecttest.Count != 0);
{
if (GUILayout.Button("Search"))
{
...
}
}
EditorGUI.EndDisabledGroup();