Search Unity

AssetDatabase unwrapping lightmap uv

Discussion in 'Asset Database' started by liszto, Sep 4, 2012.

  1. liszto

    liszto

    Joined:
    Dec 22, 2011
    Posts:
    135
    Hi, I want to know if this command :

    Unwrapping.GenerateSecondaryUVSet( mymesh );

    Automatically save something in the mesh meta ? or not ?

    Maybe I must use something like that :

    Code (csharp):
    1. string path = AssetDatabase.GetAssetPath( gameObject );
    2. ModelImporter model = AssetImporter.GetAtPath( path ) as ModelImporter;
    3.  
    4. model.generateSecondaryUV = true;
    5. Unwrapping.GenerateSecondaryUVSet( meshLightmapped );
    6.  
    7. AssetDatabase.ImportAsset( path );
    I never do something like that but I really need it :/. So if someone got an idea on this point

    Edit : I found a solution to do this, there it is :

    Code (csharp):
    1. //Recursive function switch all uv2 of mesh to true
    2.     private void GenerateLightMapUV2( GameObject gameObject )
    3.     {
    4.         MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
    5.         MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
    6.  
    7.         //Check if my mesh have and filter and a renderer so it's the final
    8.         if( meshFilter != null  meshRenderer != null )
    9.         {
    10.             //Take the mesh of the file
    11.             Mesh meshLightmapped = meshFilter.sharedMesh;
    12.  
    13.             //Take the file's path and put it in a string
    14.             string path = AssetDatabase.GetAssetPath( meshLightmapped );
    15.             //Define a modelImporter to modify its importing settings
    16.             ModelImporter model = AssetImporter.GetAtPath( path ) as ModelImporter;
    17.  
    18.             //Put the generateSecondaryUV field to true and GenerateUVSet for the mesh
    19.             model.generateSecondaryUV = true;
    20.             Unwrapping.GenerateSecondaryUVSet( meshLightmapped );
    21.  
    22.             //Update the mesh setting in editor
    23.             AssetDatabase.ImportAsset( path );
    24.         }
    25.             //If the previous mesh don't have both so go in children
    26.         foreach( Transform t in gameObject.transform )
    27.             GenerateLightMapUV2( t.gameObject );
    28.     }
     
    Last edited: Sep 4, 2012
    alexr1221 likes this.