1
+ using UnityEngine ;
2
+ using UnityEditor ;
3
+ using NUnit . Framework ;
4
+ using System . Linq ;
5
+ using UnityEditor . Formats . Fbx . Exporter ;
6
+ using System . IO ;
7
+ using Autodesk . Fbx ;
8
+
9
+ namespace FbxExporter . UnitTests
10
+ {
11
+ /// <summary>
12
+ /// Unit tests for sample code included in documentation.
13
+ /// </summary>
14
+ public class SampleCodeTest
15
+ {
16
+ private const string k_tempFolderName = "_safe_to_delete" ;
17
+ private string m_tempFolder = Path . Combine ( Application . dataPath , k_tempFolderName ) ;
18
+
19
+ [ SetUp ]
20
+ public void Init ( )
21
+ {
22
+ Assert . That ( m_tempFolder , Does . Not . Exist ) ;
23
+ Directory . CreateDirectory ( m_tempFolder ) ;
24
+ }
25
+
26
+ [ TearDown ]
27
+ public void Term ( )
28
+ {
29
+ if ( Directory . Exists ( m_tempFolder ) )
30
+ {
31
+ Directory . Delete ( m_tempFolder , recursive : true ) ;
32
+ }
33
+ }
34
+
35
+ // Export GameObjects sample function
36
+ public static void ExportGameObjects ( Object [ ] objects )
37
+ {
38
+ string filePath = Path . Combine ( Application . dataPath , "MyGame.fbx" ) ;
39
+ ModelExporter . ExportObjects ( filePath , objects ) ;
40
+
41
+ // ModelExporter.ExportObject can be used instead of
42
+ // ModelExporter.ExportObjects to export a single game object
43
+ }
44
+
45
+ [ Test ]
46
+ public void TestExportGameObjectsSample ( )
47
+ {
48
+ var cube = GameObject . CreatePrimitive ( PrimitiveType . Cube ) ;
49
+ var sphere = GameObject . CreatePrimitive ( PrimitiveType . Sphere ) ;
50
+
51
+ ExportGameObjects ( new Object [ ] { cube , sphere } ) ;
52
+
53
+ var filename = "MyGame.fbx" ;
54
+ var exportPath = Path . Combine ( Application . dataPath , filename ) ;
55
+ Assert . That ( exportPath , Does . Exist ) ;
56
+
57
+ Object [ ] loaded = AssetDatabase . LoadAllAssetsAtPath ( "Assets/" + filename ) ;
58
+ Assert . That ( loaded , Is . Not . Null . Or . Empty ) ;
59
+ var loadedMeshes = ( from loadedObj in loaded where loadedObj as Mesh != null select loadedObj as Mesh ) . ToArray ( ) ;
60
+
61
+ Assert . AreEqual ( 2 , loadedMeshes . Length ) ;
62
+ foreach ( var mesh in loadedMeshes )
63
+ {
64
+ Assert . Greater ( mesh . triangles . Length , 0 ) ;
65
+ }
66
+
67
+ File . Delete ( exportPath ) ;
68
+ }
69
+
70
+ // Export scene sample
71
+ protected void ExportScene ( string fileName )
72
+ {
73
+ using ( FbxManager fbxManager = FbxManager . Create ( ) )
74
+ {
75
+ // configure IO settings.
76
+ fbxManager . SetIOSettings ( FbxIOSettings . Create ( fbxManager , Globals . IOSROOT ) ) ;
77
+
78
+ // Export the scene
79
+ using ( Autodesk . Fbx . FbxExporter exporter = Autodesk . Fbx . FbxExporter . Create ( fbxManager , "myExporter" ) )
80
+ {
81
+
82
+ // Initialize the exporter.
83
+ bool status = exporter . Initialize ( fileName , - 1 , fbxManager . GetIOSettings ( ) ) ;
84
+
85
+ // Create a new scene to export
86
+ FbxScene scene = FbxScene . Create ( fbxManager , "myScene" ) ;
87
+
88
+ // Export the scene to the file.
89
+ exporter . Export ( scene ) ;
90
+ }
91
+ }
92
+ }
93
+
94
+ [ Test ]
95
+ public void TestExportSceneSample ( )
96
+ {
97
+ var filename = "MyGame.fbx" ;
98
+ var exportPath = Path . Combine ( m_tempFolder , filename ) ;
99
+ Assert . That ( exportPath , Does . Not . Exist ) ;
100
+
101
+ ExportScene ( exportPath ) ;
102
+ Assert . That ( exportPath , Does . Exist ) ;
103
+ }
104
+
105
+ // Import scene sample
106
+ protected void ImportScene ( string fileName )
107
+ {
108
+ using ( FbxManager fbxManager = FbxManager . Create ( ) )
109
+ {
110
+ // configure IO settings.
111
+ fbxManager . SetIOSettings ( FbxIOSettings . Create ( fbxManager , Globals . IOSROOT ) ) ;
112
+
113
+ // Import the scene to make sure file is valid
114
+ using ( FbxImporter importer = FbxImporter . Create ( fbxManager , "myImporter" ) )
115
+ {
116
+
117
+ // Initialize the importer.
118
+ bool status = importer . Initialize ( fileName , - 1 , fbxManager . GetIOSettings ( ) ) ;
119
+
120
+ // Create a new scene so it can be populated by the imported file.
121
+ FbxScene scene = FbxScene . Create ( fbxManager , "myScene" ) ;
122
+
123
+ // Import the contents of the file into the scene.
124
+ importer . Import ( scene ) ;
125
+ }
126
+ }
127
+ }
128
+
129
+ [ Test ]
130
+ public void TestImportSceneSample ( )
131
+ {
132
+ var filename = "MyGame.fbx" ;
133
+ var exportPath = Path . Combine ( m_tempFolder , filename ) ;
134
+ Assert . That ( exportPath , Does . Not . Exist ) ;
135
+
136
+ ExportScene ( exportPath ) ;
137
+ Assert . That ( exportPath , Does . Exist ) ;
138
+
139
+ ImportScene ( exportPath ) ;
140
+ }
141
+ }
142
+ }
0 commit comments