I'm creating a 2d sandbox game (like terraria). Rather than use Unity's tilemap, I'm drawing rectangle textures to a mesh (on chunks).
public List<Vector3> newVertices = new List<Vector3>();
public List<int> newTriangles = new List<int>();
public List<Vector2> newUV = new List<Vector2>();
void Start () {
mesh = GetComponent<MeshFilter> ().mesh;
float x = transform.position.x;
float y = transform.position.y;
float z = transform.position.z;
newVertices.Add( new Vector3 (x , y , z ));
newVertices.Add( new Vector3 (x + 1 , y , z ));
newVertices.Add( new Vector3 (x + 1 , y-1 , z ));
newVertices.Add( new Vector3 (x , y-1 , z ));
newTriangles.AddRange(new int[]{
0, 1, 3, // Triangle 1
1, 2, 3 // Triangle 2
});
mesh.Clear ();
mesh.vertices = newVertices.ToArray();
mesh.triangles = newTriangles.ToArray();
mesh.Optimize ();
mesh.RecalculateNormals ();
}
public void AddSquareToMesh()
{
...
}
public void UpdateMesh()
{
...
}
I can't decide whether to:
- treat objects as tiles (and refresh the tilemesh 24fps when animating objects); or
- just use GameObjects for objects.
The problem with option (1) is refreshing a mesh at 24fps makes it flicker on low end PCs. The problem with (2) is there could be 16000 GameObjects constantly loading/unloading as the player moves around. Option (2) would use a pool manager and ECS, however that rules out WebGL.