I need a way of constructing a mesh, without actually copying it, because I am going to edit that very mesh 100’s of times and each needs to be unique, where as if I just copy the mesh from one object to another it edits both of those meshes.
My idea so far was to copy the vertices of teh mesh and the triangles and uv data and sort of inmport it into a new mesh, would this work? I am getting errors if you think this is teh best way to go then by all means help me get past them, right now it is a null object error, instance not set to an object.
If however you think there is a better way to go please tell me.
//Instatiate a new cell in the given coordinates.
cell = Resources.Load(TerrainSystem.cellPath) as GameObject;
Vector3[] vertices = cell.GetComponent<MeshFilter>().sharedMesh.vertices;
int[] triangles = cell.GetComponent<MeshFilter>().sharedMesh.triangles;
Vector2[] uv = cell.GetComponent<MeshFilter>().sharedMesh.uv;
GameObject newCell = new GameObject();
newCell.AddComponent<MeshFilter>();
Mesh newMesh = newCell.GetComponent<MeshFilter>().sharedMesh;
newMesh.vertices = vertices;
newMesh.triangles = triangles;
newMesh.uv = uv;
newCell.GetComponent<MeshFilter>().sharedMesh = newMesh;
Also had the idea of if I edit a prefab it will not edit the parent this is my current code for that but it crashes at the moment, so not sure what is wrong there. it crashes due to teh loading a prefab line where it does resources.load
Vector2 newCellPosition = new Vector2((xPosition / cellSize), (yPosition / cellSize));
string newCellName = (cellName + " (" + newCellPosition.x + ", " + newCellPosition.y + ")");
cell = Resources.Load(TerrainSystem.cellPath) as GameObject;
//Once the paths are complete use them to store all the cell data.
if (!AssetDatabase.IsValidFolder(TerrainSystem.dataBasePrefix + TerrainSystem.worldFolder + "/" + newCellName))
{
PrefabUtility.CreatePrefab(TerrainSystem.dataBasePrefix + TerrainSystem.worldFolder + "/" + newCellName + ".prefab", cell);
}
//Instatiate a new cell in the given coordinates.
GameObject newCell = Resources.Load(TerrainSystem.worldFolder + "/" + newCellName) as GameObject;
Instantiate(newCell);