Search Unity

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

Unity 4: Problem with PrefabUtility.CreatePrefab

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

  1. DoctorWhy

    DoctorWhy

    Joined:
    Jul 16, 2012
    Posts:
    23
    A line of code that worked perfectly in Unity 3.5.6, does not work in 4.

    Code (csharp):
    1. GameObject gObj = PrefabUtility.CreatePrefab(name, go);
    Basically, the prefab is not copying correctly. Anything that relates to the mesh, does not copy over. A reference in the SkinnedMeshRenderer, as well as my own script, become null. On top of that, the animation clips all become null.

    This is happening in the OnPostprocessModel.

    Is this a known issue and is there a work around?

    EDIT: After further investigation, this only happens when a prefab is created in OnPostprocessModel. I was able to reproduce it effectively in a clean project and created a bug report https://fogbugz.unity3d.com/default.asp?498132_a8unhv4c81qjeuni
     
    Last edited: Nov 9, 2012
  2. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,109
    I have the same problem in 4.2! :(

    Have you find solution, or workaround?
     
  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. }