Search Unity

Serialized object

Discussion in 'Scripting' started by liszto, Sep 4, 2012.

  1. liszto

    liszto

    Joined:
    Dec 22, 2011
    Posts:
    135
    Hi,
    my last problem is this one :

    I've got an file (a prefab) and I want edit some variable in it.
    I want modify the ScaleInLightmap property and StaticEditorFlags property.
    I want modify the gameObject.isStatic to allow me bake lightmap in a custom editor window.
    The steps are :

    - Load assets in my scene dynamically. CHECK
    - Define if they need GenerateUVLightmap. CHECK
    - Pass them to static. Maybe but don't know
    - Define their new ScaleInLightmap. Don't work (SURE !)
    - Bake. CHECK (But with the bad scale all my work is screwed :/)
    - Move the lightmaps to a new folder. CHECK
    - Clean scene and reload it at its initial state. CHECK

    My tools is nearly over and after need to add the possibility to create your own baking scene and allow user to iterate automatically through all prefab he wants. But with the 2 buggy states left it's currently not possible.

    And this is what I do for those 2 fail steps.

    Code (csharp):
    1. //Force all game object in our scene as static
    2.     private void StaticAllTheScene()
    3.     {
    4.         //Iterate on all our gameobject in the scene and put them as static
    5.         GameObject[] goList = new GameObject[GameObject.FindSceneObjectsOfType( typeof( GameObject )).Length];
    6.         //Take all gameobject type and put them in da list
    7.         goList =  (GameObject[])GameObject.FindSceneObjectsOfType( typeof( GameObject ));
    8.  
    9.         Debug.Log("Real start of static  scale in lightmap...");
    10.         for( int i = 0 ; i < goList.Length ; i++ )
    11.         {
    12.             //Put all gameObject static field to true
    13.             foreach( Transform trans in goList[i].transform )
    14.             {
    15.                 Debug.Log("static before ?  " + trans.gameObject.isStatic );
    16.                 trans.gameObject.isStatic = true;
    17.                 Debug.Log("static after ?  " + trans.gameObject.isStatic );
    18.                 ScaleInLightmapAll( trans.gameObject );
    19.             }
    20.         }
    21.         Debug.Log("Static  scale in lightmap over");
    22.     }
    23.  
    24.     //Modify all scale of our object to a new one
    25.     private void ScaleInLightmapAll( GameObject go )
    26.     {
    27.         //Check if he got a renderer
    28.         if( go.renderer != null )
    29.         {
    30.             //Serialized my object to allow to modify every property in it
    31.             SerializedObject so = new SerializedObject ( go );
    32.  
    33.             Debug.Log( "Scale value : " + m_nScaleInLightmap );
    34.             Debug.Log( "Scale value in current object : " + so.FindProperty( "m_ScaleInLightmap" ).floatValue );
    35.  
    36.             //Find the property and modify them
    37.             so.FindProperty( "m_StaticEditorFlags" ).boolValue = true;
    38.             so.FindProperty( "m_ScaleInLightmap").floatValue = m_nScaleInLightmap;
    39.  
    40.             Debug.Log( "Scale value after : " + so.FindProperty( "m_ScaleInLightmap" ).floatValue );
    41.  
    42.        
    That's it. If someone got a solution on this problem, this can be very very usefull for me :eek:
    And for this part of my script :

    Code (csharp):
    1. so.FindProperty( "m_StaticEditorFlags" ).boolValue = true;
    2. so.FindProperty( "m_ScaleInLightmap").floatValue = m_nScaleInLightmap;
    Must I iterate on all property fields ? Or it does it alone ?

    Thanks in advance for your help !
     
  2. liszto

    liszto

    Joined:
    Dec 22, 2011
    Posts:
    135
    I finally find a solution to my problem and I share this with you if someone need it someday :

    Code (csharp):
    1.     private void ScaleInLightmapAll( GameObject go )
    2.     {
    3.        //Check if he got a renderer
    4.        if( go.renderer != null )
    5.        {
    6.          //Serialized my object to allow to modify every property in it
    7.          SerializedObject so = new SerializedObject ( go.GetComponent<Renderer>() );
    8.  
    9.          //Find the property and modify them
    10.          so.FindProperty( "m_ScaleInLightmap").floatValue = m_nScaleInLightmap;
    11.  
    12.          Debug.Log( "Scale value after : " + so.FindProperty( "m_ScaleInLightmap" ).floatValue );
    13.  
    14.          //Apply the modified properties
    15.          Debug.Log( "succes ?   " + so.ApplyModifiedProperties() ); //Send true but this is working ? oO
    16.        }
    17.     }
    For the iterate part, I don't know if it's needed for the moment, maybe someone can unconfirm or confirm that ?

    Thanks