From 6cd3c00b0b1b0a76b690ad0d978ae265de43a371 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Tue, 13 Jun 2023 19:14:40 +0800 Subject: *misc --- .../Assets/Scripts/Tools/ChildLocator.cs | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 WorldlineKeepers/Assets/Scripts/Tools/ChildLocator.cs (limited to 'WorldlineKeepers/Assets/Scripts/Tools/ChildLocator.cs') 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(); + + 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(string childName) + { + return FindChildComponent(FindChildIndex(childName)); + } + + public T FindChildComponent(int childIndex) + { + Transform transform = FindChild(childIndex); + if (!transform) + { + return default(T); + } + return transform.GetComponent(); + } +} -- cgit v1.1-26-g67d0