@@ -57,22 +57,16 @@ private static List<GameObject> OnConvertInPlace ()
57
57
58
58
GameObject [ ] unityActiveGOs = Selection . GetFiltered < GameObject > ( SelectionMode . Editable | SelectionMode . TopLevel ) ;
59
59
60
- var exportSet = ModelExporter . RemoveDuplicateObjects ( unityActiveGOs ) ;
60
+ var exportSet = ModelExporter . RemoveRedundantObjects ( unityActiveGOs ) ;
61
61
GameObject [ ] gosToExport = new GameObject [ exportSet . Count ] ;
62
62
exportSet . CopyTo ( gosToExport ) ;
63
63
64
64
// find common ancestor root & filePath;
65
65
string [ ] filePaths = new string [ gosToExport . Length ] ;
66
66
string dirPath = Path . Combine ( Application . dataPath , "Objects" ) ;
67
67
68
- Transform [ ] unityCommonAncestors = new Transform [ gosToExport . Length ] ;
69
- int [ ] siblingIndices = new int [ gosToExport . Length ] ;
70
-
71
68
for ( int n = 0 ; n < gosToExport . Length ; n ++ ) {
72
- GameObject goObj = gosToExport [ n ] ;
73
- unityCommonAncestors [ n ] = goObj . transform . parent ;
74
- siblingIndices [ n ] = goObj . transform . GetSiblingIndex ( ) ;
75
- filePaths [ n ] = Path . Combine ( dirPath , goObj . name + ".fbx" ) ;
69
+ filePaths [ n ] = Path . Combine ( dirPath , gosToExport [ n ] . name + ".fbx" ) ;
76
70
}
77
71
78
72
string [ ] fbxFileNames = new string [ filePaths . Length ] ;
@@ -86,6 +80,10 @@ private static List<GameObject> OnConvertInPlace ()
86
80
for ( int i = 0 ; i < fbxFileNames . Length ; i ++ )
87
81
{
88
82
var fbxFileName = fbxFileNames [ i ] ;
83
+ if ( fbxFileName == null ) {
84
+ Debug . Log ( string . Format ( "Warning: Export failed for GameObject {0}" , gosToExport [ i ] . name ) ) ;
85
+ continue ;
86
+ }
89
87
90
88
// make filepath relative to project folder
91
89
if ( fbxFileName . StartsWith ( Application . dataPath , System . StringComparison . CurrentCulture ) )
@@ -105,18 +103,24 @@ private static List<GameObject> OnConvertInPlace ()
105
103
if ( unityObj != null )
106
104
{
107
105
GameObject unityGO = unityObj as GameObject ;
106
+ Transform unityGOTransform = unityGO . transform ;
107
+ Transform origGOTransform = gosToExport [ i ] . transform ;
108
108
109
- // configure name
110
- const string cloneSuffix = "(Clone)" ;
111
-
112
- if ( unityGO . name . EndsWith ( cloneSuffix , System . StringComparison . CurrentCulture ) ) {
113
- unityGO . name = unityGO . name . Remove ( cloneSuffix . Length - 1 ) ;
114
- }
109
+ // Set the name to be the name of the instantiated asset.
110
+ // This will get rid of the "(Clone)" if it's added
111
+ unityGO . name = unityMainAsset . name ;
115
112
116
113
// configure transform and maintain local pose
117
- unityGO . transform . SetParent ( unityCommonAncestors [ i ] , false ) ;
114
+ unityGOTransform . SetParent ( origGOTransform . parent , false ) ;
118
115
119
- unityGO . transform . SetSiblingIndex ( siblingIndices [ i ] ) ;
116
+ unityGOTransform . SetSiblingIndex ( origGOTransform . GetSiblingIndex ( ) ) ;
117
+
118
+ // copy the components over, assuming that the hierarchy order is unchanged
119
+ if ( origGOTransform . hierarchyCount != unityGOTransform . hierarchyCount ) {
120
+ Debug . LogWarning ( string . Format ( "Warning: Exported {0} objects, but only imported {1}" ,
121
+ origGOTransform . hierarchyCount , unityGOTransform . hierarchyCount ) ) ;
122
+ }
123
+ CopyComponentsRecursive ( gosToExport [ i ] , unityGO ) ;
120
124
121
125
result . Add ( unityObj as GameObject ) ;
122
126
@@ -139,6 +143,36 @@ private static List<GameObject> OnConvertInPlace ()
139
143
140
144
return result ;
141
145
}
146
+
147
+ private static void CopyComponentsRecursive ( GameObject from , GameObject to ) {
148
+ if ( ! to . name . StartsWith ( from . name ) || from . transform . childCount != to . transform . childCount ) {
149
+ Debug . LogError ( string . Format ( "Error: hierarchies don't match (From: {0}, To: {1})" , from . name , to . name ) ) ;
150
+ return ;
151
+ }
152
+
153
+ CopyComponents ( from , to ) ;
154
+ for ( int i = 0 ; i < from . transform . childCount ; i ++ ) {
155
+ CopyComponentsRecursive ( from . transform . GetChild ( i ) . gameObject , to . transform . GetChild ( i ) . gameObject ) ;
156
+ }
157
+ }
158
+
159
+ private static void CopyComponents ( GameObject from , GameObject to ) {
160
+ var components = from . GetComponents < Component > ( ) ;
161
+ for ( int i = 0 ; i < components . Length ; i ++ ) {
162
+ // if to already has this component, then skip it
163
+ if ( components [ i ] == null || to . GetComponent ( components [ i ] . GetType ( ) ) != null ) {
164
+ continue ;
165
+ }
166
+ bool success = UnityEditorInternal . ComponentUtility . CopyComponent ( components [ i ] ) ;
167
+ if ( success ) {
168
+ success = UnityEditorInternal . ComponentUtility . PasteComponentAsNew ( to ) ;
169
+ }
170
+ if ( ! success ) {
171
+ Debug . LogWarning ( string . Format ( "Warning: Failed to copy component {0} from {1} to {2}" ,
172
+ components [ i ] . GetType ( ) . Name , from . name , to . name ) ) ;
173
+ }
174
+ }
175
+ }
142
176
}
143
177
}
144
178
}
0 commit comments