小编Bra*_*ssx的帖子

如何更快地加载/卸载图块块(C#)

好的,基本上在我的游戏中,世界是由图块组成的,图块被分成块(50x50 图块,图块为 16x16),因此整个世界不会加载到内存中。这些块被保存到文件中,并且仅加载需要的块(屏幕上的块和屏幕每一侧(顶部、左侧、底部、右侧)的 1 个块),其他块则被卸载等等。这一切都有效不过很好,当加载实际包含图块的块时,游戏逻辑会冻结相当多的时间(你知道加载文件吗?),基本上我的问题是如何使我当前的方法更快,所以几乎没有明显的滞后?

基本上每个块只包含一个 2D 字节数组,包含图块类型而不是实际的图块类,并且该字节数组保存到文件中以供稍后加载。我的块加载/保存功能:

保存:

    public void SaveChunk(int cx, int cy)
    {

        String chunkname = "";
        chunkname = WorldChunks[cx, cy].X + "_" + WorldChunks[cx, cy].Y;

        FileStream fileStream = File.Create(Main.ChunkPath + "\\Chunks\\" + chunkname + ".chnk");
        StreamWriter writer = new StreamWriter(fileStream);
        String json = JsonConvert.SerializeObject(WorldChunks[cx, cy].myTiles, Formatting.None);
        writer.Write(json);
        writer.Close();
    }
Run Code Online (Sandbox Code Playgroud)

加载中:

    public void LoadChunk(int xx, int yy)
    {

        string chunkname = xx + "_" + yy;
        if (File.Exists(Main.ChunkPath + "\\Chunks\\" + chunkname + ".chnk"))
        {
            //If the …
Run Code Online (Sandbox Code Playgroud)

c# performance tiles loading chunks

3
推荐指数
1
解决办法
5251
查看次数

标签 统计

c# ×1

chunks ×1

loading ×1

performance ×1

tiles ×1