blob: 57795eb3254893ebd557072f614c9b201df3f256 (
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
|
using System;
using System.Collections.Generic;
using UnityEngine;
public static class GameObjectExtensions
{
public static T Find<T>(this List<T> 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<GameObject> todo)
{
for (int i = self.transform.childCount - 1; i > -1; i--)
{
todo(self.transform.GetChild(i).gameObject);
}
}
public static void ForEachChildBehavior<T>(this MonoBehaviour self, Action<T> todo) where T : MonoBehaviour
{
for (int i = self.transform.childCount - 1; i > -1; i--)
{
T component = self.transform.GetChild(i).GetComponent<T>();
if (component)
{
todo(component);
}
}
}
}
|