diff options
author | chai <215380520@qq.com> | 2023-06-13 19:14:40 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-06-13 19:14:40 +0800 |
commit | 6cd3c00b0b1b0a76b690ad0d978ae265de43a371 (patch) | |
tree | 1004e09d96a5a12841443b72adb44a90bbd8d2e7 /WorldlineKeepers/Assets/Scripts/Tools/ChildLocator.cs | |
parent | e8049efa4a0b462b04e3087402c8485c70432de9 (diff) |
*misc
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Tools/ChildLocator.cs')
-rw-r--r-- | WorldlineKeepers/Assets/Scripts/Tools/ChildLocator.cs | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/WorldlineKeepers/Assets/Scripts/Tools/ChildLocator.cs b/WorldlineKeepers/Assets/Scripts/Tools/ChildLocator.cs new file mode 100644 index 0000000..bc550ac --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/ChildLocator.cs @@ -0,0 +1,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>(); + } +} |