Reputation: 11335
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();
}
}
I want to make that when clicking the button it will make the search and will add once the items to the EditorGUILayout.LabelField but the EditorGUILayout.LabelField need ot be inside the OnGUI if it's inside the if (GUILayout.Button("Search"))
it will not add the items to the EditorGUILayout.LabelField
Now it keep adding nonstop to the EditorGUILayout.LabelField.
Upvotes: 0
Views: 826
Reputation: 90813
Your question is a bit unclear.
If your question is only whether it is possible or not:
No! (at least not how you expect it)
anything within this block:
if (GUILayout.Button("Search"))
{
...
}
is only executed exactly the moment, the button is actually pressed.
So you have to do it the way you did it already. Maybe adding an additional check for only showing those fields if the list is not empty:
// 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();
(I usually add those {
}
for cleaning up the code a bit)
Or keep the button but disable it instead
EditorGUI.BeginDisabledGroup(gameobjecttest.Count != 0);
{
if (GUILayout.Button("Search"))
{
...
}
}
EditorGUI.EndDisabledGroup();
Upvotes: 1