Repository navigation
Expand file tree
/
Copy pathTransform.cs
More file actions
168 lines (148 loc) · 4.17 KB
/
Copy pathTransform.cs
File metadata and controls
168 lines (148 loc) · 4.17 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
// -----------------------------------------------------------------
// File: Transform
// Author: ruanban
// Date: 1/4/2018 4:54:43 PM
// Description:
//
// -----------------------------------------------------------------
using System.Collections.Generic;
namespace UnityEngine
{
[DisallowMultipleComponent]
public class Transform : Component
{
private Transform _root;
public Transform root
{
get { this.CheckDestroy(); return this._root; }
private set
{
this.CheckDestroy();
this._root = value;
}
}
private Transform _parent;
public Transform parent
{
get { this.CheckDestroy();return this._parent; }
set
{
this.CheckDestroy();
if (value == this._parent) return;
this.Detach();
this._parent = value;
this.Attach();
}
}
internal override void __OnAddToGameObject__(GameObject obj)
{
obj.transform = this;
base.__OnAddToGameObject__(obj);
}
protected override void __OnDestroy__()
{
this.Detach();
this.gameObject.transform = null;
base.__OnDestroy__();
}
private List<Transform> _children = new List<Transform>();
public void Detach()
{
if (this.parent != null)
{
this.parent._children.Remove(this);
this.scene.AddRootGameObject(this.gameObject);
}
this.root = this;
}
protected void Attach()
{
if (this.parent != null)
{
this.scene.RemoveRootGameObject(this.gameObject);
this.parent._children.Add(this);
this.root = this.parent.root;
}
}
public void SetSiblingIndex(int index)
{
if (this.parent == null)
{
this.scene.SetRootIndex(this.gameObject, index);
}
else
{
if (this.parent._children.Remove(this))
{
if (index >= 0 && index <= this.parent._children.Count)
{
this.parent._children.Insert(index, this);
}
else
{
this.parent._children.Add(this);
}
}
}
}
public int GetSiblingIndex()
{
if (this.parent == null)
{
return this.scene.GetRootIndex(this.gameObject);
}
else
{
return this.parent._children.IndexOf(this);
}
}
public void SetAsFirstSibling()
{
this.SetSiblingIndex(0);
}
public void SetAsLastSibling()
{
this.SetSiblingIndex(-1);
}
private Transform _Find(Transform target, string name)
{
foreach (var t in target._children)
{
if (t.name == name)
{
return t;
}
}
return null;
}
public Transform Find(string name)
{
if (!string.IsNullOrEmpty(name))
{
var strs = name.Split('/');
var target = this;
for (int i = 0; i < strs.Length; ++i)
{
if (target != null)
{
target = _Find(target, strs[i]);
}
else
break;
}
return target;
}
return null;
}
public Transform GetChild(int index)
{
if (index >= 0 && index < this._children.Count)
return this._children[index];
return null;
}
public int GetChildCount()
{
return this._children.Count;
}
}
}