Repository navigation
Expand file tree
/
Copy pathGameObject.cs
More file actions
237 lines (214 loc) · 6.83 KB
/
Copy pathGameObject.cs
File metadata and controls
237 lines (214 loc) · 6.83 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// -----------------------------------------------------------------
// File: GameObject
// Author: ruanban
// Date: 1/4/2018 3:59:09 PM
// Description:
//
// -----------------------------------------------------------------
using System;
using System.Reflection;
using System.Collections.Generic;
namespace UnityEngine
{
public sealed class GameObject : Object
{
private Transform _transform;
public Transform transform
{
get { this.CheckDestroy();return this._transform; }
internal set
{
this.CheckDestroy();
this._transform = value;
}
}
private bool _activeSelf = false;
public bool activeSelf
{
get { this.CheckDestroy(); return this._activeSelf; }
}
public void SetActive(bool value)
{
this.CheckDestroy();
if (this.activeSelf == value) return;
if (value)
{
this._activeSelf = value;
this.BroadcastMessage(UnityMessage.Awake);
this.BroadcastMessage(UnityMessage.OnEnable);
}
else
{
this.BroadcastMessage(UnityMessage.OnDisable);
this._activeSelf = value;
}
}
public bool activeInHierarchy
{
get
{
this.CheckDestroy();
var active = this.activeSelf;
var parent = transform.parent;
while (active && parent != null)
{
active = parent.gameObject.activeSelf;
parent = parent.parent;
}
return active;
}
}
private Dictionary<Type, Dictionary<int, Component>> _all_comps = new Dictionary<Type, Dictionary<int, Component>>();
private List<Component> all_comps
{
get
{
List<Component> list = new List<Component>();
foreach (var pair in this._all_comps)
{
list.AddRange(pair.Value.Values);
}
return list;
}
}
public GameObject()
:base()
{
this.transform = this.AddComponent<Transform>();
this.scene.AddGameObject(this);
this.SetActive(true);
}
public GameObject(string name)
:this()
{
this.name = name;
}
public GameObject(string name,params Type[] components)
:this(name)
{
if (components != null)
{
foreach (var comp in components)
{
this.AddComponent(comp);
}
}
}
public T AddComponent<T>() where T : Component
{
this.CheckDestroy();
return this.AddComponent(typeof(T)) as T;
}
public Component AddComponent(Type type)
{
this.CheckDestroy();
if (type != null && type.IsSubclassOf(typeof(Component)))
{
if (Attribute.IsDefined(type,typeof(DisallowMultipleComponentAttribute)))
{
if (this.GetComponent(type) != null)
{
throw new DisallowMultipleException(type);
}
}
Dictionary<int, Component> comps = null;
if (!this._all_comps.TryGetValue(type, out comps))
{
comps = new Dictionary<int, Component>();
this._all_comps.Add(type, comps);
}
if (comps != null)
{
var comp = Activator.CreateInstance(type) as Component;
if (comp != null)
{
comps.Add(comp.GetInstanceID(), comp);
comp.__OnAddToGameObject__(this);
}
return comp;
}
}
return null;
}
public Component GetComponent(Type type)
{
this.CheckDestroy();
Dictionary<int,Component> ret = null;
if (this._all_comps.TryGetValue(type, out ret))
{
var e = ret.GetEnumerator();
if (e.MoveNext()) return e.Current.Value;
}
return null;
}
public T GetComponent<T>() where T : Component
{
return this.GetComponent(typeof(T)) as T;
}
public List<Component> GetAllComponents()
{
List<Component> ret = new List<Component>();
foreach (var comps in this._all_comps.Values)
{
ret.AddRange(comps.Values);
}
return ret;
}
internal void DestroyComponentImmediate(Component comp)
{
if (comp != null)
{
Dictionary<int, Component> comps;
if (this._all_comps.TryGetValue(comp.GetType(), out comps) &&
comps.ContainsKey(comp.GetInstanceID()))
{
comp.SendMessage(UnityMessage.OnDestroy);
comps.Remove(comp.GetInstanceID());
}
}
}
public void SendMessage(string methodName)
{
this.CheckDestroy();
foreach(var comp in this.all_comps)
{
comp.SendMessage(methodName);
}
}
public void BroadcastMessage(string methodName)
{
this.CheckDestroy();
if (methodName == UnityMessage.OnDestroy || this.activeInHierarchy)
{
this.SendMessage(methodName);
var count = this.transform.GetChildCount();
for (int i = 0; i < count; ++i)
{
var t = this.transform.GetChild(i);
if (t != null)
{
t.gameObject.BroadcastMessage(methodName);
}
}
}
}
protected override void __OnWaitDestroy__()
{
this.scene.DestroyObject(this);
base.__OnWaitDestroy__();
}
protected override void __OnDestroy__()
{
this.SetActive(false);
while (this.transform.GetChildCount() > 0)
{
Object.DestroyImmediate(this.transform.GetChild(0).gameObject);
}
var comps = this.all_comps;
foreach (var comp in comps)
Object.__DestroyImmediate__(comp);
this.scene.DestroyGameObjectImmediate(this);
base.__OnDestroy__();
}
}
}