@@ -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,38 @@ 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="keepBackup">If set to <c>true</c> keep backup.</param>
77
+ public static GameObject [ ] CreateInstantiatedModelPrefab ( GameObject [ ] unityGameObjectsToConvert , string path = null , bool keepBackup = true )
67
78
{
68
79
List < GameObject > result = new List < GameObject > ( ) ;
69
80
70
- var exportSet = ModelExporter . RemoveRedundantObjects ( unityActiveGOs ) ;
81
+ var exportSet = ModelExporter . RemoveRedundantObjects ( unityGameObjectsToConvert ) ;
71
82
GameObject [ ] gosToExport = new GameObject [ exportSet . Count ] ;
72
83
exportSet . CopyTo ( gosToExport ) ;
73
84
74
85
EnforceUniqueNames ( gosToExport ) ;
75
86
76
87
// find common ancestor root & filePath;
77
88
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" ) ;
79
91
80
92
for ( int n = 0 ; n < gosToExport . Length ; n ++ ) {
81
93
var filename = ModelExporter . ConvertToValidFilename ( gosToExport [ n ] . name + ".fbx" ) ;
82
- var filePath = Path . Combine ( dirPath , filename ) ;
94
+ var filePath = Path . Combine ( path , filename ) ;
83
95
if ( File . Exists ( filePath ) ) {
84
- filePath = IncrementFileName ( dirPath , filename ) ;
96
+ filePath = IncrementFileName ( path , filename ) ;
85
97
}
86
98
filePaths [ n ] = filePath ;
87
99
}
@@ -93,7 +105,6 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
93
105
new UnityEngine . Object [ ] { gosToExport [ j ] } ) as string ;
94
106
}
95
107
96
- List < GameObject > selection = new List < GameObject > ( ) ;
97
108
for ( int i = 0 ; i < fbxFileNames . Length ; i ++ )
98
109
{
99
110
var fbxFileName = fbxFileNames [ i ] ;
@@ -121,26 +132,26 @@ private static List<GameObject> OnConvertInPlace (GameObject [] unityActiveGOs)
121
132
{
122
133
SetupImportedGameObject ( gosToExport [ i ] , unityGO ) ;
123
134
124
- result . Add ( unityGO ) ;
125
135
126
136
// 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 ) ;
136
149
}
137
150
}
138
151
139
152
}
140
153
141
- Selection . objects = selection . ToArray ( ) ;
142
-
143
- return result ;
154
+ return result . ToArray ( ) ;
144
155
}
145
156
146
157
/// <summary>
0 commit comments