@@ -117,6 +117,29 @@ public static GameObject Convert (
117
117
string directoryFullPath = null ,
118
118
string fbxFullPath = null )
119
119
{
120
+ // Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
121
+ // Children of model prefab instances will also have "model prefab instance"
122
+ // as their prefab type, so it is important that it is the root that is selected.
123
+ //
124
+ // e.g. If I have the following hierarchy:
125
+ // Cube
126
+ // -- Sphere
127
+ //
128
+ // Both the Cube and Sphere will have ModelPrefabInstance as their prefab type.
129
+ // However, when selecting the Sphere to convert, we don't want to connect it to the
130
+ // existing FBX but create a new FBX containing just the sphere.
131
+ PrefabType unityPrefabType = PrefabUtility . GetPrefabType ( toConvert ) ;
132
+ if ( unityPrefabType == PrefabType . ModelPrefabInstance && toConvert . Equals ( PrefabUtility . FindPrefabRoot ( toConvert ) ) ) {
133
+ // don't re-export fbx
134
+ // create prefab out of model instance in scene, link to existing fbx
135
+ var mainAsset = PrefabUtility . GetPrefabParent ( toConvert ) as GameObject ;
136
+ var mainAssetRelPath = AssetDatabase . GetAssetPath ( mainAsset ) ;
137
+ var mainAssetAbsPath = Directory . GetParent ( Application . dataPath ) + "/" + mainAssetRelPath ;
138
+ SetupFbxPrefab ( toConvert , mainAsset , mainAssetRelPath , mainAssetAbsPath ) ;
139
+
140
+ return toConvert ;
141
+ }
142
+
120
143
if ( string . IsNullOrEmpty ( fbxFullPath ) ) {
121
144
// Generate a unique filename.
122
145
if ( string . IsNullOrEmpty ( directoryFullPath ) ) {
@@ -155,12 +178,27 @@ public static GameObject Convert (
155
178
// relative to the project, not relative to the assets folder.
156
179
var unityMainAsset = AssetDatabase . LoadMainAssetAtPath ( projectRelativePath ) as GameObject ;
157
180
if ( ! unityMainAsset ) {
158
- throw new System . Exception ( "Failed to convert " + toConvert . name ) ; ;
181
+ throw new System . Exception ( "Failed to convert " + toConvert . name ) ;
159
182
}
160
183
161
184
// Copy the mesh/materials from the FBX
162
185
UpdateFromSourceRecursive ( toConvert , unityMainAsset ) ;
163
186
187
+ SetupFbxPrefab ( toConvert , unityMainAsset , projectRelativePath , fbxFullPath ) ;
188
+
189
+ toConvert . name = Path . GetFileNameWithoutExtension ( fbxFullPath ) ;
190
+ return toConvert ;
191
+ }
192
+
193
+
194
+ /// <summary>
195
+ /// Create the prefab and connect it to the given fbx asset.
196
+ /// </summary>
197
+ /// <param name="toConvert">Hierarchy to convert.</param>
198
+ /// <param name="unityMainAsset">Main asset in the FBX.</param>
199
+ /// <param name="projectRelativePath">Fbx project relative path.</param>
200
+ /// <param name="fbxFullPath">Fbx full path.</param>
201
+ public static void SetupFbxPrefab ( GameObject toConvert , GameObject unityMainAsset , string projectRelativePath , string fbxFullPath ) {
164
202
// Set up the FbxPrefab component so it will auto-update.
165
203
// Make sure to delete whatever FbxPrefab history we had.
166
204
var fbxPrefab = toConvert . GetComponent < FbxPrefab > ( ) ;
@@ -179,9 +217,6 @@ public static GameObject Convert (
179
217
string . Format ( "Failed to create prefab asset in [{0}] from fbx [{1}]" ,
180
218
prefabFileName , fbxFullPath ) ) ;
181
219
}
182
-
183
- toConvert . name = Path . GetFileNameWithoutExtension ( fbxFullPath ) ;
184
- return toConvert ;
185
220
}
186
221
187
222
/// <summary>
0 commit comments