1
+ // ***********************************************************************
2
+ // Copyright (c) 2017 Unity Technologies. All rights reserved.
3
+ //
4
+ // Licensed under the ##LICENSENAME##.
5
+ // See LICENSE.md file in the project root for full license information.
6
+ // ***********************************************************************
7
+
8
+ using System . IO ;
9
+ using System . Collections . Generic ;
10
+ using UnityEngine ;
11
+ using UnityEngine . SceneManagement ;
12
+ using UnityEditor ;
13
+ using FbxSdk ;
14
+
15
+ namespace FbxExporters
16
+ {
17
+ namespace Editor
18
+ {
19
+ public class ConvertToModel : System . IDisposable
20
+ {
21
+ const string MenuItemName1 = "Assets/Convert To Model" ;
22
+ const string MenuItemName2 = "GameObject/Convert To Model" ;
23
+
24
+ /// <summary>
25
+ /// Clean up this class on garbage collection
26
+ /// </summary>
27
+ public void Dispose ( ) { }
28
+
29
+ /// <summary>
30
+ /// create menu item in the File menu
31
+ /// </summary>
32
+ [ MenuItem ( MenuItemName1 , false ) ]
33
+ public static void OnMenuItem ( )
34
+ {
35
+ OnConvertInPlace ( ) ;
36
+ }
37
+
38
+ /// <summary>
39
+ // Validate the menu item defined by the function above.
40
+ /// </summary>
41
+ [ MenuItem ( MenuItemName1 , true ) ]
42
+ public static bool OnValidateMenuItem ( )
43
+ {
44
+ return true ;
45
+ }
46
+
47
+ // Add a menu item called "Export Model..." to a GameObject's context menu.
48
+ [ MenuItem ( MenuItemName2 , false , 30 ) ]
49
+ static void OnContextItem ( MenuCommand command )
50
+ {
51
+ OnConvertInPlace ( ) ;
52
+ }
53
+
54
+ private static List < GameObject > OnConvertInPlace ( )
55
+ {
56
+ List < GameObject > result = new List < GameObject > ( ) ;
57
+
58
+ GameObject [ ] unityActiveGOs = Selection . GetFiltered < GameObject > ( SelectionMode . Editable | SelectionMode . TopLevel ) ;
59
+
60
+ // find common ancestor root & filePath;
61
+ string filePath = "" ;
62
+ string dirPath = Path . Combine ( Application . dataPath , "Objects" ) ;
63
+
64
+ GameObject unityCommonAncestor = null ;
65
+ int siblingIndex = - 1 ;
66
+
67
+ foreach ( GameObject goObj in unityActiveGOs )
68
+ {
69
+ siblingIndex = goObj . transform . GetSiblingIndex ( ) ;
70
+ unityCommonAncestor = goObj . transform . parent . gameObject ;
71
+ filePath = Path . Combine ( dirPath , goObj . name + ".fbx" ) ;
72
+
73
+ break ;
74
+ }
75
+
76
+ string fbxFileName = FbxExporters . Editor . ModelExporter . ExportObjects ( filePath , unityActiveGOs ) as string ;
77
+
78
+ if ( fbxFileName != null )
79
+ {
80
+ // make filepath relative to project folder
81
+ if ( fbxFileName . StartsWith ( Application . dataPath , System . StringComparison . CurrentCulture ) ) {
82
+ fbxFileName = "Assets" + fbxFileName . Substring ( Application . dataPath . Length ) ;
83
+ }
84
+
85
+ // refresh the assetdata base so that we can query for the model
86
+ AssetDatabase . Refresh ( ) ;
87
+
88
+ // replace w Model asset
89
+ Object unityMainAsset = AssetDatabase . LoadMainAssetAtPath ( fbxFileName ) ;
90
+
91
+ if ( unityMainAsset != null ) {
92
+ Transform unityParentTransform =
93
+ ( unityCommonAncestor == null ) ? null : unityCommonAncestor . transform ;
94
+
95
+ Object unityObj = PrefabUtility . InstantiateAttachedAsset ( unityMainAsset ) ;
96
+
97
+ if ( unityObj != null )
98
+ {
99
+ GameObject unityGO = unityObj as GameObject ;
100
+
101
+ // configure name
102
+ const string cloneSuffix = "(Clone)" ;
103
+
104
+ if ( unityGO . name . EndsWith ( cloneSuffix , System . StringComparison . CurrentCulture ) )
105
+ {
106
+ unityGO . name = unityGO . name . Remove ( cloneSuffix . Length - 1 ) ;
107
+ }
108
+
109
+ // configure transform
110
+ unityGO . transform . parent = unityParentTransform ;
111
+ unityGO . transform . SetSiblingIndex ( siblingIndex ) ;
112
+
113
+ result . Add ( unityObj as GameObject ) ;
114
+
115
+ // remove (now redundant) gameobjects
116
+ for ( int i = 0 ; i < unityActiveGOs . Length ; i ++ ) {
117
+ unityActiveGOs [ i ] . name = "_safe_to_delete_" + unityActiveGOs [ i ] . name ;
118
+ // Object.DestroyImmediate(unityActiveGOs[i]);
119
+ }
120
+ }
121
+ }
122
+
123
+ }
124
+ return result ;
125
+ }
126
+ }
127
+ }
128
+ }
0 commit comments