minecraft chunkloading unity code example

Example 1: how to make a chunk loader in c#

public void LoadChunk(int xx, int yy)
    {

        string chunkname = xx + "_" + yy;
        if (File.Exists(Main.ChunkPath + "\\Chunks\\" + chunkname + ".chnk"))
        {
            //If the chunk is not loaded, Create a new chunk and begin loading it's tiles.
            if (WorldChunks[xx, yy] == null)
            {
                WorldChunks[xx, yy] = new Chunk(xx, yy);
            }

            System.Diagnostics.Debug.WriteLine("Loading chunk [" + xx + "," + yy + "]");
            Stream stream = File.Open(Main.ChunkPath + "\\Chunks\\" + chunkname + ".chnk", FileMode.Open);
            StreamReader reader = new StreamReader(stream);
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.TypeNameHandling = TypeNameHandling.Objects;
            WorldChunks[xx, yy].myTiles = JsonConvert.DeserializeObject<byte[,]>(reader.ReadToEnd(), settings);
            stream.Close();
            System.Diagnostics.Debug.WriteLine("Chunk loaded.. [" + xx + "," + yy + "]");
            int x, y;
            for (x = 0; x < Main.ChunkSizeX; x++)
            {
                for (y = 0; y < Main.ChunkSizeY; y++)
                {
                    byte _Type = WorldChunks[xx, yy].myTiles[x, y];
                    int tx = (xx * ChunkSizeX) + x;
                    int ty = (yy * ChunkSizeY) + y;
                    if (_Type > 0)
                    {
                        if (Tiles[tx, ty] == null && tx < MaxTilesX && ty < MaxTilesY)
                        {
                            Tiles[x + (xx * ChunkSizeX), (yy * ChunkSizeY) + y] = new Tile();
                            Tiles[(xx * ChunkSizeX) + x, (yy * ChunkSizeY) + y].Type = _Type;
                            if (ty > GROUND_LEVEL[tx] + 1)
                            {
                                //System.Diagnostics.Debug.Write("Below level:" + x + "|" + tx);
                                Tiles[tx, ty].HasBG = true;
                            }

                        }
                        else continue;
                    }

                }
            }

        }

    }

Example 2: how to make a chunk loader in c#

public void CheckChunks()
    {
        int StartX = (int)Math.Floor(ScreenPosition.X / (ChunkSizeX * 16)) - 2;
        int StartY = (int)Math.Floor(ScreenPosition.Y / (ChunkSizeY * 16)) - 2;

        if (StartX < 0) StartX = 0;
        if (StartY < 0) StartY = 0;

        int EndX = (int)Math.Floor((ScreenPosition.X + GraphicsDevice.Viewport.Width) / (ChunkSizeX * 16)) + 2;
        int EndY = (int)Math.Floor((ScreenPosition.Y + GraphicsDevice.Viewport.Height) / (ChunkSizeY * 16)) + 2;
        if (EndX > MaxChunksX) EndX = MaxChunksX;
        if (EndY > MaxChunksY) EndY = MaxChunksY;

        for (int x = StartX; x < EndX; x++)
        {
            for (int y = StartY; y < EndY; y++)
            {
                if (WorldChunks[x, y] == null)
                {

                    LoadChunk(x, y, true);

                    if (WorldChunks[x, y] != null)
                    {

                        LoadedChunks.Add(WorldChunks[x, y]);
                    }
                     break;
                }

            }
        }

            foreach (Chunk cc in LoadedChunks)
            {

                if (cc.X < StartX  || cc.Y < StartY || cc.X >EndX || cc.Y > EndY)
                {
                    System.Diagnostics.Debug.WriteLine("Chunk to be unloaded..");
                    ChunkBuffer.Add(cc);
                    UnloadChunk(cc);

                }
            }

            foreach (Chunk c in ChunkBuffer)
            {
                LoadedChunks.Remove(c);
            }
        ChunkBuffer.Clear();

    }