Skip to content

Commit 24baf06

Browse files
author
Benoit Hudson
committed
Offer a setting to keep-original, default off, not in inspector.
Includes tests to make sure it worked. Slight API change.
1 parent f0a5c9d commit 24baf06

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ public static bool OnValidateMenuItem ()
6262
return true;
6363
}
6464

65+
/// <summary>
66+
/// After a convert-to-model, we normally delete the original object.
67+
/// That can be overridden.
68+
/// </summary>
69+
public enum KeepOriginal {
70+
Default, // Use the export settings.
71+
Keep, // Don't delete, just rename it.
72+
Delete, // Delete the original hierarchy.
73+
}
74+
75+
/// <summary>
76+
/// Gets the export settings.
77+
/// </summary>
78+
public static EditorTools.ExportSettings ExportSettings {
79+
get { return EditorTools.ExportSettings.instance; }
80+
}
81+
6582
/// <summary>
6683
/// Create instantiated model prefabs from a selection of objects.
6784
///
@@ -73,7 +90,10 @@ public static bool OnValidateMenuItem ()
7390
/// <param name="unityGameObjectsToConvert">Unity game objects to convert to Model Prefab instances</param>
7491
/// <param name="path">Path to save Model Prefab; use FbxExportSettings if null</param>
7592
/// <param name="keepOriginal">If set to <c>true</c> keep original gameobject hierarchy.</param>
76-
public static GameObject[] CreateInstantiatedModelPrefab (GameObject [] unityGameObjectsToConvert, string directoryFullPath = null, bool keepOriginal = true)
93+
public static GameObject[] CreateInstantiatedModelPrefab (
94+
GameObject [] unityGameObjectsToConvert,
95+
string directoryFullPath = null,
96+
KeepOriginal keepOriginal = KeepOriginal.Default)
7797
{
7898
if (directoryFullPath == null) {
7999
directoryFullPath = FbxExporters.EditorTools.ExportSettings.GetAbsoluteSavePath();
@@ -119,7 +139,7 @@ public static GameObject Convert (
119139
GameObject toConvert,
120140
string directoryFullPath = null,
121141
string fbxFullPath = null,
122-
bool keepOriginal = true)
142+
KeepOriginal keepOriginal = KeepOriginal.Default)
123143
{
124144
if (string.IsNullOrEmpty(fbxFullPath)) {
125145
// Generate a unique filename.
@@ -197,7 +217,20 @@ public static GameObject Convert (
197217
unityGO = PrefabUtility.ConnectGameObjectToPrefab(unityGO, prefab);
198218

199219
// Remove (now redundant) gameobject
200-
if (!keepOriginal) {
220+
bool actuallyKeepOriginal;
221+
switch(keepOriginal) {
222+
case KeepOriginal.Delete:
223+
actuallyKeepOriginal = false;
224+
break;
225+
case KeepOriginal.Keep:
226+
actuallyKeepOriginal = true;
227+
break;
228+
case KeepOriginal.Default:
229+
default:
230+
actuallyKeepOriginal = ExportSettings.keepOriginalAfterConvert;
231+
break;
232+
}
233+
if (!actuallyKeepOriginal) {
201234
Object.DestroyImmediate (toConvert);
202235
} else {
203236
// rename and put under scene root in case we need to check values

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ public class ExportSettings : ScriptableSingleton<ExportSettings>
125125
public bool mayaCompatibleNames;
126126
public bool centerObjects;
127127

128+
/// <summary>
129+
/// In Convert-to-model, by default we delete the original object.
130+
/// This option lets you override that.
131+
/// </summary>
132+
[HideInInspector]
133+
public bool keepOriginalAfterConvert;
134+
128135
[SerializeField]
129136
public UnityEngine.Object turntableScene;
130137

@@ -146,6 +153,7 @@ protected override void LoadDefaults()
146153
weldVertices = true;
147154
mayaCompatibleNames = true;
148155
centerObjects = true;
156+
keepOriginalAfterConvert = false;
149157
convertToModelSavePath = kDefaultSavePath;
150158
turntableScene = null;
151159
}

Assets/FbxExporters/Editor/UnitTests/ConvertToModelTest.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ public void BasicTest()
132132
// Create a cube.
133133
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
134134

135-
// Convert it to a prefab.
135+
// Convert it to a prefab -- but keep the cube.
136136
var cubePrefabInstance = ConvertToModel.Convert(cube,
137-
directoryFullPath: path, keepOriginal: true);
137+
directoryFullPath: path, keepOriginal: ConvertToModel.KeepOriginal.Keep);
138138

139139
// Make sure it's what we expect.
140140
Assert.That(cube); // we kept the original
@@ -167,9 +167,20 @@ public void BasicTest()
167167
Assert.AreEqual(Path.GetFullPath(path), Path.GetDirectoryName(assetFullPath));
168168

169169
// Convert it again, make sure there's only one FbxPrefab (see UNI-25528).
170+
// Also make sure we deleted.
170171
var cubePrefabInstance2 = ConvertToModel.Convert(cubePrefabInstance,
171-
directoryFullPath: path, keepOriginal: false);
172+
directoryFullPath: path, keepOriginal: ConvertToModel.KeepOriginal.Delete);
173+
Assert.IsFalse(cubePrefabInstance);
172174
Assert.That(cubePrefabInstance2.GetComponents<FbxPrefab>().Length, Is.EqualTo(1));
175+
176+
// Create another cube, make sure the export settings drive whether we keep the cube or not.
177+
var cubePrefabInstance3 = ConvertToModel.Convert(cube, directoryFullPath: path,
178+
keepOriginal: ConvertToModel.KeepOriginal.Default);
179+
if (ConvertToModel.ExportSettings.keepOriginalAfterConvert) {
180+
Assert.IsTrue(cube);
181+
} else {
182+
Assert.IsFalse(cube);
183+
}
173184
}
174185
}
175186
}

Assets/FbxExporters/Editor/UnitTests/FbxPrefabTest.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NUnit.Framework;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using FbxExporters.Editor;
78

89
namespace FbxExporters.UnitTests
910
{
@@ -152,7 +153,7 @@ public void Init() {
152153

153154
// Convert it to FBX. The asset file will be deleted automatically
154155
// on termination.
155-
var fbxAsset = FbxExporters.Editor.ModelExporter.ExportObject(
156+
var fbxAsset = ModelExporter.ExportObject(
156157
GetRandomFbxFilePath(), m_original);
157158
m_source = AssetDatabase.LoadMainAssetAtPath(fbxAsset) as GameObject;
158159
m_originalHistory = Rep(m_source);
@@ -210,7 +211,7 @@ GameObject ModifySourceFbx()
210211
// enough, so the asset database knows to reload it. I was getting
211212
// test failures otherwise.
212213
SleepForFileTimestamp();
213-
FbxExporters.Editor.ModelExporter.ExportObjects (
214+
ModelExporter.ExportObjects (
214215
AssetDatabase.GetAssetPath(m_source),
215216
new Object[] { newModel } );
216217
AssetDatabase.Refresh();
@@ -291,7 +292,7 @@ public void BasicTest() {
291292
// Switch to some other model, which looks like the original model
292293
// (but is a totally different file). This will cause an update
293294
// immediately.
294-
var fbxAsset = FbxExporters.Editor.ModelExporter.ExportObject(
295+
var fbxAsset = ModelExporter.ExportObject(
295296
GetRandomFbxFilePath(), m_original);
296297
var newSource = AssetDatabase.LoadMainAssetAtPath(fbxAsset) as GameObject;
297298
Assert.IsTrue(newSource);
@@ -351,13 +352,13 @@ public void TestCubeAtRoot()
351352
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
352353
cube.name = "Cube";
353354
var cubeAssetPath = GetRandomFbxFilePath();
354-
var autoPrefab = FbxExporters.Editor.ConvertToModel.Convert(cube,
355+
var autoPrefab = ConvertToModel.Convert(cube,
355356
fbxFullPath: cubeAssetPath);
356357
Assert.IsTrue(autoPrefab);
357358

358359
// Make a maya locator.
359360
var locator = new GameObject("Cube");
360-
var locatorAssetPath = FbxExporters.Editor.ModelExporter.ExportObject(
361+
var locatorAssetPath = ModelExporter.ExportObject(
361362
GetRandomFbxFilePath(), locator);
362363

363364
// Check the prefab has all the default stuff it should have.
@@ -405,8 +406,9 @@ public void TestTransformAndReparenting()
405406
building3.transform.parent = building2.transform;
406407
building3.transform.localScale = new Vector3(.5f, .5f, .5f);
407408

408-
var original = FbxExporters.Editor.ConvertToModel.Convert (root,
409-
fbxFullPath: GetRandomFbxFilePath (), keepOriginal: true);
409+
var original = ConvertToModel.Convert (root,
410+
fbxFullPath: GetRandomFbxFilePath (),
411+
keepOriginal: ConvertToModel.KeepOriginal.Keep);
410412

411413
// Make sure it's OK.
412414
Assert.That (original.transform.GetChild (0).name, Is.EqualTo ("building2"));
@@ -417,7 +419,7 @@ public void TestTransformAndReparenting()
417419
// Modify the hierarchy, export to a new FBX, then copy the FBX under the prefab.
418420
root.SetActive(true);
419421
building2.name = "building2_renamed";
420-
var newCopyPath = FbxExporters.Editor.ModelExporter.ExportObject(
422+
var newCopyPath = ModelExporter.ExportObject(
421423
GetRandomFbxFilePath(), root);
422424
SleepForFileTimestamp();
423425
System.IO.File.Copy(

0 commit comments

Comments
 (0)