@@ -20,7 +20,7 @@ public class ConvertToModel : System.IDisposable
20
20
{
21
21
const string MenuItemName1 = "Assets/Convert To Model" ;
22
22
const string MenuItemName2 = "GameObject/Convert To Model" ;
23
-
23
+
24
24
/// <summary>
25
25
/// Clean up this class on garbage collection
26
26
/// </summary>
@@ -32,7 +32,7 @@ public void Dispose () { }
32
32
[ MenuItem ( MenuItemName1 , false ) ]
33
33
public static void OnMenuItem ( )
34
34
{
35
- OnConvertInPlace ( ) ;
35
+ OnConvertInPlace ( ) ;
36
36
}
37
37
38
38
/// <summary>
@@ -41,21 +41,21 @@ public static void OnMenuItem ()
41
41
[ MenuItem ( MenuItemName1 , true ) ]
42
42
public static bool OnValidateMenuItem ( )
43
43
{
44
- return true ;
44
+ return true ;
45
45
}
46
46
47
47
// Add a menu item called "Export Model..." to a GameObject's context menu.
48
48
[ MenuItem ( MenuItemName2 , false , 30 ) ]
49
49
static void OnContextItem ( MenuCommand command )
50
50
{
51
- OnConvertInPlace ( ) ;
51
+ OnConvertInPlace ( ) ;
52
52
}
53
53
54
54
private static List < GameObject > OnConvertInPlace ( )
55
55
{
56
56
List < GameObject > result = new List < GameObject > ( ) ;
57
57
58
- GameObject [ ] unityActiveGOs = Selection . GetFiltered < GameObject > ( SelectionMode . Editable | SelectionMode . TopLevel ) ;
58
+ GameObject [ ] unityActiveGOs = Selection . GetFiltered < GameObject > ( SelectionMode . Editable | SelectionMode . TopLevel ) ;
59
59
60
60
// find common ancestor root & filePath;
61
61
string filePath = "" ;
@@ -64,22 +64,22 @@ private static List<GameObject> OnConvertInPlace ()
64
64
GameObject unityCommonAncestor = null ;
65
65
int siblingIndex = - 1 ;
66
66
67
- foreach ( GameObject goObj in unityActiveGOs )
68
- {
67
+ foreach ( GameObject goObj in unityActiveGOs ) {
69
68
siblingIndex = goObj . transform . GetSiblingIndex ( ) ;
70
- unityCommonAncestor = goObj . transform . parent . gameObject ;
69
+ unityCommonAncestor = ( goObj . transform . parent != null ) ? goObj . transform . parent . gameObject : null ;
71
70
filePath = Path . Combine ( dirPath , goObj . name + ".fbx" ) ;
72
71
73
72
break ;
74
73
}
75
74
76
75
string fbxFileName = FbxExporters . Editor . ModelExporter . ExportObjects ( filePath , unityActiveGOs ) as string ;
77
76
78
- if ( fbxFileName != null )
77
+ if ( fbxFileName != null )
79
78
{
80
79
// make filepath relative to project folder
81
- if ( fbxFileName . StartsWith ( Application . dataPath , System . StringComparison . CurrentCulture ) ) {
82
- fbxFileName = "Assets" + fbxFileName . Substring ( Application . dataPath . Length ) ;
80
+ if ( fbxFileName . StartsWith ( Application . dataPath , System . StringComparison . CurrentCulture ) )
81
+ {
82
+ fbxFileName = "Assets" + fbxFileName . Substring ( Application . dataPath . Length ) ;
83
83
}
84
84
85
85
// refresh the assetdata base so that we can query for the model
@@ -89,38 +89,47 @@ private static List<GameObject> OnConvertInPlace ()
89
89
Object unityMainAsset = AssetDatabase . LoadMainAssetAtPath ( fbxFileName ) ;
90
90
91
91
if ( unityMainAsset != null ) {
92
- Transform unityParentTransform =
93
- ( unityCommonAncestor == null ) ? null : unityCommonAncestor . transform ;
92
+ Object unityObj = PrefabUtility . InstantiateAttachedAsset ( unityMainAsset ) ;
94
93
95
- Object unityObj = PrefabUtility . InstantiateAttachedAsset ( unityMainAsset ) ;
96
-
97
- if ( unityObj != null )
94
+ if ( unityObj != null )
98
95
{
99
96
GameObject unityGO = unityObj as GameObject ;
100
97
101
98
// configure name
102
99
const string cloneSuffix = "(Clone)" ;
103
100
104
- if ( unityGO . name . EndsWith ( cloneSuffix , System . StringComparison . CurrentCulture ) )
105
- {
106
- unityGO . name = unityGO . name . Remove ( cloneSuffix . Length - 1 ) ;
101
+ if ( unityGO . name . EndsWith ( cloneSuffix , System . StringComparison . CurrentCulture ) ) {
102
+ unityGO . name = unityGO . name . Remove ( cloneSuffix . Length - 1 ) ;
103
+ }
104
+
105
+ // configure transform and maintain local pose
106
+ if ( unityCommonAncestor != null ) {
107
+ unityGO . transform . SetParent ( unityCommonAncestor . transform , false ) ;
107
108
}
108
109
109
- // configure transform
110
- unityGO . transform . parent = unityParentTransform ;
111
110
unityGO . transform . SetSiblingIndex ( siblingIndex ) ;
112
111
113
112
result . Add ( unityObj as GameObject ) ;
114
113
115
114
// remove (now redundant) gameobjects
116
115
for ( int i = 0 ; i < unityActiveGOs . Length ; i ++ ) {
116
+ #if UNI_19965
117
+ Object . DestroyImmediate ( unityActiveGOs [ i ] ) ;
118
+ #else
119
+ // rename and put under scene root in case we need to check values
117
120
unityActiveGOs [ i ] . name = "_safe_to_delete_" + unityActiveGOs [ i ] . name ;
118
- // Object.DestroyImmediate(unityActiveGOs[i]);
121
+ unityActiveGOs [ i ] . transform . parent = null ;
122
+ unityActiveGOs [ i ] . SetActive ( false ) ;
123
+ #endif
119
124
}
125
+
126
+ // select the instanced Model Prefab
127
+ Selection . objects = new GameObject [ ] { unityGO } ;
120
128
}
121
129
}
122
130
123
131
}
132
+
124
133
return result ;
125
134
}
126
135
}
0 commit comments