Skip to content

Commit 50aa8fd

Browse files
authored
Merge pull request #42 from Unity-Technologies/Uni-21697-calling-converttomodel
Uni-21697 call converttomodel from script
2 parents 9bfde82 + 790699c commit 50aa8fd

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ***********************************************************************
1+
// ***********************************************************************
22
// Copyright (c) 2017 Unity Technologies. All rights reserved.
33
//
44
// Licensed under the ##LICENSENAME##.
@@ -32,8 +32,10 @@ public void Dispose () { }
3232
[MenuItem (MenuItemName1, false)]
3333
public static void OnMenuItem ()
3434
{
35-
GameObject [] unityActiveGOs = Selection.GetFiltered<GameObject> (SelectionMode.Editable | SelectionMode.TopLevel);
36-
OnConvertInPlace (unityActiveGOs);
35+
GameObject [] unityGameObjectsToConvert = Selection.GetFiltered<GameObject> (SelectionMode.Editable | SelectionMode.TopLevel);
36+
Object[] result = CreateInstantiatedModelPrefab (unityGameObjectsToConvert);
37+
if (result.Length>0)
38+
Selection.objects = result;
3739
}
3840

3941
/// <summary>
@@ -60,28 +62,39 @@ static void OnContextItem (MenuCommand command)
6062
Debug.LogError (string.Format("Error: {0} is not a GameObject and cannot be converted", command.context.name));
6163
return;
6264
}
63-
OnConvertInPlace (new GameObject[]{selected});
65+
GameObject[] result = CreateInstantiatedModelPrefab (new GameObject[]{selected});
66+
if (result.Length>0)
67+
Selection.objects = result;
68+
6469
}
6570

66-
private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
71+
/// <summary>
72+
/// Create an instantiated model prefab from an game object hierarchy.
73+
/// </summary>
74+
/// <returns>list of instanced Model Prefabs</returns>
75+
/// <param name="unityGameObjectsToConvert">Unity game objects to convert to Model Prefab instances</param>
76+
/// <param name="path">Path to save Model Prefab</param>
77+
/// <param name="keepOriginal">If set to <c>true</c> keep original gameobject hierarchy.</param>
78+
public static GameObject[] CreateInstantiatedModelPrefab (GameObject [] unityGameObjectsToConvert, string path = null, bool keepOriginal = true)
6779
{
6880
List<GameObject> result = new List<GameObject> ();
6981

70-
var exportSet = ModelExporter.RemoveRedundantObjects (unityActiveGOs);
82+
var exportSet = ModelExporter.RemoveRedundantObjects (unityGameObjectsToConvert);
7183
GameObject[] gosToExport = new GameObject[exportSet.Count];
7284
exportSet.CopyTo (gosToExport);
7385

7486
EnforceUniqueNames (gosToExport);
7587

7688
// find common ancestor root & filePath;
7789
string[] filePaths = new string[gosToExport.Length];
78-
string dirPath = FbxExporters.EditorTools.ExportSettings.instance.convertToModelSavePath;
90+
if (path==null)
91+
path = FbxExporters.EditorTools.ExportSettings.instance.convertToModelSavePath;
7992

8093
for(int n = 0; n < gosToExport.Length; n++){
8194
var filename = ModelExporter.ConvertToValidFilename (gosToExport [n].name + ".fbx");
82-
var filePath = Path.Combine (dirPath, filename);
95+
var filePath = Path.Combine (path, filename);
8396
if (File.Exists (filePath)) {
84-
filePath = IncrementFileName (dirPath, filename);
97+
filePath = IncrementFileName (path, filename);
8598
}
8699
filePaths[n] = filePath;
87100
}
@@ -93,7 +106,6 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
93106
new UnityEngine.Object[] {gosToExport[j]}) as string;
94107
}
95108

96-
List<GameObject> selection = new List<GameObject> ();
97109
for(int i = 0; i < fbxFileNames.Length; i++)
98110
{
99111
var fbxFileName = fbxFileNames [i];
@@ -121,26 +133,26 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
121133
{
122134
SetupImportedGameObject (gosToExport [i], unityGO);
123135

124-
result.Add (unityGO);
125136

126137
// remove (now redundant) gameobject
127-
#if UNI_19965
128-
Object.DestroyImmediate (unityActiveGOs [i]);
129-
#else
130-
// rename and put under scene root in case we need to check values
131-
gosToExport [i].name = "_safe_to_delete_" + gosToExport[i].name;
132-
gosToExport [i].SetActive (false);
133-
#endif
134-
// select the instanced Model Prefab
135-
selection.Add(unityGO);
138+
if (!keepOriginal) {
139+
Object.DestroyImmediate (unityGameObjectsToConvert [i]);
140+
}
141+
else
142+
{
143+
// rename and put under scene root in case we need to check values
144+
gosToExport [i].name = "_safe_to_delete_" + gosToExport [i].name;
145+
gosToExport [i].SetActive (false);
146+
}
147+
148+
// add the instanced Model Prefab
149+
result.Add (unityGO);
136150
}
137151
}
138152

139153
}
140154

141-
Selection.objects = selection.ToArray ();
142-
143-
return result;
155+
return result.ToArray ();
144156
}
145157

146158
/// <summary>
@@ -298,4 +310,4 @@ private static void CopyComponents(GameObject from, GameObject to){
298310
}
299311
}
300312
}
301-
}
313+
}

0 commit comments

Comments
 (0)