1
- // ***********************************************************************
1
+ // ***********************************************************************
2
2
// Copyright (c) 2017 Unity Technologies. All rights reserved.
3
3
//
4
4
// Licensed under the ##LICENSENAME##.
@@ -32,8 +32,10 @@ public void Dispose () { }
32
32
[ MenuItem ( MenuItemName1 , false ) ]
33
33
public static void OnMenuItem ( )
34
34
{
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 ;
37
39
}
38
40
39
41
/// <summary>
@@ -60,28 +62,39 @@ static void OnContextItem (MenuCommand command)
60
62
Debug . LogError ( string . Format ( "Error: {0} is not a GameObject and cannot be converted" , command . context . name ) ) ;
61
63
return ;
62
64
}
63
- OnConvertInPlace ( new GameObject [ ] { selected } ) ;
65
+ GameObject [ ] result = CreateInstantiatedModelPrefab ( new GameObject [ ] { selected } ) ;
66
+ if ( result . Length > 0 )
67
+ Selection . objects = result ;
68
+
64
69
}
65
70
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 )
67
79
{
68
80
List < GameObject > result = new List < GameObject > ( ) ;
69
81
70
- var exportSet = ModelExporter . RemoveRedundantObjects ( unityActiveGOs ) ;
82
+ var exportSet = ModelExporter . RemoveRedundantObjects ( unityGameObjectsToConvert ) ;
71
83
GameObject [ ] gosToExport = new GameObject [ exportSet . Count ] ;
72
84
exportSet . CopyTo ( gosToExport ) ;
73
85
74
86
EnforceUniqueNames ( gosToExport ) ;
75
87
76
88
// find common ancestor root & filePath;
77
89
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 ;
79
92
80
93
for ( int n = 0 ; n < gosToExport . Length ; n ++ ) {
81
94
var filename = ModelExporter . ConvertToValidFilename ( gosToExport [ n ] . name + ".fbx" ) ;
82
- var filePath = Path . Combine ( dirPath , filename ) ;
95
+ var filePath = Path . Combine ( path , filename ) ;
83
96
if ( File . Exists ( filePath ) ) {
84
- filePath = IncrementFileName ( dirPath , filename ) ;
97
+ filePath = IncrementFileName ( path , filename ) ;
85
98
}
86
99
filePaths [ n ] = filePath ;
87
100
}
@@ -93,7 +106,6 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
93
106
new UnityEngine . Object [ ] { gosToExport [ j ] } ) as string ;
94
107
}
95
108
96
- List < GameObject > selection = new List < GameObject > ( ) ;
97
109
for ( int i = 0 ; i < fbxFileNames . Length ; i ++ )
98
110
{
99
111
var fbxFileName = fbxFileNames [ i ] ;
@@ -121,26 +133,26 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
121
133
{
122
134
SetupImportedGameObject ( gosToExport [ i ] , unityGO ) ;
123
135
124
- result . Add ( unityGO ) ;
125
136
126
137
// 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 ) ;
136
150
}
137
151
}
138
152
139
153
}
140
154
141
- Selection . objects = selection . ToArray ( ) ;
142
-
143
- return result ;
155
+ return result . ToArray ( ) ;
144
156
}
145
157
146
158
/// <summary>
@@ -298,4 +310,4 @@ private static void CopyComponents(GameObject from, GameObject to){
298
310
}
299
311
}
300
312
}
301
- }
313
+ }
0 commit comments