generated from Raicuparta/ow-mod-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSearchUtilities.cs
77 lines (67 loc) · 2.75 KB
/
SearchUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace OWRichPresence
{
internal static class SearchUtilities
{
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 useResources = true, bool warn = true)
{
GameObject 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) OWRichPresence.WriteLine($"Couldn't find root object in path {path}", OWML.Common.MessageType.Warning, true);
return null;
}
var childPath = string.Join("/", names.Skip(1));
go = root.FindChild(childPath);
if (go == null)
{
var name = names.Last();
if (warn) OWRichPresence.WriteLine($"Couldn't find object in path {path}, will look for potential matches for name {name}", OWML.Common.MessageType.Warning, true);
if (useResources)
{
// find resource to include inactive objects
// also includes prefabs but hopefully thats okay
go = FindResourceOfTypeAndName<GameObject>(name);
if (go == null)
{
if (warn) OWRichPresence.WriteLine($"Couldn't find object with name {name}", OWML.Common.MessageType.Warning, true);
return null;
}
}
}
}
return go;
}
}
}