Skip to content

Commit 4f120b7

Browse files
committed
Uni-21697 call converttomodel from script
1 parent 13eea1d commit 4f120b7

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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,38 @@ 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="keepBackup">If set to <c>true</c> keep backup.</param>
77+
public static GameObject[] CreateInstantiatedModelPrefab (GameObject [] unityGameObjectsToConvert, string path = null, bool keepBackup = true)
6778
{
6879
List<GameObject> result = new List<GameObject> ();
6980

70-
var exportSet = ModelExporter.RemoveRedundantObjects (unityActiveGOs);
81+
var exportSet = ModelExporter.RemoveRedundantObjects (unityGameObjectsToConvert);
7182
GameObject[] gosToExport = new GameObject[exportSet.Count];
7283
exportSet.CopyTo (gosToExport);
7384

7485
EnforceUniqueNames (gosToExport);
7586

7687
// find common ancestor root & filePath;
7788
string[] filePaths = new string[gosToExport.Length];
78-
string dirPath = Path.Combine (Application.dataPath, "Objects");
89+
if (path==null)
90+
path = Path.Combine (Application.dataPath, "Objects");
7991

8092
for(int n = 0; n < gosToExport.Length; n++){
8193
var filename = ModelExporter.ConvertToValidFilename (gosToExport [n].name + ".fbx");
82-
var filePath = Path.Combine (dirPath, filename);
94+
var filePath = Path.Combine (path, filename);
8395
if (File.Exists (filePath)) {
84-
filePath = IncrementFileName (dirPath, filename);
96+
filePath = IncrementFileName (path, filename);
8597
}
8698
filePaths[n] = filePath;
8799
}
@@ -93,7 +105,6 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
93105
new UnityEngine.Object[] {gosToExport[j]}) as string;
94106
}
95107

96-
List<GameObject> selection = new List<GameObject> ();
97108
for(int i = 0; i < fbxFileNames.Length; i++)
98109
{
99110
var fbxFileName = fbxFileNames [i];
@@ -121,26 +132,26 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
121132
{
122133
SetupImportedGameObject (gosToExport [i], unityGO);
123134

124-
result.Add (unityGO);
125135

126136
// 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);
137+
if (!keepBackup) {
138+
Object.DestroyImmediate (unityGameObjectsToConvert [i]);
139+
}
140+
else
141+
{
142+
// rename and put under scene root in case we need to check values
143+
gosToExport [i].name = "_safe_to_delete_" + gosToExport [i].name;
144+
gosToExport [i].SetActive (false);
145+
}
146+
147+
// add the instanced Model Prefab
148+
result.Add (unityGO);
136149
}
137150
}
138151

139152
}
140153

141-
Selection.objects = selection.ToArray ();
142-
143-
return result;
154+
return result.ToArray ();
144155
}
145156

146157
/// <summary>

0 commit comments

Comments
 (0)