From e9ea621b93fbb58d9edfca8375918791637bbd52 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 30 Dec 2020 20:59:04 +0800 Subject: +init --- Client/Assembly-CSharp/GameObjectExtensions.cs | 77 ++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Client/Assembly-CSharp/GameObjectExtensions.cs (limited to 'Client/Assembly-CSharp/GameObjectExtensions.cs') diff --git a/Client/Assembly-CSharp/GameObjectExtensions.cs b/Client/Assembly-CSharp/GameObjectExtensions.cs new file mode 100644 index 0000000..57795eb --- /dev/null +++ b/Client/Assembly-CSharp/GameObjectExtensions.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using UnityEngine; + +public static class GameObjectExtensions +{ + public static T Find(this List self, GameObject toFind) where T : MonoBehaviour + { + for (int i = 0; i < self.Count; i++) + { + T t = self[i]; + if (t.gameObject == toFind) + { + return t; + } + } + return default(T); + } + + public static void SetZ(this Transform self, float z) + { + Vector3 localPosition = self.localPosition; + localPosition.z = z; + self.localPosition = localPosition; + } + + public static void LookAt2d(this Transform self, Vector3 target) + { + Vector3 vector = target - self.transform.position; + vector.Normalize(); + float num = Mathf.Atan2(vector.y, vector.x); + self.transform.rotation = Quaternion.Euler(0f, 0f, num * 57.29578f); + } + + public static void LookAt2d(this Transform self, Transform target) + { + self.LookAt2d(target.transform.position); + } + + public static void DestroyChildren(this Transform self) + { + for (int i = self.childCount - 1; i > -1; i--) + { + Transform child = self.GetChild(i); + child.transform.SetParent(null); + UnityEngine.Object.Destroy(child.gameObject); + } + } + + public static void DestroyChildren(this MonoBehaviour self) + { + for (int i = self.transform.childCount - 1; i > -1; i--) + { + UnityEngine.Object.Destroy(self.transform.GetChild(i).gameObject); + } + } + + public static void ForEachChild(this GameObject self, Action todo) + { + for (int i = self.transform.childCount - 1; i > -1; i--) + { + todo(self.transform.GetChild(i).gameObject); + } + } + + public static void ForEachChildBehavior(this MonoBehaviour self, Action todo) where T : MonoBehaviour + { + for (int i = self.transform.childCount - 1; i > -1; i--) + { + T component = self.transform.GetChild(i).GetComponent(); + if (component) + { + todo(component); + } + } + } +} -- cgit v1.1-26-g67d0