@@ -60,50 +60,80 @@ protected string GetRandomFileNamePath(string pathName = null, string prefixName
60
60
return temp ;
61
61
}
62
62
63
+ protected GameObject m_root ;
64
+
63
65
[ TearDown ]
64
66
public void Term ( )
65
67
{
66
68
foreach ( string file in Directory . GetFiles ( this . filePath , MakeFileName ( "*" ) ) ) {
67
69
File . Delete ( file ) ;
68
70
}
71
+ if ( m_root ) {
72
+ UnityEngine . Object . DestroyImmediate ( m_root ) ;
73
+ }
69
74
}
70
75
71
76
[ Test ]
72
77
public void TestDefaultSelection ( )
73
78
{
74
- var root = CreateHierarchy ( ) ;
75
- Assert . IsNotNull ( root ) ;
79
+ m_root = CreateHierarchy ( ) ;
80
+ Assert . IsNotNull ( m_root ) ;
76
81
77
82
// test Export Root
78
83
// Expected result: everything gets exported
79
- var exportedRoot = ExportSelection ( new Object [ ] { root } ) ;
80
- CompareHierarchies ( root , exportedRoot , true ) ;
84
+ // Expected transform: root is zeroed out, all other transforms unchanged
85
+ var exportedRoot = ExportSelection ( new Object [ ] { m_root } ) ;
86
+ CompareHierarchies ( m_root , exportedRoot , true , false ) ;
87
+ CompareGlobalTransform ( exportedRoot . transform ) ;
81
88
82
89
// test Export Parent1, Child1
83
90
// Expected result: Parent1, Child1, Child2
84
- var parent1 = root . transform . Find ( "Parent1" ) ;
91
+ // Expected transform: Parent1 zeroed out, all other transforms unchanged
92
+ var parent1 = m_root . transform . Find ( "Parent1" ) ;
85
93
var child1 = parent1 . Find ( "Child1" ) ;
86
94
exportedRoot = ExportSelection ( new Object [ ] { parent1 . gameObject , child1 . gameObject } ) ;
87
- CompareHierarchies ( parent1 . gameObject , exportedRoot , true ) ;
95
+ CompareHierarchies ( parent1 . gameObject , exportedRoot , true , false ) ;
96
+ CompareGlobalTransform ( exportedRoot . transform ) ;
88
97
89
98
// test Export Child2
90
99
// Expected result: Child2
100
+ // Expected transform: Child2 zeroed out
91
101
var child2 = parent1 . Find ( "Child2" ) . gameObject ;
92
102
exportedRoot = ExportSelection ( new Object [ ] { child2 } ) ;
93
- CompareHierarchies ( child2 , exportedRoot , true ) ;
103
+ CompareHierarchies ( child2 , exportedRoot , true , false ) ;
104
+ CompareGlobalTransform ( exportedRoot . transform ) ;
94
105
95
106
// test Export Child2, Parent2
96
107
// Expected result: Parent2, Child3, Child2
97
- var parent2 = root . transform . Find ( "Parent2" ) ;
108
+ // Expected transform: Child2 and Parent2 maintain global transform
109
+ var parent2 = m_root . transform . Find ( "Parent2" ) ;
98
110
exportedRoot = ExportSelection ( new Object [ ] { child2 , parent2 } ) ;
99
111
100
112
List < GameObject > children = new List < GameObject > ( ) ;
101
113
foreach ( Transform child in exportedRoot . transform ) {
102
114
children . Add ( child . gameObject ) ;
103
115
}
104
116
CompareHierarchies ( new GameObject [ ] { child2 , parent2 . gameObject } , children . ToArray ( ) ) ;
117
+ }
105
118
106
- UnityEngine . Object . DestroyImmediate ( root ) ;
119
+ /// <summary>
120
+ /// Compares the global transform of expected
121
+ /// to the local transform of actual.
122
+ /// </summary>
123
+ /// <param name="actual">Actual.</param>
124
+ /// <param name="expected">Expected.</param>
125
+ private void CompareGlobalTransform ( Transform actual , Transform expected = null ) {
126
+ if ( ! expected ) {
127
+ // test that actual is zeroed out
128
+ Assert . AreEqual ( Vector3 . zero , actual . localPosition ) ;
129
+ Assert . AreEqual ( Vector3 . zero , actual . localEulerAngles ) ;
130
+ Assert . AreEqual ( Vector3 . one , actual . localScale ) ;
131
+ return ;
132
+ }
133
+ float epsilon = 0.0001f ;
134
+ Assert . IsTrue ( Vector3 . SqrMagnitude ( expected . position - actual . localPosition ) < epsilon ) ;
135
+ Assert . IsTrue ( Vector3 . SqrMagnitude ( expected . rotation . eulerAngles - actual . localEulerAngles ) < epsilon ) ;
136
+ Assert . IsTrue ( Vector3 . SqrMagnitude ( expected . lossyScale - actual . localScale ) < epsilon ) ;
107
137
}
108
138
109
139
private GameObject CreateHierarchy ( )
@@ -117,9 +147,23 @@ private GameObject CreateHierarchy ()
117
147
// ----> Child3
118
148
119
149
var root = CreateGameObject ( "Root" ) ;
150
+ SetTransform ( root . transform ,
151
+ new Vector3 ( 3 , 4 , - 6 ) ,
152
+ new Vector3 ( 45 , 10 , 34 ) ,
153
+ new Vector3 ( 2 , 1 , 3 ) ) ;
120
154
121
155
var parent1 = CreateGameObject ( "Parent1" , root . transform ) ;
156
+ SetTransform ( parent1 . transform ,
157
+ new Vector3 ( 53 , 0 , - 1 ) ,
158
+ new Vector3 ( 0 , 5 , 0 ) ,
159
+ new Vector3 ( 1 , 1 , 1 ) ) ;
160
+
122
161
var parent2 = CreateGameObject ( "Parent2" , root . transform ) ;
162
+ SetTransform ( parent2 . transform ,
163
+ new Vector3 ( 0 , 0 , 0 ) ,
164
+ new Vector3 ( 90 , 1 , 3 ) ,
165
+ new Vector3 ( 1 , 0.3f , 0.5f ) ) ;
166
+
123
167
parent1 . transform . SetAsFirstSibling ( ) ;
124
168
125
169
CreateGameObject ( "Child1" , parent1 . transform ) ;
@@ -129,21 +173,34 @@ private GameObject CreateHierarchy ()
129
173
return root ;
130
174
}
131
175
176
+ private void SetTransform ( Transform t , Vector3 pos , Vector3 rot , Vector3 scale ) {
177
+ t . localPosition = pos ;
178
+ t . localEulerAngles = rot ;
179
+ t . localScale = scale ;
180
+ }
181
+
132
182
private GameObject CreateGameObject ( string name , Transform parent = null )
133
183
{
134
184
var go = new GameObject ( name ) ;
135
185
go . transform . SetParent ( parent ) ;
136
186
return go ;
137
187
}
138
188
139
- private void CompareHierarchies ( GameObject expectedHierarchy , GameObject actualHierarchy , bool ignoreName = false )
189
+ private void CompareHierarchies (
190
+ GameObject expectedHierarchy , GameObject actualHierarchy ,
191
+ bool ignoreName = false , bool compareTransform = true )
140
192
{
141
193
if ( ! ignoreName ) {
142
194
Assert . AreEqual ( expectedHierarchy . name , actualHierarchy . name ) ;
143
195
}
144
196
145
197
var expectedTransform = expectedHierarchy . transform ;
146
198
var actualTransform = actualHierarchy . transform ;
199
+
200
+ if ( compareTransform ) {
201
+ Assert . AreEqual ( expectedTransform , actualTransform ) ;
202
+ }
203
+
147
204
Assert . AreEqual ( expectedTransform . childCount , actualTransform . childCount ) ;
148
205
149
206
foreach ( Transform expectedChild in expectedTransform ) {
@@ -165,7 +222,10 @@ private void CompareHierarchies(GameObject[] expectedHierarchy, GameObject[] act
165
222
} ) ;
166
223
167
224
for ( int i = 0 ; i < expectedHierarchy . Length ; i ++ ) {
168
- CompareHierarchies ( expectedHierarchy [ i ] , actualHierarchy [ i ] ) ;
225
+ CompareHierarchies ( expectedHierarchy [ i ] , actualHierarchy [ i ] , false , false ) ;
226
+ // if we are Comparing lists of hierarchies, that means that the transforms
227
+ // should be the global transform of expected, as there is no zeroed out root
228
+ CompareGlobalTransform ( actualHierarchy [ i ] . transform , expectedHierarchy [ i ] . transform ) ;
169
229
}
170
230
}
171
231
0 commit comments