2
2
using UnityEditor ;
3
3
using UnityEngine . TestTools ;
4
4
using NUnit . Framework ;
5
+ using System . Collections . Generic ;
5
6
6
7
namespace FbxExporters . UnitTests
7
8
{
@@ -17,7 +18,61 @@ public class FbxPrefabTest : ExporterTestBase
17
18
public static void AssertAreIdentical (
18
19
FbxPrefab . FbxRepresentation a ,
19
20
FbxPrefab . FbxRepresentation b ) {
20
- Assert . AreEqual ( a . ToJson ( ) , b . ToJson ( ) ) ;
21
+ // A bit of a laborious comparison scheme. This is due to the
22
+ // round-trip through FBX causing tiny errors in the transforms.
23
+ var astack = new List < FbxPrefab . FbxRepresentation > ( ) ;
24
+ astack . Add ( a ) ;
25
+ var bstack = new List < FbxPrefab . FbxRepresentation > ( ) ;
26
+ bstack . Add ( b ) ;
27
+
28
+ var aDummy = new GameObject ( "aDummy" ) . transform ;
29
+ var bDummy = new GameObject ( "bDummy" ) . transform ;
30
+ while ( astack . Count > 0 ) {
31
+ Assert . AreEqual ( astack . Count , bstack . Count ) ; // should never fail
32
+ a = astack [ astack . Count - 1 ] ; astack . RemoveAt ( astack . Count - 1 ) ;
33
+ b = bstack [ bstack . Count - 1 ] ; bstack . RemoveAt ( bstack . Count - 1 ) ;
34
+
35
+ // Verify that they have the same children (by name).
36
+ var achildren = a . ChildNames ;
37
+ var bchildren = b . ChildNames ;
38
+ Assert . That ( achildren , Is . EquivalentTo ( bchildren ) ) ;
39
+
40
+ // Add the children to each stack.
41
+ foreach ( var child in achildren ) {
42
+ astack . Add ( a . GetChild ( child ) ) ;
43
+ bstack . Add ( b . GetChild ( child ) ) ;
44
+ }
45
+
46
+ // Verify that they have the same components.
47
+ var atypes = a . ComponentTypes ;
48
+ var btypes = b . ComponentTypes ;
49
+ Assert . That ( atypes , Is . EquivalentTo ( btypes ) ) ;
50
+
51
+ foreach ( var t in atypes ) {
52
+ var avalues = a . GetComponentValues ( t ) ;
53
+ var bvalues = b . GetComponentValues ( t ) ;
54
+ Assert . AreEqual ( avalues . Count , bvalues . Count ) ;
55
+
56
+ if ( t != "UnityEngine.Transform" ) {
57
+ Assert . AreEqual ( avalues , bvalues ) ;
58
+ } else {
59
+ // Verify that the transforms are nearly (but don't require bitwise) equal.
60
+ EditorJsonUtility . FromJsonOverwrite ( avalues [ 0 ] , aDummy ) ;
61
+ EditorJsonUtility . FromJsonOverwrite ( bvalues [ 0 ] , bDummy ) ;
62
+ var dist = Vector3 . Distance ( aDummy . localPosition , bDummy . localPosition ) ;
63
+ Assert . That ( dist , Is . LessThan ( 1e-6 ) , ( ) => string . Format ( "position {0} vs {1} dist {2}" ,
64
+ aDummy . localPosition , bDummy . localPosition , dist ) ) ;
65
+
66
+ dist = Vector3 . Distance ( aDummy . localScale , bDummy . localScale ) ;
67
+ Assert . That ( dist , Is . LessThan ( 1e-6 ) , ( ) => string . Format ( "scale {0} vs {1} dist {2}" ,
68
+ aDummy . localScale , bDummy . localScale , dist ) ) ;
69
+
70
+ dist = Quaternion . Angle ( aDummy . localRotation , bDummy . localRotation ) ;
71
+ Assert . That ( dist , Is . LessThan ( 1e-6 ) , ( ) => string . Format ( "rotation {0} vs {1} angle {2}" ,
72
+ aDummy . localRotation . eulerAngles , bDummy . localRotation . eulerAngles , dist ) ) ;
73
+ }
74
+ }
75
+ }
21
76
}
22
77
23
78
public static void AssertAreDifferent (
@@ -72,7 +127,6 @@ FbxPrefab.FbxRepresentation History(GameObject go) {
72
127
73
128
GameObject ModifySourceFbx ( )
74
129
{
75
-
76
130
// Modify the source fbx file:
77
131
// - delete parent1
78
132
// - add parent3
@@ -101,6 +155,7 @@ public void BasicTest() {
101
155
AssertAreIdentical ( m_originalRep , History ( m_manualPrefab ) ) ;
102
156
AssertAreIdentical ( m_originalRep , History ( m_autoPrefab ) ) ;
103
157
158
+ Debug . Log ( "Testing auto update" ) ;
104
159
var newHierarchy = Rep ( ModifySourceFbx ( ) ) ;
105
160
AssertAreDifferent ( m_originalRep , newHierarchy ) ;
106
161
@@ -117,6 +172,7 @@ public void BasicTest() {
117
172
AssertAreIdentical ( m_originalRep , History ( m_manualPrefab ) ) ;
118
173
119
174
// Manual update, make sure it updated.
175
+ Debug . Log ( "Testing manual update" ) ;
120
176
var manualPrefabComponent = m_manualPrefab . GetComponent < FbxPrefab > ( ) ;
121
177
manualPrefabComponent . SyncPrefab ( ) ;
122
178
AssertAreIdentical ( newHierarchy , Rep ( m_manualPrefab ) ) ;
@@ -128,14 +184,17 @@ public void BasicTest() {
128
184
// Illegal to set the source model to something that isn't an
129
185
// asset.
130
186
var go = CreateGameObject ( "foo" ) ;
187
+ Debug . Log ( "Testing SetSourceModel to scene object" ) ;
131
188
Assert . That ( ( ) => manualPrefabComponent . SetSourceModel ( go ) , Throws . Exception ) ;
132
189
133
190
// Illegal to set the source model to something that isn't an fbx
134
191
// asset (it's a prefab).
192
+ Debug . Log ( "Testing SetSourceModel to prefab" ) ;
135
193
Assert . That ( ( ) => manualPrefabComponent . SetSourceModel ( m_autoPrefab ) , Throws . Exception ) ;
136
194
137
195
// Legal to set the source model to null. It doesn't change the
138
196
// hierarchy or anything.
197
+ Debug . Log ( "Testing SetSourceModel to null" ) ;
139
198
Assert . That ( ( ) => manualPrefabComponent . SetSourceModel ( null ) , Throws . Nothing ) ;
140
199
Assert . IsNull ( manualPrefabComponent . GetFbxAsset ( ) ) ;
141
200
AssertAreIdentical ( newHierarchy , Rep ( m_manualPrefab ) ) ;
@@ -151,6 +210,7 @@ public void BasicTest() {
151
210
GetRandomFbxFilePath ( ) , m_original ) ;
152
211
var newSource = AssetDatabase . LoadMainAssetAtPath ( fbxAsset ) as GameObject ;
153
212
Assert . IsTrue ( newSource ) ;
213
+ Debug . Log ( "Testing SetSourceModel relink" ) ;
154
214
manualPrefabComponent . SetSourceModel ( newSource ) ;
155
215
AssertAreIdentical ( m_originalRep , Rep ( m_manualPrefab ) ) ;
156
216
AssertAreIdentical ( m_originalRep , History ( m_manualPrefab ) ) ;
0 commit comments