summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/Scripts/Tools/ChildLocator.cs
blob: bc550ac253916a97dd3988f143ec6309146929a9 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using UnityEngine;

[DisallowMultipleComponent]
public class ChildLocator : MonoBehaviour
{
	[Serializable]
	private struct NameTransformPair
	{
		public string name;

		public Transform transform;
	}

	[SerializeField]
	private NameTransformPair[] transformPairs = Array.Empty<NameTransformPair>();

	public int Count => transformPairs.Length;

	public int FindChildIndex(string childName)
	{
		for (int i = 0; i < transformPairs.Length; i++)
		{
			if (childName == transformPairs[i].name)
			{
				return i;
			}
		}
		return -1;
	}

	public int FindChildIndex(Transform childTransform)
	{
		for (int i = 0; i < transformPairs.Length; i++)
		{
			if ((object)childTransform == transformPairs[i].transform)
			{
				return i;
			}
		}
		return -1;
	}

	public string FindChildName(int childIndex)
	{
		if ((uint)childIndex < transformPairs.Length)
		{
			return transformPairs[childIndex].name;
		}
		return null;
	}

	public Transform FindChild(string childName)
	{
		return FindChild(FindChildIndex(childName));
	}

	public GameObject FindChildGameObject(int childIndex)
	{
		Transform transform = FindChild(childIndex);
		if (!transform)
		{
			return null;
		}
		return transform.gameObject;
	}

	public GameObject FindChildGameObject(string childName)
	{
		return FindChildGameObject(FindChildIndex(childName));
	}

	public Transform FindChild(int childIndex)
	{
		if ((uint)childIndex < transformPairs.Length)
		{
			return transformPairs[childIndex].transform;
		}
		return null;
	}

	public T FindChildComponent<T>(string childName)
	{
		return FindChildComponent<T>(FindChildIndex(childName));
	}

	public T FindChildComponent<T>(int childIndex)
	{
		Transform transform = FindChild(childIndex);
		if (!transform)
		{
			return default(T);
		}
		return transform.GetComponent<T>();
	}
}