Skip to content

Commit 2c7bef8

Browse files
author
Benoit Hudson
committed
Create unit tests and solve a crash revealed by the unit tests.
1 parent bbbd093 commit 2c7bef8

File tree

2 files changed

+151
-34
lines changed

2 files changed

+151
-34
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public static GameObject[] CreateInstantiatedModelPrefab (GameObject [] unityGam
181181
/// </summary>
182182
/// <returns>new file name.</returns>
183183
/// <param name="filename">Filename.</param>
184-
private static string IncrementFileName(string path, string filename)
184+
public static string IncrementFileName(string path, string filename)
185185
{
186186
string fileWithoutExt = Path.GetFileNameWithoutExtension (filename);
187187
string ext = Path.GetExtension (filename);
@@ -217,7 +217,7 @@ private static string IncrementFileName(string path, string filename)
217217
/// e.g. Sphere becomes Sphere 1
218218
/// </summary>
219219
/// <param name="exportSet">Export set.</param>
220-
private static void EnforceUniqueNames(GameObject[] exportSet)
220+
public static void EnforceUniqueNames(GameObject[] exportSet)
221221
{
222222
Dictionary<string, int> NameToIndexMap = new Dictionary<string, int> ();
223223
string format = "{0} {1}";
@@ -249,7 +249,7 @@ private static void EnforceUniqueNames(GameObject[] exportSet)
249249
/// </summary>
250250
/// <param name="orig">Original GameObject.</param>
251251
/// <param name="imported">Imported GameObject.</param>
252-
private static void SetupImportedGameObject(GameObject orig, GameObject imported)
252+
public static void SetupImportedGameObject(GameObject orig, GameObject imported)
253253
{
254254
Transform importedTransform = imported.transform;
255255
Transform origTransform = orig.transform;
@@ -258,16 +258,6 @@ private static void SetupImportedGameObject(GameObject orig, GameObject imported
258258
importedTransform.SetParent (origTransform.parent, false);
259259
importedTransform.SetSiblingIndex (origTransform.GetSiblingIndex());
260260

261-
// set the transform to be the same as before
262-
bool success = UnityEditorInternal.ComponentUtility.CopyComponent (origTransform);
263-
if (success) {
264-
success = UnityEditorInternal.ComponentUtility.PasteComponentValues(importedTransform);
265-
}
266-
if (!success) {
267-
Debug.LogWarning (string.Format ("Warning: Failed to copy component Transform from {0} to {1}",
268-
imported.name, origTransform.name));
269-
}
270-
271261
// copy the components over, assuming that the hierarchy order is unchanged
272262
if (origTransform.hierarchyCount != importedTransform.hierarchyCount) {
273263
Debug.LogWarning (string.Format ("Warning: Exported {0} objects, but only imported {1}",
@@ -280,7 +270,7 @@ private static void SetupImportedGameObject(GameObject orig, GameObject imported
280270
CopyComponentsRecursive (orig, imported, namesExpectedMatch:false);
281271
}
282272

283-
private static void FixSiblingOrder(Transform orig, Transform imported){
273+
public static void FixSiblingOrder(Transform orig, Transform imported){
284274
foreach (Transform origChild in orig) {
285275
Transform importedChild = imported.Find (origChild.name);
286276
if (importedChild == null) {
@@ -307,16 +297,16 @@ private static void CopyComponentsRecursive(GameObject from, GameObject to, bool
307297
}
308298
}
309299

310-
private static void CopyComponents(GameObject from, GameObject to){
300+
public static void CopyComponents(GameObject from, GameObject to){
311301
var originalComponents = new List<Component>(to.GetComponents<Component> ());
312302
var components = from.GetComponents<Component> ();
313-
for(int i = 0; i < components.Length; i++){
314-
if(components[i] == null){
303+
for(int i = 0, n = components.Length; i < n; ++i) {
304+
if (!components [i]) {
315305
continue;
316306
}
317307

318-
bool success = UnityEditorInternal.ComponentUtility.CopyComponent (components[i]);
319-
if (success) {
308+
var json = EditorJsonUtility.ToJson(components[i]);
309+
{
320310
bool foundComponentOfType = false;
321311
for (int j = 0; j < originalComponents.Count; j++) {
322312
var toComponent = originalComponents [j];
@@ -327,42 +317,43 @@ private static void CopyComponents(GameObject from, GameObject to){
327317
originalComponents.RemoveAt (j);
328318
foundComponentOfType = true;
329319

330-
// Don't want to copy MeshFilter because then we will replace the
331-
// exported mesh with the old mesh.
320+
// Don't want to copy MeshFilter because then
321+
// we will replace the exported mesh with the
322+
// old mesh. TODO: just remove some entries
323+
// from the json.
332324
if (toComponent is MeshFilter) {
333325
break;
334-
}
335-
336-
// Don't want to copy materials over in case the materials are
337-
// embedded in another model.
338-
if (toComponent is SkinnedMeshRenderer) {
326+
} else if (toComponent is SkinnedMeshRenderer) {
327+
// Don't want to copy materials over in
328+
// case the materials are embedded in
329+
// another model.
339330
var skinnedMesh = toComponent as SkinnedMeshRenderer;
340331
var sharedMesh = skinnedMesh.sharedMesh;
341332
var sharedMats = skinnedMesh.sharedMaterials;
342-
success = UnityEditorInternal.ComponentUtility.PasteComponentValues (toComponent);
333+
EditorJsonUtility.FromJsonOverwrite(json, toComponent);
343334
skinnedMesh.sharedMesh = sharedMesh;
344335
skinnedMesh.sharedMaterials = sharedMats;
345336
} else if (toComponent is Renderer) {
337+
// Don't want to copy materials over in
338+
// case the materials are embedded in
339+
// another model.
346340
var renderer = toComponent as Renderer;
347341
var sharedMats = renderer.sharedMaterials;
348-
success = UnityEditorInternal.ComponentUtility.PasteComponentValues (toComponent);
342+
EditorJsonUtility.FromJsonOverwrite(json, toComponent);
349343
renderer.sharedMaterials = sharedMats;
350344
} else {
351-
success = UnityEditorInternal.ComponentUtility.PasteComponentValues (toComponent);
345+
EditorJsonUtility.FromJsonOverwrite(json, toComponent);
352346
}
353347
break;
354348
}
355349
}
356350

357351
// component was not part of the original components, so add it
358352
if (!foundComponentOfType) {
359-
success = UnityEditorInternal.ComponentUtility.PasteComponentAsNew (to);
353+
var toComponent = to.AddComponent(components[i].GetType());
354+
EditorJsonUtility.FromJsonOverwrite(json, toComponent);
360355
}
361356
}
362-
if (!success) {
363-
Debug.LogWarning (string.Format ("Warning: Failed to copy component {0} from {1} to {2}",
364-
components[i].GetType().Name, from.name, to.name));
365-
}
366357
}
367358
}
368359
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using UnityEngine.TestTools;
4+
using NUnit.Framework;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using FbxExporters.Editor;
8+
9+
namespace FbxExporters.UnitTests
10+
{
11+
public class ConvertToModelTest
12+
{
13+
public static List<string> ChildNames(Transform a) {
14+
var names = new List<string>();
15+
foreach(Transform child in a) {
16+
names.Add(child.name);
17+
}
18+
return names;
19+
}
20+
21+
[Test]
22+
public void TestStaticHelpers()
23+
{
24+
// Test IncrementFileName
25+
{
26+
var tempPath = Path.GetTempPath ();
27+
var basename = Path.GetFileNameWithoutExtension (Path.GetRandomFileName ());
28+
29+
var filename1 = basename + " 1.fbx";
30+
var filename2 = Path.Combine(tempPath, basename + " 2.fbx");
31+
Assert.AreEqual (filename2, ConvertToModel.IncrementFileName (tempPath, filename1));
32+
33+
filename1 = basename + "1.fbx";
34+
filename2 = Path.Combine(tempPath, basename + "2.fbx");
35+
Assert.AreEqual (filename2, ConvertToModel.IncrementFileName (tempPath, filename1));
36+
37+
filename1 = basename + "k.fbx";
38+
filename2 = Path.Combine(tempPath, basename + "k 1.fbx");
39+
Assert.AreEqual (filename2, ConvertToModel.IncrementFileName (tempPath, filename1));
40+
}
41+
42+
// Test EnforceUniqueNames
43+
{
44+
var a = new GameObject("a");
45+
var b = new GameObject("b");
46+
var a1 = new GameObject("a");
47+
var a2 = new GameObject("a");
48+
ConvertToModel.EnforceUniqueNames(new GameObject[] { a, b, a1, a2 });
49+
Assert.AreEqual("a", a.name);
50+
Assert.AreEqual("b", b.name);
51+
Assert.AreEqual("a 1", a1.name);
52+
Assert.AreEqual("a 2", a2.name);
53+
}
54+
55+
// Test FixSiblingOrder
56+
{
57+
var a = new GameObject("a").transform;
58+
var b = new GameObject("a").transform;
59+
60+
var a1 = new GameObject("a1").transform;
61+
var a2 = new GameObject("a2").transform;
62+
var a3 = new GameObject("a3").transform;
63+
64+
var b1 = new GameObject("a1").transform;
65+
var b2 = new GameObject("a2").transform;
66+
var b3 = new GameObject("a3").transform;
67+
68+
a1.parent = a;
69+
a2.parent = a;
70+
a3.parent = a;
71+
72+
b3.parent = b;
73+
b1.parent = b;
74+
b2.parent = b;
75+
76+
// Assert same set, different order.
77+
Assert.That(ChildNames(b), Is.EquivalentTo(ChildNames(a)));
78+
Assert.That(ChildNames(b), Is.Not.EqualTo(ChildNames(a)));
79+
80+
// Fix the sibling order. Now we have same set, same order!
81+
ConvertToModel.FixSiblingOrder(a, b);
82+
Assert.That(ChildNames(b), Is.EquivalentTo(ChildNames(a)));
83+
Assert.That(ChildNames(b), Is.EqualTo(ChildNames(a)));
84+
}
85+
86+
// Test CopyComponents
87+
if (false){
88+
var a = new GameObject("a");
89+
var b = new GameObject("b");
90+
a.AddComponent<BoxCollider>();
91+
a.transform.localPosition += new Vector3(1,2,3);
92+
Assert.IsFalse(b.GetComponent<BoxCollider>());
93+
Assert.AreEqual(Vector3.zero, b.transform.localPosition);
94+
ConvertToModel.CopyComponents(a, b);
95+
Assert.IsTrue(b.GetComponent<BoxCollider>());
96+
Assert.AreEqual(new Vector3(1,2,3), b.transform.localPosition);
97+
}
98+
99+
// Test SetupImportedGameObject. Very similar but recursive.
100+
{
101+
var a = new GameObject ("a");
102+
var a1 = new GameObject ("AA");
103+
var a2 = new GameObject ("BB");
104+
a2.transform.parent = a.transform;
105+
a1.transform.parent = a.transform; // out of alpha order!
106+
var b = new GameObject ("b");
107+
var b1 = new GameObject ("AA");
108+
var b2 = new GameObject ("BB");
109+
b1.transform.parent = b.transform;
110+
b2.transform.parent = b.transform; // in alpha order
111+
a.AddComponent<BoxCollider> ();
112+
a1.transform.localPosition = new Vector3 (1, 2, 3);
113+
114+
Assert.IsFalse (b.GetComponent<BoxCollider> ());
115+
Assert.AreEqual ("BB", b.transform.GetChild (1).name);
116+
Assert.AreEqual (Vector3.zero, b1.transform.localPosition);
117+
118+
ConvertToModel.SetupImportedGameObject (a, b);
119+
120+
Assert.IsTrue (b.GetComponent<BoxCollider> ());
121+
Assert.AreEqual ("AA", b.transform.GetChild (1).name);
122+
Assert.AreEqual (new Vector3 (1, 2, 3), b1.transform.localPosition);
123+
}
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)