Skip to content

Commit 2eb4a06

Browse files
committed
refactor if statement into function
for clarity and to remove duplicate code
1 parent 8585ce7 commit 2eb4a06

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,7 @@ public static GameObject Convert (
108108
)
109109
{
110110
// Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
111-
// Children of model prefab instances will also have "model prefab instance"
112-
// as their prefab type, so it is important that it is the root that is selected.
113-
//
114-
// e.g. If I have the following hierarchy:
115-
// Cube
116-
// -- Sphere
117-
//
118-
// Both the Cube and Sphere will have ModelPrefabInstance as their prefab type.
119-
// However, when selecting the Sphere to convert, we don't want to connect it to the
120-
// existing FBX but create a new FBX containing just the sphere.
121-
PrefabType unityPrefabType = PrefabUtility.GetPrefabType(toConvert);
122-
if (unityPrefabType == PrefabType.ModelPrefabInstance && toConvert.Equals(PrefabUtility.FindPrefabRoot(toConvert))) {
111+
if(IsModelInstance(toConvert)){
123112
// don't re-export fbx
124113
// create prefab out of model instance in scene, link to existing fbx
125114
var mainAsset = PrefabUtility.GetPrefabParent(toConvert) as GameObject;
@@ -223,6 +212,26 @@ public static void SetupFbxPrefab(GameObject toConvert, GameObject unityMainAsse
223212
}
224213
}
225214

215+
/// <summary>
216+
/// Determines if the given GameObject is a model instance.
217+
/// </summary>
218+
/// <returns><c>true</c> if go is a model instance; otherwise, <c>false</c>.</returns>
219+
/// <param name="go">Go.</param>
220+
public static bool IsModelInstance(GameObject go){
221+
// Children of model prefab instances will also have "model prefab instance"
222+
// as their prefab type, so it is important that it is the root that is selected.
223+
//
224+
// e.g. If I have the following hierarchy:
225+
// Cube
226+
// -- Sphere
227+
//
228+
// Both the Cube and Sphere will have ModelPrefabInstance as their prefab type.
229+
// However, when selecting the Sphere to convert, we don't want to connect it to the
230+
// existing FBX but create a new FBX containing just the sphere.
231+
PrefabType unityPrefabType = PrefabUtility.GetPrefabType(go);
232+
return unityPrefabType == PrefabType.ModelPrefabInstance && go.Equals (PrefabUtility.FindPrefabRoot (go));
233+
}
234+
226235
/// <summary>
227236
/// Check if the file exists, and if it does, then increment the name.
228237
/// e.g. if filename is Sphere.fbx and it already exists, change it to Sphere 1.fbx.

Assets/FbxExporters/Editor/ConvertToPrefabEditorWindow.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ protected void SetGameObjectsToConvert(IEnumerable<GameObject> toConvert){
3434
if (m_toConvert.Length == 1) {
3535
var go = m_toConvert [0];
3636
// check if the GameObject is a model instance, use as default filename and path if it is
37-
PrefabType unityPrefabType = PrefabUtility.GetPrefabType(go);
38-
if (unityPrefabType == PrefabType.ModelPrefabInstance && go.Equals (PrefabUtility.FindPrefabRoot (go))) {
37+
if(ConvertToModel.IsModelInstance(go)) {
3938
var mainAsset = PrefabUtility.GetPrefabParent (go) as GameObject;
4039
var mainAssetRelPath = AssetDatabase.GetAssetPath (mainAsset);
4140
// remove Assets/ from beginning of path
@@ -82,18 +81,7 @@ protected override void Export ()
8281
}
8382

8483
// Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
85-
// Children of model prefab instances will also have "model prefab instance"
86-
// as their prefab type, so it is important that it is the root that is selected.
87-
//
88-
// e.g. If I have the following hierarchy:
89-
// Cube
90-
// -- Sphere
91-
//
92-
// Both the Cube and Sphere will have ModelPrefabInstance as their prefab type.
93-
// However, when selecting the Sphere to convert, we don't want to connect it to the
94-
// existing FBX but create a new FBX containing just the sphere.
95-
PrefabType unityPrefabType = PrefabUtility.GetPrefabType(go);
96-
if (unityPrefabType == PrefabType.ModelPrefabInstance && go.Equals(PrefabUtility.FindPrefabRoot(go))) {
84+
if(ConvertToModel.IsModelInstance(go)) {
9785
// don't re-export fbx
9886
// create prefab out of model instance in scene, link to existing fbx
9987
var mainAsset = PrefabUtility.GetPrefabParent(go) as GameObject;

0 commit comments

Comments
 (0)