@@ -12,6 +12,8 @@ public class FbxPrefabTest : ExporterTestBase
12
12
FbxPrefab . FbxRepresentation m_originalRep ;
13
13
14
14
GameObject m_source ; // the fbx model
15
+ FbxPrefab . FbxRepresentation m_originalHistory ;
16
+
15
17
GameObject m_autoPrefab ; // prefab that auto-updates
16
18
GameObject m_manualPrefab ; // prefab that doesn't auto-update
17
19
@@ -49,61 +51,86 @@ void OnUpdate(FbxPrefab prefabInstance, IEnumerable<GameObject> updated)
49
51
}
50
52
}
51
53
54
+ static KeyValuePair < string , FbxPrefab . FbxRepresentation > StackItem (
55
+ string name , FbxPrefab . FbxRepresentation rep )
56
+ {
57
+ return new KeyValuePair < string , FbxPrefab . FbxRepresentation > ( name , rep ) ;
58
+ }
59
+
52
60
public static void AssertAreIdentical (
53
61
FbxPrefab . FbxRepresentation a ,
54
62
FbxPrefab . FbxRepresentation b ) {
55
63
// A bit of a laborious comparison scheme. This is due to the
56
64
// round-trip through FBX causing tiny errors in the transforms.
57
- var astack = new List < FbxPrefab . FbxRepresentation > ( ) ;
58
- astack . Add ( a ) ;
59
- var bstack = new List < FbxPrefab . FbxRepresentation > ( ) ;
60
- bstack . Add ( b ) ;
65
+ var astack = new List < KeyValuePair < string , FbxPrefab . FbxRepresentation > > ( ) ;
66
+ astack . Add ( StackItem ( "(root)" , a ) ) ;
67
+ var bstack = new List < KeyValuePair < string , FbxPrefab . FbxRepresentation > > ( ) ;
68
+ bstack . Add ( StackItem ( "(root)" , b ) ) ;
61
69
62
70
var aDummy = new GameObject ( "aDummy" ) . transform ;
63
71
var bDummy = new GameObject ( "bDummy" ) . transform ;
64
72
while ( astack . Count > 0 ) {
65
73
Assert . AreEqual ( astack . Count , bstack . Count ) ; // should never fail
66
- a = astack [ astack . Count - 1 ] ; astack . RemoveAt ( astack . Count - 1 ) ;
67
- b = bstack [ bstack . Count - 1 ] ; bstack . RemoveAt ( bstack . Count - 1 ) ;
74
+ var aKvp = astack [ astack . Count - 1 ] ; astack . RemoveAt ( astack . Count - 1 ) ;
75
+ var bKvp = bstack [ bstack . Count - 1 ] ; bstack . RemoveAt ( bstack . Count - 1 ) ;
76
+
77
+ var aName = aKvp . Key ;
78
+ var bName = bKvp . Key ;
79
+ Assert . AreEqual ( aName , bName ) ;
80
+
81
+ a = aKvp . Value ;
82
+ b = bKvp . Value ;
68
83
69
84
// Verify that they have the same children (by name).
70
85
var achildren = a . ChildNames ;
71
86
var bchildren = b . ChildNames ;
72
- Assert . That ( achildren , Is . EquivalentTo ( bchildren ) ) ;
87
+ Assert . That ( achildren , Is . EquivalentTo ( bchildren ) , aName + " children" ) ;
73
88
74
- // Add the children to each stack.
89
+ // Add the children to each stack. It's important to get the
90
+ // same order for both stacks.
75
91
foreach ( var child in achildren ) {
76
- astack . Add ( a . GetChild ( child ) ) ;
77
- bstack . Add ( b . GetChild ( child ) ) ;
92
+ astack . Add ( StackItem ( child , a . GetChild ( child ) ) ) ;
93
+ bstack . Add ( StackItem ( child , b . GetChild ( child ) ) ) ;
78
94
}
79
95
80
96
// Verify that they have the same components.
81
97
var atypes = a . ComponentTypes ;
82
98
var btypes = b . ComponentTypes ;
83
- Assert . That ( atypes , Is . EquivalentTo ( btypes ) ) ;
99
+ Assert . That ( atypes , Is . EquivalentTo ( btypes ) , aName + " component types" ) ;
84
100
85
101
foreach ( var t in atypes ) {
86
102
var avalues = a . GetComponentValues ( t ) ;
87
103
var bvalues = b . GetComponentValues ( t ) ;
88
- Assert . AreEqual ( avalues . Count , bvalues . Count ) ;
89
-
90
- if ( t != "UnityEngine.Transform" ) {
91
- Assert . AreEqual ( avalues , bvalues ) ;
92
- } else {
104
+ Assert . AreEqual ( avalues . Count , bvalues . Count , aName + " component multiplicity" ) ;
105
+
106
+ //
107
+ // TODO: test that the values match up.
108
+ // Exceptions:
109
+ // - Transforms we expect small changes.
110
+ // - MeshFilter and MeshRenderer we expect guids to change
111
+ //
112
+ if ( t == "UnityEngine.MeshFilter" ) {
113
+ // TODO: test that everything but the guid matches
114
+ } else if ( t == "UnityEngine.MeshRenderer" ) {
115
+ // TODO: test that everything but the guid matches
116
+ } else if ( t == "UnityEngine.Transform" ) {
93
117
// Verify that the transforms are nearly (but don't require bitwise) equal.
94
118
EditorJsonUtility . FromJsonOverwrite ( avalues [ 0 ] , aDummy ) ;
95
119
EditorJsonUtility . FromJsonOverwrite ( bvalues [ 0 ] , bDummy ) ;
96
120
var dist = Vector3 . Distance ( aDummy . localPosition , bDummy . localPosition ) ;
97
- Assert . That ( dist , Is . LessThan ( 1e-6 ) , ( ) => string . Format ( "position {0} vs {1} dist {2}" ,
98
- aDummy . localPosition , bDummy . localPosition , dist ) ) ;
121
+ Assert . That ( dist , Is . LessThan ( 1e-6 ) , ( ) => string . Format ( "{3}: position {0} vs {1} dist {2}" ,
122
+ aDummy . localPosition , bDummy . localPosition , dist , aName ) ) ;
99
123
100
124
dist = Vector3 . Distance ( aDummy . localScale , bDummy . localScale ) ;
101
- Assert . That ( dist , Is . LessThan ( 1e-6 ) , ( ) => string . Format ( "scale {0} vs {1} dist {2}" ,
102
- aDummy . localScale , bDummy . localScale , dist ) ) ;
125
+ Assert . That ( dist , Is . LessThan ( 1e-6 ) , ( ) => string . Format ( "{3}: scale {0} vs {1} dist {2}" ,
126
+ aDummy . localScale , bDummy . localScale , dist , aName ) ) ;
103
127
104
128
dist = Quaternion . Angle ( aDummy . localRotation , bDummy . localRotation ) ;
105
- Assert . That ( dist , Is . LessThan ( 1e-6 ) , ( ) => string . Format ( "rotation {0} vs {1} angle {2}" ,
106
- aDummy . localRotation . eulerAngles , bDummy . localRotation . eulerAngles , dist ) ) ;
129
+ Assert . That ( dist , Is . LessThan ( 1e-6 ) , ( ) => string . Format ( "{3}: rotation {0} vs {1} angle {2}" ,
130
+ aDummy . localRotation . eulerAngles , bDummy . localRotation . eulerAngles , dist , aName ) ) ;
131
+ } else {
132
+ // Default: test that the components are precisely identical.
133
+ Assert . AreEqual ( avalues , bvalues , string . Format ( "{0}: type {1}" , aName , t ) ) ;
107
134
}
108
135
}
109
136
}
@@ -126,6 +153,7 @@ public void Init() {
126
153
var fbxAsset = FbxExporters . Editor . ModelExporter . ExportObject (
127
154
GetRandomFbxFilePath ( ) , m_original ) ;
128
155
m_source = AssetDatabase . LoadMainAssetAtPath ( fbxAsset ) as GameObject ;
156
+ m_originalHistory = Rep ( m_source ) ;
129
157
Assert . IsTrue ( m_source ) ;
130
158
131
159
// Create an FbxPrefab linked to the Fbx file. Make it auto-update.
@@ -166,7 +194,11 @@ GameObject ModifySourceFbx()
166
194
// - add parent3
167
195
var newModel = PrefabUtility . InstantiatePrefab ( m_source ) as GameObject ;
168
196
GameObject . DestroyImmediate ( newModel . transform . Find ( "Parent1" ) . gameObject ) ;
169
- CreateGameObject ( "Parent3" , newModel . transform ) ;
197
+ var parent3 = CreateGameObject ( "Parent3" , newModel . transform ) ;
198
+
199
+ // We're not doing an apply operation, so the collider isn't
200
+ // supposed to get to the prefab, so don't test that it does.
201
+ Object . DestroyImmediate ( parent3 . GetComponent < BoxCollider > ( ) ) ;
170
202
171
203
// Export it to clobber the old FBX file.
172
204
// Sleep one second first to make sure the timestamp differs
@@ -183,29 +215,35 @@ GameObject ModifySourceFbx()
183
215
184
216
[ Test ]
185
217
public void BasicTest ( ) {
186
- // Check the history is good at the start
218
+ // Verify we start in the right place.
187
219
AssertAreIdentical ( m_originalRep , Rep ( m_manualPrefab ) ) ;
188
220
AssertAreIdentical ( m_originalRep , Rep ( m_autoPrefab ) ) ;
189
- AssertAreIdentical ( m_originalRep , History ( m_manualPrefab ) ) ;
190
- AssertAreIdentical ( m_originalRep , History ( m_autoPrefab ) ) ;
221
+
222
+ // Verify history is as we expect. It differs from the originalRep
223
+ // in that it doesn't have box colliders.
224
+ AssertAreIdentical ( m_originalHistory , History ( m_manualPrefab ) ) ;
225
+ AssertAreIdentical ( m_originalHistory , History ( m_autoPrefab ) ) ;
191
226
192
227
FbxPrefab . FbxRepresentation newHierarchy ;
228
+ FbxPrefab . FbxRepresentation newHistory ;
193
229
using ( var updateSet = new UpdateListener ( m_autoPrefab ) ) {
194
230
Debug . Log ( "Testing auto update" ) ;
195
231
newHierarchy = Rep ( ModifySourceFbx ( ) ) ;
232
+ newHistory = Rep ( m_source ) ;
196
233
AssertAreDifferent ( m_originalRep , newHierarchy ) ;
197
234
198
- // Make sure the fbx source changed.
199
- AssertAreDifferent ( m_originalRep , Rep ( m_source ) ) ;
200
- AssertAreIdentical ( newHierarchy , Rep ( m_source ) ) ;
235
+ // Make sure the fbx source changed (testing the test).
236
+ AssertAreDifferent ( m_originalHistory , Rep ( m_source ) ) ;
201
237
202
238
// Make sure the auto-update prefab changed.
239
+ Debug . Log ( string . Format ( "source: {0}\n prefab: {1}" ,
240
+ newHierarchy . ToJson ( ) , Rep ( m_autoPrefab ) . ToJson ( ) ) ) ;
203
241
AssertAreIdentical ( newHierarchy , Rep ( m_autoPrefab ) ) ;
204
- AssertAreIdentical ( newHierarchy , History ( m_autoPrefab ) ) ;
242
+ AssertAreIdentical ( newHierarchy , Rep ( m_source ) ) ;
205
243
206
244
// Make sure the manual-update prefab didn't.
207
245
AssertAreIdentical ( m_originalRep , Rep ( m_manualPrefab ) ) ;
208
- AssertAreIdentical ( m_originalRep , History ( m_manualPrefab ) ) ;
246
+ AssertAreIdentical ( m_originalHistory , History ( m_manualPrefab ) ) ;
209
247
210
248
// Make sure we got the right changes.
211
249
Assert . AreEqual ( 1 , updateSet . NumUpdates ) ;
@@ -223,7 +261,7 @@ public void BasicTest() {
223
261
var manualPrefabComponent = m_manualPrefab . GetComponent < FbxPrefab > ( ) ;
224
262
manualPrefabComponent . SyncPrefab ( ) ;
225
263
AssertAreIdentical ( newHierarchy , Rep ( m_manualPrefab ) ) ;
226
- AssertAreIdentical ( newHierarchy , History ( m_manualPrefab ) ) ;
264
+ AssertAreIdentical ( newHistory , History ( m_manualPrefab ) ) ;
227
265
228
266
// Check some corner cases.
229
267
Assert . AreEqual ( m_source , manualPrefabComponent . GetFbxAsset ( ) ) ;
@@ -245,38 +283,44 @@ public void BasicTest() {
245
283
Assert . That ( ( ) => manualPrefabComponent . SetSourceModel ( null ) , Throws . Nothing ) ;
246
284
Assert . IsNull ( manualPrefabComponent . GetFbxAsset ( ) ) ;
247
285
AssertAreIdentical ( newHierarchy , Rep ( m_manualPrefab ) ) ;
248
- AssertAreIdentical ( newHierarchy , History ( m_manualPrefab ) ) ;
286
+ AssertAreIdentical ( newHistory , History ( m_manualPrefab ) ) ;
249
287
Assert . That ( ( ) => manualPrefabComponent . SyncPrefab ( ) , Throws . Nothing ) ;
250
288
AssertAreIdentical ( newHierarchy , Rep ( m_manualPrefab ) ) ;
251
- AssertAreIdentical ( newHierarchy , History ( m_manualPrefab ) ) ;
289
+ AssertAreIdentical ( newHistory , History ( m_manualPrefab ) ) ;
252
290
253
291
// Switch to some other model, which looks like the original model
254
292
// (but is a totally different file). This will cause an update
255
- // immediately.
293
+ // immediately. We expect to have lost the colliders on the objects
294
+ // that were deleted in the interim.
256
295
var fbxAsset = FbxExporters . Editor . ModelExporter . ExportObject (
257
296
GetRandomFbxFilePath ( ) , m_original ) ;
258
297
var newSource = AssetDatabase . LoadMainAssetAtPath ( fbxAsset ) as GameObject ;
259
298
Assert . IsTrue ( newSource ) ;
260
299
Debug . Log ( "Testing SetSourceModel relink" ) ;
261
300
manualPrefabComponent . SetSourceModel ( newSource ) ;
262
301
AssertAreIdentical ( m_originalRep , Rep ( m_manualPrefab ) ) ;
263
- AssertAreIdentical ( m_originalRep , History ( m_manualPrefab ) ) ;
302
+ AssertAreIdentical ( m_originalHistory , History ( m_manualPrefab ) ) ;
264
303
}
265
304
266
305
[ Test ]
267
306
public void ManualToAuto ( ) {
268
307
// Check what happens when we go from manual to auto-update.
308
+ Debug . Log ( "ManualToAuto: modifying source" ) ;
269
309
var newHierarchy = Rep ( ModifySourceFbx ( ) ) ;
310
+ var newHistory = Rep ( m_source ) ;
311
+
270
312
AssertAreIdentical ( m_originalRep , Rep ( m_manualPrefab ) ) ;
271
- AssertAreIdentical ( m_originalRep , History ( m_manualPrefab ) ) ;
313
+ AssertAreIdentical ( m_originalHistory , History ( m_manualPrefab ) ) ;
272
314
315
+ Debug . Log ( "ManualToAuto: setting manual to manual" ) ;
273
316
m_manualPrefab . GetComponent < FbxPrefab > ( ) . SetAutoUpdate ( false ) ;
274
317
AssertAreIdentical ( m_originalRep , Rep ( m_manualPrefab ) ) ;
275
- AssertAreIdentical ( m_originalRep , History ( m_manualPrefab ) ) ;
318
+ AssertAreIdentical ( m_originalHistory , History ( m_manualPrefab ) ) ;
276
319
320
+ Debug . Log ( "ManualToAuto: setting manual to auto" ) ;
277
321
m_manualPrefab . GetComponent < FbxPrefab > ( ) . SetAutoUpdate ( true ) ;
278
322
AssertAreIdentical ( newHierarchy , Rep ( m_manualPrefab ) ) ;
279
- AssertAreIdentical ( newHierarchy , History ( m_manualPrefab ) ) ;
323
+ AssertAreIdentical ( newHistory , History ( m_manualPrefab ) ) ;
280
324
}
281
325
}
282
326
0 commit comments