mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
143 lines
4.5 KiB
C#
143 lines
4.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace NewHorizons.Utility
|
|
{
|
|
public static class SearchUtilities
|
|
{
|
|
private static readonly Dictionary<string, GameObject> CachedGameObjects = new Dictionary<string, GameObject>();
|
|
|
|
public static void ClearCache()
|
|
{
|
|
Logger.LogVerbose("Clearing search cache");
|
|
CachedGameObjects.Clear();
|
|
}
|
|
|
|
public static List<T> FindObjectsOfTypeAndName<T>(string name) where T : Object
|
|
{
|
|
T[] firstList = GameObject.FindObjectsOfType<T>();
|
|
List<T> finalList = new List<T>();
|
|
|
|
for (var i = 0; i < firstList.Length; i++)
|
|
{
|
|
if (firstList[i].name == name)
|
|
{
|
|
finalList.Add(firstList[i]);
|
|
}
|
|
}
|
|
|
|
return finalList;
|
|
}
|
|
|
|
public static T FindObjectOfTypeAndName<T>(string name) where T : Object
|
|
{
|
|
T[] firstList = GameObject.FindObjectsOfType<T>();
|
|
|
|
for (var i = 0; i < firstList.Length; i++)
|
|
{
|
|
if (firstList[i].name == name)
|
|
{
|
|
return firstList[i];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static List<T> FindResourcesOfTypeAndName<T>(string name) where T : Object
|
|
{
|
|
T[] firstList = Resources.FindObjectsOfTypeAll<T>();
|
|
List<T> finalList = new List<T>();
|
|
|
|
for (var i = 0; i < firstList.Length; i++)
|
|
{
|
|
if (firstList[i].name == name)
|
|
{
|
|
finalList.Add(firstList[i]);
|
|
}
|
|
}
|
|
|
|
return finalList;
|
|
}
|
|
|
|
public static T FindResourceOfTypeAndName<T>(string name) where T : Object
|
|
{
|
|
T[] firstList = Resources.FindObjectsOfTypeAll<T>();
|
|
|
|
for (var i = 0; i < firstList.Length; i++)
|
|
{
|
|
if (firstList[i].name == name)
|
|
{
|
|
return firstList[i];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static string GetPath(this Transform current)
|
|
{
|
|
if (current.parent == null) return current.name;
|
|
return current.parent.GetPath() + "/" + current.name;
|
|
}
|
|
|
|
public static GameObject FindChild(this GameObject g, string childPath) =>
|
|
g.transform.Find(childPath)?.gameObject;
|
|
|
|
/// <summary>
|
|
/// finds active or inactive object by path,
|
|
/// or recursively finds an active or inactive object by name
|
|
/// </summary>
|
|
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)
|
|
{
|
|
// 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<GameObject>(name);
|
|
if (go == null)
|
|
{
|
|
if (warn) Logger.LogWarning($"Couldn't find object with name {name}");
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
CachedGameObjects.Add(path, go);
|
|
return go;
|
|
}
|
|
|
|
public static List<GameObject> GetAllChildren(this GameObject parent)
|
|
{
|
|
var children = new List<GameObject>();
|
|
foreach (Transform child in parent.transform)
|
|
{
|
|
children.Add(child.gameObject);
|
|
}
|
|
return children;
|
|
}
|
|
}
|
|
}
|