Hi,
i try to make a chunksystem for my game, to place, remove and color cubes.
But my problem is that i can’t color each side from a normal cube. I tried to make a mesh but i don’t find out how to color spezefic areas.
Sorry for my bad english, i’m german^^
So i try that the player can place a cube on a 16*16 chunk. When the cube is placed the player should be able to color each side. like the front red, the back green etc.
I tried to make a cube out of planes, but that a way to much objects and couses into lags.
Maybe u have an idea? Please i try it now for over 3 days^^
My code for now: (I Already have a cube place system etc. This is a new try)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class World_Chunks : MonoBehaviour
{
public List<MP_Chunk> thisChunk = new List<MP_Chunk>();
void Start()
{
}
IEnumerator FloorkRender()
{
GameObject.DestroyImmediate(GetComponent<MeshCollider>());
GameObject.DestroyImmediate(GetComponent<MeshFilter>());
gameObject.AddComponent<MeshCollider>();
gameObject.AddComponent<MeshFilter>();
Mesh mesh = GetComponent<MeshFilter>().mesh;
List<Vector3> vertices = new List<Vector3>();
List<Vector2> uvs = new List<Vector2>();
List<int> triangles = new List<int>();
List<Color> colors = new List<Color>();
int vertexIndex;
vertexIndex = 0;
mesh.Clear();
for (int x = 0; x < World_ChunkLoader.instance.chunk_X_Width; x++)
{
for (int z = 0; z < World_ChunkLoader.instance.chunk_Z_Width; z++)
{
MP_Chunk tempchunk = new MP_Chunk();
tempchunk.x = x;
tempchunk.y = 0;
tempchunk.z = z;
tempchunk.isLoaded = false;
tempchunk.Render_Top = true;
tempchunk.Render_Bottom = false;
tempchunk.Render_Left = false;
tempchunk.Render_Right = false;
tempchunk.Render_Back = false;
tempchunk.Render_Front = false;
thisChunk.Add (tempchunk);
vertices.Add(new Vector3(x, 0 + 1, z));
vertices.Add(new Vector3(x, 0 + 1, z+1));
vertices.Add(new Vector3(x+1, 0 + 1, z+1));
vertices.Add(new Vector3(x+1, 0 + 1, z));
colors.Add (new Color(Random.Range (1,254), Random.Range (1,254), Random.Range (1,254)));
colors.Add (new Color(Random.Range (1,254), Random.Range (1,254), Random.Range (1,254)));
colors.Add (new Color(Random.Range (1,254), Random.Range (1,254), Random.Range (1,254)));
colors.Add (new Color(Random.Range (1,254), Random.Range (1,254), Random.Range (1,254)));
// first triangle for the block top
triangles.Add(vertexIndex);
triangles.Add(vertexIndex+1);
triangles.Add(vertexIndex+2);
// second triangle for the block top
triangles.Add(vertexIndex+2);
triangles.Add(vertexIndex+3);
triangles.Add(vertexIndex);
// add UV
uvs.Add(new Vector2(0, 0));
uvs.Add(new Vector2(0, 1));
uvs.Add(new Vector2(1, 1));
uvs.Add(new Vector2(1, 0));
vertexIndex += 4;
}
}
// Build the Mesh:
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.colors = colors.ToArray ();
mesh.uv = uvs.ToArray();
mesh.Optimize();
mesh.RecalculateNormals();
// update mesh collider
GetComponent<MeshCollider>().sharedMesh = null;
GetComponent<MeshCollider>().sharedMesh = mesh;
yield return null;
}
void Update()
{
if(Input.GetMouseButtonDown (0))
StartCoroutine(FloorkRender());
}
}
public class MP_Chunk
{
public int x;
public int y;
public int z;
public bool isLoaded;
public Transform ChunkObj;
public bool Render_Top;
public bool Render_Bottom;
public bool Render_Left;
public bool Render_Right;
public bool Render_Front;
public bool Render_Back;
}