@@ -181,7 +181,7 @@ public static GameObject[] CreateInstantiatedModelPrefab (GameObject [] unityGam
181
181
/// </summary>
182
182
/// <returns>new file name.</returns>
183
183
/// <param name="filename">Filename.</param>
184
- private static string IncrementFileName ( string path , string filename )
184
+ public static string IncrementFileName ( string path , string filename )
185
185
{
186
186
string fileWithoutExt = Path . GetFileNameWithoutExtension ( filename ) ;
187
187
string ext = Path . GetExtension ( filename ) ;
@@ -217,7 +217,7 @@ private static string IncrementFileName(string path, string filename)
217
217
/// e.g. Sphere becomes Sphere 1
218
218
/// </summary>
219
219
/// <param name="exportSet">Export set.</param>
220
- private static void EnforceUniqueNames ( GameObject [ ] exportSet )
220
+ public static void EnforceUniqueNames ( GameObject [ ] exportSet )
221
221
{
222
222
Dictionary < string , int > NameToIndexMap = new Dictionary < string , int > ( ) ;
223
223
string format = "{0} {1}" ;
@@ -249,7 +249,7 @@ private static void EnforceUniqueNames(GameObject[] exportSet)
249
249
/// </summary>
250
250
/// <param name="orig">Original GameObject.</param>
251
251
/// <param name="imported">Imported GameObject.</param>
252
- private static void SetupImportedGameObject ( GameObject orig , GameObject imported )
252
+ public static void SetupImportedGameObject ( GameObject orig , GameObject imported )
253
253
{
254
254
Transform importedTransform = imported . transform ;
255
255
Transform origTransform = orig . transform ;
@@ -258,16 +258,6 @@ private static void SetupImportedGameObject(GameObject orig, GameObject imported
258
258
importedTransform . SetParent ( origTransform . parent , false ) ;
259
259
importedTransform . SetSiblingIndex ( origTransform . GetSiblingIndex ( ) ) ;
260
260
261
- // set the transform to be the same as before
262
- bool success = UnityEditorInternal . ComponentUtility . CopyComponent ( origTransform ) ;
263
- if ( success ) {
264
- success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( importedTransform ) ;
265
- }
266
- if ( ! success ) {
267
- Debug . LogWarning ( string . Format ( "Warning: Failed to copy component Transform from {0} to {1}" ,
268
- imported . name , origTransform . name ) ) ;
269
- }
270
-
271
261
// copy the components over, assuming that the hierarchy order is unchanged
272
262
if ( origTransform . hierarchyCount != importedTransform . hierarchyCount ) {
273
263
Debug . LogWarning ( string . Format ( "Warning: Exported {0} objects, but only imported {1}" ,
@@ -280,7 +270,7 @@ private static void SetupImportedGameObject(GameObject orig, GameObject imported
280
270
CopyComponentsRecursive ( orig , imported , namesExpectedMatch : false ) ;
281
271
}
282
272
283
- private static void FixSiblingOrder ( Transform orig , Transform imported ) {
273
+ public static void FixSiblingOrder ( Transform orig , Transform imported ) {
284
274
foreach ( Transform origChild in orig ) {
285
275
Transform importedChild = imported . Find ( origChild . name ) ;
286
276
if ( importedChild == null ) {
@@ -307,16 +297,16 @@ private static void CopyComponentsRecursive(GameObject from, GameObject to, bool
307
297
}
308
298
}
309
299
310
- private static void CopyComponents ( GameObject from , GameObject to ) {
300
+ public static void CopyComponents ( GameObject from , GameObject to ) {
311
301
var originalComponents = new List < Component > ( to . GetComponents < Component > ( ) ) ;
312
302
var components = from . GetComponents < Component > ( ) ;
313
- for ( int i = 0 ; i < components . Length ; i ++ ) {
314
- if ( components [ i ] == null ) {
303
+ for ( int i = 0 , n = components . Length ; i < n ; ++ i ) {
304
+ if ( ! components [ i ] ) {
315
305
continue ;
316
306
}
317
307
318
- bool success = UnityEditorInternal . ComponentUtility . CopyComponent ( components [ i ] ) ;
319
- if ( success ) {
308
+ var json = EditorJsonUtility . ToJson ( components [ i ] ) ;
309
+ {
320
310
bool foundComponentOfType = false ;
321
311
for ( int j = 0 ; j < originalComponents . Count ; j ++ ) {
322
312
var toComponent = originalComponents [ j ] ;
@@ -327,42 +317,43 @@ private static void CopyComponents(GameObject from, GameObject to){
327
317
originalComponents . RemoveAt ( j ) ;
328
318
foundComponentOfType = true ;
329
319
330
- // Don't want to copy MeshFilter because then we will replace the
331
- // exported mesh with the old mesh.
320
+ // Don't want to copy MeshFilter because then
321
+ // we will replace the exported mesh with the
322
+ // old mesh. TODO: just remove some entries
323
+ // from the json.
332
324
if ( toComponent is MeshFilter ) {
333
325
break ;
334
- }
335
-
336
- // Don't want to copy materials over in case the materials are
337
- // embedded in another model.
338
- if ( toComponent is SkinnedMeshRenderer ) {
326
+ } else if ( toComponent is SkinnedMeshRenderer ) {
327
+ // Don't want to copy materials over in
328
+ // case the materials are embedded in
329
+ // another model.
339
330
var skinnedMesh = toComponent as SkinnedMeshRenderer ;
340
331
var sharedMesh = skinnedMesh . sharedMesh ;
341
332
var sharedMats = skinnedMesh . sharedMaterials ;
342
- success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
333
+ EditorJsonUtility . FromJsonOverwrite ( json , toComponent ) ;
343
334
skinnedMesh . sharedMesh = sharedMesh ;
344
335
skinnedMesh . sharedMaterials = sharedMats ;
345
336
} else if ( toComponent is Renderer ) {
337
+ // Don't want to copy materials over in
338
+ // case the materials are embedded in
339
+ // another model.
346
340
var renderer = toComponent as Renderer ;
347
341
var sharedMats = renderer . sharedMaterials ;
348
- success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
342
+ EditorJsonUtility . FromJsonOverwrite ( json , toComponent ) ;
349
343
renderer . sharedMaterials = sharedMats ;
350
344
} else {
351
- success = UnityEditorInternal . ComponentUtility . PasteComponentValues ( toComponent ) ;
345
+ EditorJsonUtility . FromJsonOverwrite ( json , toComponent ) ;
352
346
}
353
347
break ;
354
348
}
355
349
}
356
350
357
351
// component was not part of the original components, so add it
358
352
if ( ! foundComponentOfType ) {
359
- success = UnityEditorInternal . ComponentUtility . PasteComponentAsNew ( to ) ;
353
+ var toComponent = to . AddComponent ( components [ i ] . GetType ( ) ) ;
354
+ EditorJsonUtility . FromJsonOverwrite ( json , toComponent ) ;
360
355
}
361
356
}
362
- if ( ! success ) {
363
- Debug . LogWarning ( string . Format ( "Warning: Failed to copy component {0} from {1} to {2}" ,
364
- components [ i ] . GetType ( ) . Name , from . name , to . name ) ) ;
365
- }
366
357
}
367
358
}
368
359
}
0 commit comments