Skip to content

Commit d32b6a1

Browse files
committed
check prefab root matches GO instead of if parent null
- this ensures that it is possible to convert a model instance parented under a different GO. - also added a comment explaining why
1 parent eceefdb commit d32b6a1

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,27 @@ public static GameObject Convert (
117117
string directoryFullPath = null,
118118
string fbxFullPath = null)
119119
{
120-
if(toConvert.transform.parent == null){
121-
PrefabType unityPrefabType = PrefabUtility.GetPrefabType(toConvert);
122-
if (unityPrefabType == PrefabType.ModelPrefabInstance) {
123-
// don't re-export fbx
124-
// create prefab out of model instance in scene, link to existing fbx
125-
var mainAsset = PrefabUtility.GetPrefabParent(toConvert) as GameObject;
126-
var mainAssetRelPath = AssetDatabase.GetAssetPath(mainAsset);
127-
var mainAssetAbsPath = Directory.GetParent(Application.dataPath) + "/" + mainAssetRelPath;
128-
SetupFbxPrefab(toConvert, mainAsset, mainAssetRelPath, mainAssetAbsPath);
129-
130-
return toConvert;
131-
}
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;
132141
}
133142

134143
if (string.IsNullOrEmpty(fbxFullPath)) {

0 commit comments

Comments
 (0)