Skip to content

Commit 945d5c6

Browse files
committed
Merge branch 'master' into UNI-41969-restore-include-setting
2 parents e5a2d10 + 499ddca commit 945d5c6

File tree

4 files changed

+92
-80
lines changed

4 files changed

+92
-80
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: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Collections.Generic;
33
using UnityEngine;
44
using UnityEditor;
@@ -45,12 +45,23 @@ protected void SetGameObjectsToConvert(IEnumerable<GameObject> toConvert){
4545

4646
TransferAnimationSource = null;
4747
TransferAnimationDest = null;
48-
4948
if (ToExport.Length == 1) {
50-
m_prefabFileName = ToExport [0].name;
49+
var go = ModelExporter.GetGameObject (ToExport [0]);
50+
// check if the GameObject is a model instance, use as default filename and path if it is
51+
if(ConvertToModel.IsModelInstance(go)) {
52+
var mainAsset = PrefabUtility.GetPrefabParent (go) as GameObject;
53+
var mainAssetRelPath = AssetDatabase.GetAssetPath (mainAsset);
54+
// remove Assets/ from beginning of path
55+
mainAssetRelPath = mainAssetRelPath.Substring ("Assets".Length);
56+
57+
m_prefabFileName = System.IO.Path.GetFileNameWithoutExtension (mainAssetRelPath);
58+
ExportSettings.AddFbxSavePath (System.IO.Path.GetDirectoryName (mainAssetRelPath));
59+
}
60+
else{
61+
m_prefabFileName = ToExport [0].name;
62+
}
5163

5264
// if only one object selected, set transfer source/dest to this object
53-
var go = ModelExporter.GetGameObject (ToExport [0]);
5465
if (go) {
5566
TransferAnimationSource = go.transform;
5667
TransferAnimationDest = go.transform;
@@ -71,8 +82,18 @@ protected override void OnEnable ()
7182
m_prefabExtLabelWidth = m_fbxExtLabelStyle.CalcSize (new GUIContent (".prefab")).x;
7283
}
7384

74-
protected override void Export ()
85+
protected override bool Export ()
7586
{
87+
if (string.IsNullOrEmpty (m_exportFileName)) {
88+
Debug.LogError ("FbxExporter: Please specify an fbx filename");
89+
return false;
90+
}
91+
92+
if (string.IsNullOrEmpty (m_prefabFileName)) {
93+
Debug.LogError ("FbxExporter: Please specify a prefab filename");
94+
return false;
95+
}
96+
7697
var fbxDirPath = ExportSettings.GetFbxAbsoluteSavePath ();
7798
var fbxPath = System.IO.Path.Combine (fbxDirPath, m_exportFileName + ".fbx");
7899

@@ -81,20 +102,45 @@ protected override void Export ()
81102

82103
// check if file already exists, give a warning if it does
83104
if (!OverwriteExistingFile (fbxPath) || !OverwriteExistingFile (prefabPath)) {
84-
return;
105+
return false;
85106
}
86107

87108
if (ToExport == null) {
88109
Debug.LogError ("FbxExporter: missing object for conversion");
89-
return;
110+
return false;
90111
}
91112

92113
if (ToExport.Length == 1) {
93114
var go = ModelExporter.GetGameObject (ToExport [0]);
115+
116+
if (!OverwriteExistingFile (prefabPath)) {
117+
return false;
118+
}
119+
120+
// Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
121+
if(ConvertToModel.IsModelInstance(go)) {
122+
// don't re-export fbx
123+
// create prefab out of model instance in scene, link to existing fbx
124+
var mainAsset = PrefabUtility.GetPrefabParent(go) as GameObject;
125+
var mainAssetRelPath = AssetDatabase.GetAssetPath(mainAsset);
126+
var mainAssetAbsPath = System.IO.Directory.GetParent(Application.dataPath) + "/" + mainAssetRelPath;
127+
var relPrefabPath = ExportSettings.GetProjectRelativePath (prefabPath);
128+
129+
if (string.Equals(System.IO.Path.GetFullPath(fbxPath), System.IO.Path.GetFullPath(mainAssetAbsPath))) {
130+
ConvertToModel.SetupFbxPrefab(go, mainAsset, relPrefabPath, mainAssetAbsPath);
131+
return true;
132+
}
133+
}
134+
135+
// check if file already exists, give a warning if it does
136+
if (!OverwriteExistingFile (fbxPath)) {
137+
return false;
138+
}
139+
94140
ConvertToModel.Convert (
95141
go, fbxFullPath: fbxPath, prefabFullPath: prefabPath, exportOptions: ExportSettings.instance.convertToPrefabSettings.info
96142
);
97-
return;
143+
return true;
98144
}
99145

100146
foreach (var obj in ToExport) {
@@ -103,6 +149,7 @@ protected override void Export ()
103149
go, fbxDirectoryFullPath: fbxDirPath, prefabDirectoryFullPath: prefabDirPath, exportOptions: ExportSettings.instance.convertToPrefabSettings.info
104150
);
105151
}
152+
return true;
106153
}
107154

108155
protected override ExportOptionsSettingsSerializeBase SettingsObject

Assets/FbxExporters/Editor/ExportModelEditorWindow.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections;
1+
using System.Collections;
22
using System.Collections.Generic;
33
using UnityEngine;
44
using UnityEditor;
@@ -116,7 +116,7 @@ public void OnPresetSelectionChanged()
116116
this.Repaint ();
117117
}
118118

119-
protected abstract void Export ();
119+
protected abstract bool Export ();
120120

121121
/// <summary>
122122
/// Function to be used by derived classes to add custom UI between the file path selector and export options.
@@ -356,8 +356,9 @@ protected void OnGUI ()
356356
}
357357

358358
if (GUILayout.Button (ExportButtonName, GUILayout.Width(ExportButtonWidth))) {
359-
Export ();
360-
this.Close ();
359+
if (Export ()) {
360+
this.Close ();
361+
}
361362
}
362363
GUILayout.EndHorizontal ();
363364

@@ -528,12 +529,17 @@ protected virtual void RestoreSettings()
528529
}
529530
}
530531

531-
protected override void Export(){
532+
533+
protected override bool Export(){
534+
if (string.IsNullOrEmpty (m_exportFileName)) {
535+
Debug.LogError ("FbxExporter: Please specify an fbx filename");
536+
return false;
537+
532538
var folderPath = ExportSettings.GetFbxAbsoluteSavePath ();
533539
var filePath = System.IO.Path.Combine (folderPath, m_exportFileName + ".fbx");
534540

535541
if (!OverwriteExistingFile (filePath)) {
536-
return;
542+
return false;
537543
}
538544

539545
if (IsPlayableDirector) {
@@ -547,14 +553,15 @@ protected override void Export(){
547553
// refresh the asset database so that the file appears in the
548554
// asset folder view.
549555
AssetDatabase.Refresh ();
550-
return;
556+
return true;
551557
}
552558

553559
if (ModelExporter.ExportObjects (filePath, ToExport, SettingsObject, timelineAnim: m_isTimelineAnim) != null) {
554560
// refresh the asset database so that the file appears in the
555561
// asset folder view.
556562
AssetDatabase.Refresh ();
557563
}
564+
return true;
558565
}
559566

560567
#if UNITY_2018_1_OR_NEWER

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,8 @@ public class ModelExporter : System.IDisposable
8282
// from being passed to command, thus resulting in OnContextItem()
8383
// being called only once regardless of what is selected.
8484
const string MenuItemName = "GameObject/Export To FBX...";
85-
const string ModelOnlyMenuItemName = "GameObject/Export Model Only...";
8685

87-
const string ClipMenuItemName = "GameObject/Export All Recorded Animation Clips...";
88-
const string TimelineClipMenuItemName = "GameObject/Export Selected Timeline Clip...";
89-
90-
const string AnimOnlyMenuItemName = "GameObject/Export Animation Only...";
91-
92-
const string FileBaseName = "Untitled";
86+
const string TimelineClipMenuItemName = "GameObject/Export Selected Timeline Clip...";
9387

9488
const string ProgressBarTitle = "Fbx Export";
9589

@@ -3401,40 +3395,6 @@ public static bool OnValidateMenuItem ()
34013395
return true;
34023396
}
34033397

3404-
/// <summary>
3405-
/// Validate the menu item defined by the function ModelOnlyOnContextItem.
3406-
/// </summary>
3407-
[MenuItem (ModelOnlyMenuItemName, true, 30)]
3408-
public static bool ModelOnlyOnValidateMenuItem ()
3409-
{
3410-
return true;
3411-
}
3412-
3413-
/// <summary>
3414-
// Validate the menu item defined by the function above.
3415-
/// </summary>
3416-
[MenuItem (AnimOnlyMenuItemName, true, 30)]
3417-
public static bool OnValidateAnimOnlyMenuItem ()
3418-
{
3419-
Object[] selection = Selection.objects;
3420-
3421-
if (selection == null || selection.Length == 0)
3422-
{
3423-
return false;
3424-
}
3425-
3426-
foreach (Object obj in selection)
3427-
{
3428-
GameObject gameObj = obj as GameObject;
3429-
if (gameObj != null && (gameObj.GetComponent<Animation>() != null || gameObj.GetComponent<Animator>() != null))
3430-
{
3431-
return true;
3432-
}
3433-
}
3434-
3435-
return false;
3436-
}
3437-
34383398
public static void DisplayNoSelectionDialog()
34393399
{
34403400
UnityEditor.EditorUtility.DisplayDialog (
@@ -3885,18 +3845,7 @@ private static string MakeFileName (string basename = "test", string extension =
38853845
{
38863846
return basename + "." + extension;
38873847
}
3888-
3889-
3890-
private static string GetExportFilePath(string filenameSuggestion = ""){
3891-
var directory = string.IsNullOrEmpty (LastFilePath)
3892-
? Application.dataPath
3893-
: System.IO.Path.GetDirectoryName (LastFilePath);
3894-
3895-
var title = string.Format ("Export To FBX ({0})", FileBaseName);
3896-
3897-
return EditorUtility.SaveFilePanel (title, directory, filenameSuggestion, kFBXFileExtension);
3898-
}
3899-
3848+
39003849
private static void OnExport ()
39013850
{
39023851
GameObject [] selectedGOs = Selection.GetFiltered<GameObject> (SelectionMode.TopLevel);

0 commit comments

Comments
 (0)