2using System.Collections.Generic;
4using System.Reflection;
15 private List<GameObject> _cachedRootGameObjects;
16 private List<GameObject> _searchResults;
20 return Resources.FindObjectsOfTypeAll<Transform>()
21 .Where(t => t.parent ==
null)
22 .Select(x => x.gameObject);
27 if (_cachedRootGameObjects !=
null)
29 _cachedRootGameObjects.RemoveAll(o => o ==
null);
30 return _cachedRootGameObjects;
32 return Enumerable.Empty<GameObject>();
37 if (_searchResults !=
null)
39 _searchResults.RemoveAll(o => o ==
null);
40 return _searchResults;
45 public void Refresh(
bool full, Predicate<GameObject> objectFilter)
47 if (_searchResults !=
null)
50 if (_cachedRootGameObjects ==
null || full)
52 _cachedRootGameObjects =
FindAllRootGameObjects().OrderBy(x => x.name, StringComparer.InvariantCultureIgnoreCase).ToList();
57 _cachedRootGameObjects.RemoveAll(o => o ==
null);
60 if (UnityFeatureHelper.SupportsScenes && !full)
62 var newItems = UnityFeatureHelper.GetSceneGameObjects().Except(_cachedRootGameObjects).ToList();
63 if (newItems.Count > 0)
65 _cachedRootGameObjects.AddRange(newItems);
66 _cachedRootGameObjects.Sort((o1, o2) =>
string.Compare(o1.name, o2.name, StringComparison.InvariantCultureIgnoreCase));
70 if (objectFilter !=
null)
71 _cachedRootGameObjects.RemoveAll(objectFilter);
74 public void Search(
string searchString,
bool searchProperties)
76 if (
string.IsNullOrEmpty(searchString))
77 _searchResults =
null;
81 .SelectMany(x => x.GetComponentsInChildren<Transform>(
true))
82 .Where(x => x.name.Contains(searchString, StringComparison.InvariantCultureIgnoreCase) || x.GetComponents<
Component>().Any(c =>
SearchInComponent(searchString, c, searchProperties)))
83 .OrderBy(x => x.name, StringComparer.InvariantCultureIgnoreCase)
84 .Select(x => x.gameObject)
91 if (c ==
null)
return false;
93 var type = c.GetType();
94 if (type.Name.Contains(searchString, StringComparison.InvariantCultureIgnoreCase))
97 if (!searchProperties)
100 var nameBlacklist =
new[] {
"parent",
"parentInternal",
"root",
"transform",
"gameObject"};
101 var typeBlacklist =
new[] {typeof(
bool)};
103 foreach (var prop
in type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
104 .Where(x => x.CanRead && !nameBlacklist.Contains(x.Name) && !typeBlacklist.Contains(x.PropertyType)))
108 if (prop.GetValue(c,
null).ToString().Contains(searchString, StringComparison.InvariantCultureIgnoreCase))
113 foreach (var field
in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
114 .Where(x => !nameBlacklist.Contains(x.Name) && !typeBlacklist.Contains(x.FieldType)))
118 if (field.GetValue(c).ToString().Contains(searchString, StringComparison.InvariantCultureIgnoreCase))
UnityEngine.Component Component
Keeps track of root gameobjects and allows searching objects in the scene
void Search(string searchString, bool searchProperties)
void Refresh(bool full, Predicate< GameObject > objectFilter)
static IEnumerable< GameObject > FindAllRootGameObjects()
IEnumerable< GameObject > GetSearchedOrAllObjects()
static bool SearchInComponent(string searchString, Component c, bool searchProperties)
IEnumerable< GameObject > GetRootObjects()