I have a Chunk based game like Minecraft and I have a instance of the script World. In this script World I have a array of chunks (WorldChunks) and inside the array I have an other array of Blocks (ChunkData). When I edit the Block Array It doesn't update the chunk array.
Here's my World script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Modded
{
namespace ChunkSystems
{
public class World
{
//World scale
public int twx;
public int twz;
//Chunks in world scale
public int tcx;
public int tcz;
//the chunks
public Chunk[,] WorldChunks;
//the textures
public Material WorldTexture;
//init the chunk and set all the variables
public World(int TWX, int TWZ, int tcx, int tcy, int tcz, bool renderAndInit, Material worldTexture)
{
WorldChunks = new Chunk[TWX, TWZ];
WorldTexture = worldTexture;
twx = TWX;
twz = TWZ;
this.tcx = tcx;
this.tcz = tcz;
for (int x = 0; x < TWX; x++)
{
for (int z = 0; z < TWZ; z++)
{
WorldChunks[x, z] = new Chunk(tcx, tcy, tcz, null);
}
}
if (renderAndInit)
{
Init();
RenderWorld();
}
}
//init the chunk.
public void Init()
{
for (int x = 0; x < twx; x++)
{
for (int z = 0; z < twz; z++)
{
GameObject NewChunk = new GameObject("Chunk" + x + "," + z, typeof(MeshFilter), typeof(MeshRenderer));
NewChunk.transform.position = new Vector3(x * tcx, 0f, z * tcz);
NewChunk.GetComponent().sharedMaterial = WorldTexture;
WorldChunks[x, z].myGameObject = NewChunk;
}
}
}
//render the chunk.
public void RenderWorld()
{
for (int x = 0; x < twx; x++)
{
for (int z = 0; z < twz; z++)
{
WorldChunks[x, z].RenderChunk();
}
}
}
}
}
}
And my Chunk script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Modded
{
namespace ChunkSystems
{
public class Chunk
{
//mesh variables
private List Triangles;
private List Vertexs;
private List UVs;
//chunk scale
private int tx;
private int ty;
private int tz;
//the object of the chunk.
public GameObject myGameObject;
public byte[,,] ChunkData;
//how many voxels are in chunk.
public int voxelsInChunk;
//MeshFilter
private MeshFilter filter;
//set variables of chunk.
public Chunk(int tx, int ty, int tz, GameObject reference)
{
this.tx = tx;
this.ty = ty;
this.tz = tz;
InitChunk(tx, ty, tz);
myGameObject = reference;
}
//render the chunk.
public void RenderChunk()
{
Vertexs.Clear();
Triangles.Clear();
UVs.Clear();
SetDefaultChunk();
CreateChunk(16, 16, 16);
}
//Init the chunk.
public void InitChunk(int tx, int ty, int tz)
{
ChunkData = new byte[tx + 2, ty + 2, tz + 2];
for (int x = 0; x < tx + 2; x++)
{
for (int y = 0; y < ty + 2; y++)
{
for (int z = 0; z < tz + 1; z++)
{
ChunkData[x, y, z] = 0;
}
}
}
Vertexs = new List();
Triangles = new List();
UVs = new List();
}
//set chunk in default settings.
public void SetDefaultChunk()
{
for (int x = 0; x < tx; x++)
{
for (int y = 0; y < ty; y++)
{
for (int z = 0; z < tz; z++)
{
ChangeBlock(new Vector3Int(x, y, z), 2);
}
}
}
}
//change a block in the chunk.
public void ChangeBlock(Vector3Int position, byte texture)
{
ChunkData[position.x + 1, position.y + 1, position.z + 1] = texture;
}
//change a block and render.
public void ChangeBlockAndRender(Vector3Int position, byte texture)
{
ChunkData[position.x + 1, position.y + 1, position.z + 1] = texture;
RenderChunk();
}
//create the variables of mesh.
private void CreateChunk(int xt, int yt, int zt)
{
voxelsInChunk = 0;
for (int x = 0; x < xt; x++)
{
for (int y = 0; y < yt; y++)
{
for (int z = 0; z < zt; z++)
{
if (!(ChunkData[x + 1, y + 1, z + 1] == 0))
{
if (ChunkData[x, y + 1, z + 1] == 0 || ChunkData[x + 2, y + 1, z + 1] == 0 || ChunkData[x + 1, y, z + 1] == 0 || ChunkData[x + 1, y + 2, z + 1] == 0 || ChunkData[x + 1, y + 1, z] == 0 || ChunkData[x + 1, y + 1, z + 2] == 0)
{
AddCubeToChunk(new Vector3(x, y, z), ChunkData[x + 1, y + 1, z + 1]);
}
}
}
}
}
CreateMesh();
}
//adding the voxels to variables.
private void AddCubeToChunk(Vector3 position, float texture)
{
for (int i = 0; i < 24; i++)
{
Vertexs.Add(BlockMeshData.BlockVerts[i] + position);
}
UVs.AddRange(BlockMeshData.GetUVs(texture));
for (int i = 0; i < 36; i++)
{
Triangles.Add(BlockMeshData.VoxelTriangles[i] + voxelsInChunk * 24);
}
voxelsInChunk++;
}
//creating the mesh and updating it.
private void CreateMesh()
{
filter = myGameObject.GetComponent();
Mesh mesh = new Mesh();
mesh.Clear();
mesh.name = "chunk";
mesh.vertices = Vertexs.ToArray();
mesh.triangles = Triangles.ToArray();
mesh.uv = UVs.ToArray();
mesh.RecalculateNormals();
if (!(filter.sharedMesh == null))
{
filter.sharedMesh.Clear();
}
filter.sharedMesh = mesh;
}
}
}
}
Thanks for your time!
↧