mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
improve SearchUtilities.Find (#341)
writes it as a 3-strategy process, since that's effectively what it is. makes it so that not finding the root wont immediately return null
This commit is contained in:
commit
a1db1bc5d2
@ -95,38 +95,41 @@ namespace NewHorizons.Utility
|
|||||||
{
|
{
|
||||||
if (CachedGameObjects.TryGetValue(path, out var go)) return go;
|
if (CachedGameObjects.TryGetValue(path, out var go)) return go;
|
||||||
|
|
||||||
|
// 1: normal find
|
||||||
go = GameObject.Find(path);
|
go = GameObject.Find(path);
|
||||||
if (go == null)
|
if (go)
|
||||||
{
|
{
|
||||||
// find inactive use root + transform.find
|
CachedGameObjects.Add(path, go);
|
||||||
var names = path.Split('/');
|
return go;
|
||||||
var rootName = names[0];
|
|
||||||
var root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == rootName);
|
|
||||||
if (root == null)
|
|
||||||
{
|
|
||||||
if (warn) Logger.LogWarning($"Couldn't find root object in path {path}");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var childPath = string.Join("/", names.Skip(1));
|
|
||||||
go = root.FindChild(childPath);
|
|
||||||
if (go == null)
|
|
||||||
{
|
|
||||||
var name = names.Last();
|
|
||||||
if (warn) Logger.LogWarning($"Couldn't find object in path {path}, will look for potential matches for name {name}");
|
|
||||||
// find resource to include inactive objects
|
|
||||||
// also includes prefabs but hopefully thats okay
|
|
||||||
go = FindResourceOfTypeAndName<GameObject>(name);
|
|
||||||
if (go == null)
|
|
||||||
{
|
|
||||||
if (warn) Logger.LogWarning($"Couldn't find object with name {name}");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CachedGameObjects.Add(path, go);
|
// 2: find inactive using root + transform.find
|
||||||
return go;
|
var names = path.Split('/');
|
||||||
|
|
||||||
|
var rootName = names[0];
|
||||||
|
var root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == rootName);
|
||||||
|
|
||||||
|
var childPath = string.Join("/", names.Skip(1));
|
||||||
|
go = root ? root.FindChild(childPath) : null;
|
||||||
|
if (go)
|
||||||
|
{
|
||||||
|
CachedGameObjects.Add(path, go);
|
||||||
|
return go;
|
||||||
|
}
|
||||||
|
|
||||||
|
var name = names.Last();
|
||||||
|
if (warn) Logger.LogWarning($"Couldn't find object in path {path}, will look for potential matches for name {name}");
|
||||||
|
// 3: find resource to include inactive objects
|
||||||
|
// also includes prefabs but hopefully thats okay
|
||||||
|
go = FindResourceOfTypeAndName<GameObject>(name);
|
||||||
|
if (go)
|
||||||
|
{
|
||||||
|
CachedGameObjects.Add(path, go);
|
||||||
|
return go;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (warn) Logger.LogWarning($"Couldn't find object with name {name}");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<GameObject> GetAllChildren(this GameObject parent)
|
public static List<GameObject> GetAllChildren(this GameObject parent)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user