Skip to content

Commit c52c346

Browse files
committed
add unit test
1 parent 73602dc commit c52c346

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ public static GameObject[] CreateInstantiatedModelPrefab (
9292
// don't re-export fbx
9393
// create prefab out of model instance in scene, link to existing fbx
9494
var mainAsset = PrefabUtility.GetPrefabParent(go) as GameObject;
95-
var mainAssetRelPath = AssetDatabase.GetAssetOrScenePath(mainAsset);
95+
var mainAssetRelPath = AssetDatabase.GetAssetPath(mainAsset);
9696
var mainAssetAbsPath = Directory.GetParent(Application.dataPath) + "/" + mainAssetRelPath;
9797
SetupFbxPrefab(go, mainAsset, mainAssetRelPath, mainAssetAbsPath);
98+
99+
wasExported.Add(go);
98100
continue;
99101
}
100102
}
@@ -181,6 +183,13 @@ public static GameObject Convert (
181183
}
182184

183185

186+
/// <summary>
187+
/// Create the fbx prefab and connect it to the given fbx asset.
188+
/// </summary>
189+
/// <param name="toConvert">Hierarchy to convert.</param>
190+
/// <param name="unityMainAsset">Main asset in the FBX.</param>
191+
/// <param name="projectRelativePath">Fbx project relative path.</param>
192+
/// <param name="fbxFullPath">Fbx full path.</param>
184193
public static void SetupFbxPrefab(GameObject toConvert, GameObject unityMainAsset, string projectRelativePath, string fbxFullPath){
185194
// Set up the FbxPrefab component so it will auto-update.
186195
// Make sure to delete whatever FbxPrefab history we had.

Assets/FbxExporters/Editor/UnitTests/ConvertToModelTest.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,5 +256,61 @@ public void TestInstanceNameMatchesFilename()
256256

257257
Assert.AreEqual (Path.GetFileNameWithoutExtension (path), cube.name);
258258
}
259+
260+
[Test]
261+
public void TestConvertModelInstance()
262+
{
263+
// expected result: prefab is linked to originally exported
264+
// fbx and no new fbx is created.
265+
266+
// create hierarchy (Cube->Sphere)
267+
var cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
268+
var sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
269+
270+
sphere.transform.SetParent (cube.transform);
271+
272+
// export using regular model export
273+
var filename = GetRandomFileNamePath();
274+
GameObject fbxObj = ExportSelection (filename, cube);
275+
276+
// add back to scene
277+
GameObject fbxInstance = PrefabUtility.InstantiatePrefab (fbxObj) as GameObject;
278+
Assert.That (fbxInstance, Is.Not.Null);
279+
280+
// attach some components
281+
var rigidBody = fbxInstance.AddComponent<Rigidbody> ();
282+
Assert.That (rigidBody, Is.Not.Null);
283+
284+
var instanceSphere = fbxInstance.transform.GetChild (0);
285+
Assert.That (instanceSphere, Is.Not.Null);
286+
287+
var boxCollider = instanceSphere.gameObject.AddComponent<BoxCollider> ();
288+
Assert.That (boxCollider, Is.Not.Null);
289+
290+
// convert to prefab
291+
GameObject[] converted = ConvertToModel.CreateInstantiatedModelPrefab (new GameObject[]{ fbxInstance }, Path.GetDirectoryName(filename));
292+
293+
Assert.That (converted.Length, Is.EqualTo(1));
294+
Assert.That (converted [0], Is.EqualTo (fbxInstance));
295+
296+
// check meshes link to original fbx
297+
var prefabCubeMesh = fbxInstance.GetComponent<MeshFilter>().sharedMesh;
298+
Assert.That (prefabCubeMesh, Is.Not.Null);
299+
300+
var fbxObjAssetPath = AssetDatabase.GetAssetPath (fbxObj);
301+
302+
Assert.That (AssetDatabase.GetAssetPath (prefabCubeMesh), Is.EqualTo (fbxObjAssetPath));
303+
304+
var prefabSphere = fbxInstance.transform.GetChild (0);
305+
Assert.That (prefabSphere, Is.Not.Null);
306+
var prefabSphereMesh = prefabSphere.GetComponent<MeshFilter> ().sharedMesh;
307+
Assert.That (prefabSphere, Is.Not.Null);
308+
309+
Assert.That (AssetDatabase.GetAssetPath (prefabSphereMesh), Is.EqualTo (fbxObjAssetPath));
310+
311+
// check that components are still there
312+
Assert.That (fbxInstance.GetComponent<Rigidbody>(), Is.Not.Null);
313+
Assert.That (prefabSphere.GetComponent<BoxCollider> (), Is.Not.Null);
314+
}
259315
}
260316
}

Assets/FbxExporters/Editor/UnitTests/ExporterTestBase.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ protected virtual GameObject ExportSelection(params Object[] selected)
223223
{
224224
// export selected to a file, then return the root
225225
var filename = GetRandomFileNamePath();
226+
return ExportSelection (filename, selected);
227+
}
226228

229+
protected virtual GameObject ExportSelection(string filename, params Object[] selected){
227230
Debug.unityLogger.logEnabled = false;
228231
var fbxFileName = FbxExporters.Editor.ModelExporter.ExportObjects (filename, selected) as string;
229232
Debug.unityLogger.logEnabled = true;

0 commit comments

Comments
 (0)