From 096ac539a1c09956cb3d205d8e4bb92307d1c4b0 Mon Sep 17 00:00:00 2001 From: JohnCorby Date: Sat, 9 Jul 2022 16:13:32 -0700 Subject: [PATCH] make Find better (cache more) --- NewHorizons/Utility/SearchUtilities.cs | 37 +++++++++++++++----------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/NewHorizons/Utility/SearchUtilities.cs b/NewHorizons/Utility/SearchUtilities.cs index 18f3299e..cd728301 100644 --- a/NewHorizons/Utility/SearchUtilities.cs +++ b/NewHorizons/Utility/SearchUtilities.cs @@ -88,29 +88,34 @@ namespace NewHorizons.Utility public static GameObject FindChild(this GameObject g, string childPath) => g.transform.Find(childPath)?.gameObject; + /// + /// finds active or inactive object by path, + /// or recursively finds an active object by name + /// public static GameObject Find(string path, bool warn = true) { if (CachedGameObjects.TryGetValue(path, out var go)) return go; go = GameObject.Find(path); - if (go != null) return go; - - 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 = names.Skip(1).Join(delimiter: "/"); - 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}"); - go = FindObjectOfTypeAndName(name); + // 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 = names.Skip(1).Join(delimiter: "/"); + go = root.FindChild(childPath); + if (go == null) + { + if (warn) Logger.LogWarning($"Couldn't find child object in path ({path})"); + return null; + } } CachedGameObjects.Add(path, go);