Search Unity

Apply changes to an Asset (after changing its mesh)

Discussion in 'Editor & General Support' started by IgorAherne, Feb 26, 2020.

  1. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    I'd like to add UV2 to the mesh of imported asset ('planet_spherical_20kVerts', as seen on the image).

    Code (CSharp):
    1.  
    2.     //assigns a custom UV2 coordinate to every vertex of every child mesh.
    3.     void bake(){
    4.         if(_myAsset == null){ return; }
    5.  
    6.         List<MeshFilter> mfs = _myAsset.GetComponentsInChildren<MeshFilter>().ToList();
    7.         foreach( var mf in mfs){
    8.             Undo.RegisterFullObjectHierarchyUndo(mf, "undo mesh filter bake");
    9.             Vector3[] verts = mf.sharedMesh.vertices;
    10.             int len = verts.Length;
    11.             Vector4[] attribs = new Vector4[len];
    12.             System.Diagnostics.Debug.Assert(len % 3 == 0);
    13.  
    14.             for(int i=0; i<len; i+=3){
    15.                 attribs[i+0] = new Vector4(1,0,0,1);
    16.                 attribs[i+1] = new Vector4(0,1,0,1);
    17.                 attribs[i+2] = new Vector4(0,0,1,1);
    18.             }
    19.  
    20.             mf.sharedMesh.SetUVs(1, (Vector4[])attribs);
    21.             EditorUtility.SetDirty(mf);
    22.             EditorUtility.SetDirty(mf.sharedMesh);
    23.             EditorUtility.SetDirty(_myAsset);
    24.         }
    25.  
    26.         AssetDatabase.Refresh();
    27.         AssetDatabase.SaveAssets();
    28.     }//end()
    29. }
    30.  

    This correctly updates the shared meshes of this asset. However, the changes revert if I right-click and re-import this asset. The same happens if I exit the editor, and come back - the asset has the 'old' mesh.

    How can I "bake-in" these changes to the shared mesh of an asset, so this asset never reverts them?
    example.JPG
     
    Last edited: Feb 26, 2020