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); } } } }