Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Unity 4: PrefabUtility.CreatePrefab not copying correctly?

Discussion in 'Editor & General Support' started by DoctorWhy, Nov 7, 2012.

  1. DoctorWhy

    DoctorWhy

    Joined:
    Jul 16, 2012
    Posts:
    23
    I have this simple code:

    Code (csharp):
    1. //go is defined as a GameObject above
    2. go = PrefabUtility.CreatePrefab(name, go);
    In 3.5.6, this works perfectly. However, in 4, when the prefab is created, the animation clips are not copied, and the SkinnedMeshRenderer isn't either. There might be others as well. Is this a known issue, or do I need to spend the time to create a test scene and report the bug?
     
  2. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,751
    Did you ever get a fix or work around for this as we are having the same problem here trying to create prefabs of skinned meshes?
     
  3. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    I have find workaround!

    Code (csharp):
    1. PrefabCreator.Create    ( assetPath, "Assets/Prefabs/Platforms/" + platform.name + ".prefab" );

    Code (csharp):
    1. private class PrefabCreator{
    2.     private PrefabCreator    ( String source, String destination )                        
    3.     {
    4.         _source                            = source;
    5.         _destination                    = destination;
    6.         EditorApplication.update        += CreatePrefab;
    7.     }
    8.  
    9.  
    10.     private        String        _source;
    11.     private        String        _destination;
    12.  
    13.  
    14.     public static    void    Create            ( String source, String destination )        
    15.     {
    16.         new PrefabCreator ( source, destination );
    17.     }
    18.     private            void    CreatePrefab    ( )                                            
    19.     {
    20.         var model                        = (GameObject)AssetDatabase.LoadAssetAtPath ( _source, typeof(GameObject) );
    21.  
    22.  
    23.         if( model == null )
    24.         {
    25.             Debug.Log        ( "[PrefabCreator] - CreatePrefab: Wait For Model at " + _source );
    26.             return;
    27.         }
    28.  
    29.  
    30.         var prefab                        = PrefabUtility.CreatePrefab ( _destination, model, ReplacePrefabOptions.ReplaceNameBased );
    31.         EditorUtility.SetDirty            ( prefab );
    32.         AssetDatabase.LoadAssetAtPath    ( AssetDatabase.GetAssetPath ( prefab.GetInstanceID ( ) ), typeof(GameObject) );
    33.  
    34.  
    35.         Debug.Log                        ( "[PrefabCreator] - CreatePrefab: PrefabCreated " + _destination, prefab );
    36.  
    37.  
    38.         EditorApplication.update        -= CreatePrefab;
    39.     }
    40. }