1
- //#define UNI_15935
2
1
// ***********************************************************************
3
2
// Copyright (c) 2017 Unity Technologies. All rights reserved.
4
3
//
@@ -31,7 +30,7 @@ public class ModelExporter : System.IDisposable
31
30
const string Comments =
32
31
@"" ;
33
32
34
- const string MenuItemName = "Assets/Export Model... %e " ;
33
+ const string MenuItemName = "Assets/Export Model..." ;
35
34
36
35
const string FileBaseName = "Untitled" ;
37
36
@@ -348,9 +347,6 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
348
347
}
349
348
}
350
349
351
- //
352
- // Create a simple user interface (menu items)
353
- //
354
350
/// <summary>
355
351
/// create menu item in the File menu
356
352
/// </summary>
@@ -360,13 +356,6 @@ public static void OnMenuItem ()
360
356
OnExport ( ) ;
361
357
}
362
358
363
- // Add a menu item called "Export Model..." to a GameObject's context menu.
364
- [ MenuItem ( "GameObject/Export Model... %e" , false , 30 ) ]
365
- static void OnContextItem ( MenuCommand command )
366
- {
367
- OnExport ( ) ;
368
- }
369
-
370
359
/// <summary>
371
360
// Validate the menu item defined by the function above.
372
361
/// </summary>
@@ -376,6 +365,13 @@ public static bool OnValidateMenuItem ()
376
365
return true ;
377
366
}
378
367
368
+ // Add a menu item called "Export Model..." to a GameObject's context menu.
369
+ [ MenuItem ( "GameObject/Export Model... %e" , false , 30 ) ]
370
+ static void OnContextItem ( MenuCommand command )
371
+ {
372
+ OnExport ( ) ;
373
+ }
374
+
379
375
//
380
376
// export mesh info from Unity
381
377
//
@@ -591,44 +587,60 @@ private static string MakeFileName(string basename = "test", string extension =
591
587
return basename + "." + extension ;
592
588
}
593
589
594
- // use the SaveFile panel to allow user to enter a file name
595
- private static void OnExport ( object objects = null )
590
+ private static void OnExport ( )
596
591
{
597
592
// Now that we know we have stuff to export, get the user-desired path.
598
593
var directory = string . IsNullOrEmpty ( LastFilePath )
599
- ? Application . dataPath
600
- : System . IO . Path . GetDirectoryName ( LastFilePath ) ;
594
+ ? Application . dataPath
595
+ : System . IO . Path . GetDirectoryName ( LastFilePath ) ;
601
596
602
597
var filename = string . IsNullOrEmpty ( LastFilePath )
603
- ? MakeFileName ( basename : FileBaseName , extension : Extension )
604
- : System . IO . Path . GetFileName ( LastFilePath ) ;
598
+ ? MakeFileName ( basename : FileBaseName , extension : Extension )
599
+ : System . IO . Path . GetFileName ( LastFilePath ) ;
605
600
606
601
var title = string . Format ( "Export Model FBX ({0})" , FileBaseName ) ;
607
602
608
603
var filePath = EditorUtility . SaveFilePanel ( title , directory , filename , "" ) ;
609
604
610
605
if ( string . IsNullOrEmpty ( filePath ) ) {
611
- return ;
606
+ return ;
607
+ }
608
+
609
+ if ( ExportObjects ( filePath ) != null )
610
+ {
611
+ // refresh the asset database so that the file appears in the
612
+ // asset folder view.
613
+ AssetDatabase . Refresh ( ) ;
612
614
}
615
+ }
613
616
617
+ /// <summary>
618
+ /// Export a list of (Game) objects to FBX file.
619
+ /// Use the SaveFile panel to allow user to enter a file name.
620
+ /// <summary>
621
+ public static object ExportObjects ( string filePath , UnityEngine . Object [ ] objects = null )
622
+ {
614
623
LastFilePath = filePath ;
615
624
616
625
using ( var fbxExporter = Create ( ) )
617
626
{
618
627
// ensure output directory exists
619
628
EnsureDirectory ( filePath ) ;
620
629
621
- if ( objects == null )
630
+ if ( objects == null )
622
631
{
623
632
objects = Selection . objects ;
624
633
}
625
634
626
- if ( fbxExporter . ExportAll ( objects as IEnumerable < UnityEngine . Object > ) > 0 )
635
+ if ( fbxExporter . ExportAll ( objects ) > 0 )
627
636
{
628
637
string message = string . Format ( "Successfully exported: {0}" , filePath ) ;
629
638
UnityEngine . Debug . Log ( message ) ;
639
+
640
+ return filePath ;
630
641
}
631
642
}
643
+ return null ;
632
644
}
633
645
634
646
private static void EnsureDirectory ( string path )
0 commit comments