From a1ba3a6beeae22c340f63cb613150e7bc986ded1 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Thu, 1 Sep 2022 21:17:31 -0700 Subject: [PATCH] improve SearchUtilities.Find --- NewHorizons/Utility/SearchUtilities.cs | 59 ++++++++++++++------------ 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/NewHorizons/Utility/SearchUtilities.cs b/NewHorizons/Utility/SearchUtilities.cs index 970c33d4..6f4d1be5 100644 --- a/NewHorizons/Utility/SearchUtilities.cs +++ b/NewHorizons/Utility/SearchUtilities.cs @@ -95,38 +95,41 @@ namespace NewHorizons.Utility { if (CachedGameObjects.TryGetValue(path, out var go)) return go; + // 1: normal find go = GameObject.Find(path); - if (go == null) + if (go) { - // find inactive use root + transform.find - var names = path.Split('/'); - 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(name); - if (go == null) - { - if (warn) Logger.LogWarning($"Couldn't find object with name {name}"); - return null; - } - } + CachedGameObjects.Add(path, go); + return go; } - CachedGameObjects.Add(path, go); - return go; + // 2: find inactive using root + transform.find + 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(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 GetAllChildren(this GameObject parent)