adarsh
adarsh

Reputation: 142

How To Store Meshes in Game [Not in Editor mode]?

I have tried Assetdatabase and then I came to know that it works only in editor mode ... How can I store the Mesh now in playing mode and then I want to load it also??

var savePath1 = "Assets/" + saveMaterial + ".asset";
if (AssetDatabase.LoadAssetAtPath(savePath1, typeof(Mesh))) {
    AssetDatabase.DeleteAsset(savePath1);
    AssetDatabase.CreateAsset(pf.mesh, savePath1);
    //here pf is a reference of sphere(gameobject)   
}

Upvotes: 0

Views: 91

Answers (1)

zambari
zambari

Reputation: 5035

a Mesh is a relatively simple structure, for a single submesh you can serialize quite easily to json or other format in runtime

[System.Serializable]
public class MeshEquivalent
{
    public Vector3[] vertices;
    public Vector3[] normals;
    public Vector2[] uv;
    public int[] triangles;
    public MeshEquivalent(Mesh mesh)
    {
        vertices = mesh.vertices;
        uv = mesh.uv;
        normals = mesh.normals;
        triangles = mesh.triangles;
    }
}

There's also other uv channels (which you might not need), submeshes (same) and bounds which you can recalculate. The opposite process can be used to rebuild that mesh.

Upvotes: 1

Related Questions