mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
shorten Find
This commit is contained in:
parent
d568815a37
commit
cb8cbdd285
@ -1,8 +1,9 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.SceneManagement;
|
||||||
using Object = UnityEngine.Object;
|
using Object = UnityEngine.Object;
|
||||||
|
|
||||||
namespace NewHorizons.Utility
|
namespace NewHorizons.Utility
|
||||||
{
|
{
|
||||||
public static class SearchUtilities
|
public static class SearchUtilities
|
||||||
@ -83,81 +84,29 @@ namespace NewHorizons.Utility
|
|||||||
return current.parent.GetPath() + "/" + current.name;
|
return current.parent.GetPath() + "/" + current.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static GameObject FindChild(this GameObject g, string path) => g.transform.Find(path)?.gameObject;
|
public static GameObject FindChild(this GameObject g, string path) =>
|
||||||
|
g.transform.Find(path)?.gameObject;
|
||||||
|
|
||||||
public static GameObject Find(string path, bool warn = true)
|
public static GameObject Find(string path, bool warn = true)
|
||||||
{
|
{
|
||||||
if (CachedGameObjects.ContainsKey(path))
|
if (CachedGameObjects.TryGetValue(path, out var go)) return go;
|
||||||
{
|
|
||||||
return CachedGameObjects[path];
|
var paths = path.Split(new[] { '/' }, 1);
|
||||||
}
|
var root = SceneManager.GetActiveScene().GetRootGameObjects().FirstOrDefault(x => x.name == paths[0]);
|
||||||
try
|
if (root == null)
|
||||||
{
|
|
||||||
var go = GameObject.Find(path);
|
|
||||||
|
|
||||||
var names = path.Split(new char[] { '\\', '/' });
|
|
||||||
if (go == null)
|
|
||||||
{
|
|
||||||
|
|
||||||
// Get the root object and hope its the right one
|
|
||||||
var root = GameObject.Find(names[0]);
|
|
||||||
if (root == null) root = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects().Where(x => x.name.Equals(names[0])).FirstOrDefault();
|
|
||||||
|
|
||||||
var t = root?.transform;
|
|
||||||
if (t == null)
|
|
||||||
{
|
|
||||||
if (warn) Logger.LogWarning($"Couldn't find root object in path ({names[0]})");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (int i = 1; i < names.Length; i++)
|
|
||||||
{
|
|
||||||
var child = t.transform.Find(names[i]);
|
|
||||||
|
|
||||||
if (child == null)
|
|
||||||
{
|
|
||||||
foreach (Transform c in t.GetComponentsInChildren<Transform>(true))
|
|
||||||
{
|
|
||||||
if (c.name.Equals(names[i]))
|
|
||||||
{
|
|
||||||
child = c;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (child == null)
|
|
||||||
{
|
|
||||||
if (warn) Logger.LogWarning($"Couldn't find object in path ({names[i]})");
|
|
||||||
t = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
t = child;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
go = t?.gameObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (go == null)
|
|
||||||
{
|
|
||||||
var name = names.Last();
|
|
||||||
if (warn) Logger.LogWarning($"Couldn't find object {path}, will look for potential matches for name {name}");
|
|
||||||
go = FindObjectOfTypeAndName<GameObject>(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (go != null)
|
|
||||||
{
|
|
||||||
CachedGameObjects.Add(path, go);
|
|
||||||
}
|
|
||||||
|
|
||||||
return go;
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
{
|
||||||
|
if (warn) Logger.LogWarning($"Couldn't find root object in path ({path})");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go = root.FindChild(paths[1]);
|
||||||
|
if (go == null)
|
||||||
|
{
|
||||||
|
if (warn) Logger.LogWarning($"Couldn't find object in path ({path})");
|
||||||
|
}
|
||||||
|
|
||||||
|
CachedGameObjects.Add(path, go);
|
||||||
|
return go;
|
||||||
}
|
}
|
||||||
|
|
||||||
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