unity下九宫格加载地形

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TerrainMgr : MonoBehaviour
{
    public static TerrainData terrainData;
    public static Dictionary<Vector2, TerrainData> datadic = new Dictionary<Vector2, TerrainData>();
    //全部9块地形
    public static float p = 0.01f;
    public static int wh;//513
    //简易对象池
    Queue<Terrain> terrainPool = new Queue<Terrain>();
    //显示中的地图
    public Dictionary<string, Terrain> dic = new Dictionary<string, Terrain>();
    //玩家
    GameObject player;
    Vector2 pos;//玩家坐标
    // Start is called before the first frame update
    void Start()
    {
        GameObject terrain = ABManager.Ins.Load<GameObject>("TerrainMgr", "Terrain", "Terrain");
        terrainData = terrain.GetComponent<Terrain>().terrainData;
        //把所有块存入对象池
        for (int i = 0; i < 9; i++)
        {
            terrainPool.Enqueue(Instantiate(terrain,transform).GetComponent<Terrain>());
        }
        //获取高度图宽高
        wh = terrain.GetComponent<Terrain>().terrainData.heightmapResolution;
        //初始化或移动后会收到消息
        MessageCenter.Ins.Add(MsgID.MOVE, Refresh);

        Refresh(null);
    }

    public void Refresh(object obj)
    {
        //获取玩家坐标
        player = UserData.Ins.player;
        if (player == null)
        {
            pos = Vector2.zero;
        }
        else
        {
            pos = new Vector2(player.transform.position.x, player.transform.position.z);
        }
        //计算中间快id 每块宽高128 锚点在左下角所以-64再除128后取整
        int x = (int)((pos.x - 64) / 128);
        int y = (int)((pos.y - 64) / 128);
        //获取所有显示的块的集合
        List<string> showlist = GetShowList(x, y);
        //不需要显示的集合
        List<string> unshowlist = new List<string>();
        //循环显示中的 判断在不在需要显示的里 不需要的存入集合
        foreach (var key in dic.Keys)
        {
            if (!showlist.Contains(key))
            {
                unshowlist.Add(key);
            }
        }
        //把所有不需要显示的隐藏 并存入对象池
        foreach (var key in unshowlist)
        {
            dic[key].gameObject.SetActive(false);
            terrainPool.Enqueue(dic[key]);
            dic.Remove(key);
        }
        //循环需要显示的 把没显示的创建出来
        foreach (var key in showlist)
        {
            if (!dic.ContainsKey(key))
            {
                dic.Add(key, terrainPool.Dequeue());
                dic[key].gameObject.SetActive(true);
                string[] ids = key.Split(',');
                int xid = int.Parse(ids[0]);
                int zid = int.Parse(ids[1]);
                dic[key].transform.position = new Vector3(xid * 128, 0, zid * 128);
                dic[key].GetComponent<SetTerrain>().xid = xid;
                dic[key].GetComponent<SetTerrain>().zid = zid;
                dic[key].GetComponent<SetTerrain>().Init();
            }
        }
        MessageCenter.Ins.Send(1007);
    }

    public List<string> GetShowList(int x, int y)
    {
        List<string> list = new List<string>();
        //存入中间块
        list.Add(x + "," + y);
        //上左 只要集合中没有 就把左上角3块都加进去
        if (Vector2.Distance(pos, new Vector2(x * 128, (y + 1) * 128)) <= 128)
        {
            if (!list.Contains((x - 1) + "," + (y + 1)))
            {
                list.Add((x - 1) + "," + (y + 1));
            }
            if (!list.Contains((x - 1) + "," + y))
            {
                list.Add((x - 1) + "," + y);
            }
            if (!list.Contains(x + "," + (y + 1)))
            {
                list.Add(x + "," + (y + 1));
            }
        }
        //上右
        if (Vector2.Distance(pos, new Vector2((x + 1) * 128, (y + 1) * 128)) <= 128)
        {
            if (!list.Contains(x + "," + (y + 1)))
            {
                list.Add(x + "," + (y + 1));
            }
            if (!list.Contains((x + 1) + "," + (y + 1)))
            {
                list.Add((x + 1) + "," + (y + 1));
            }
            if (!list.Contains((x + 1) + "," + y))
            {
                list.Add((x + 1) + "," + y);
            }
        }
        //下左
        if (Vector2.Distance(pos, new Vector2(x * 128, y * 128)) <= 128)
        {
            if (!list.Contains((x - 1) + "," + y))
            {
                list.Add((x - 1) + "," + y);
            }
            if (!list.Contains((x - 1) + "," + (y - 1)))
            {
                list.Add((x - 1) + "," + (y - 1));
            }
            if (!list.Contains(x + "," + (y - 1)))
            {
                list.Add(x + "," + (y - 1));
            }
        }
        //下右
        if (Vector2.Distance(pos, new Vector2((x + 1) * 128, y * 128)) <= 128)
        {
            if (!list.Contains(x + "," + (y - 1)))
            {
                list.Add(x + "," + (y - 1));
            }
            if (!list.Contains((x + 1) + "," + (y - 1)))
            {
                list.Add((x + 1) + "," + (y - 1));
            }
            if (!list.Contains((x + 1) + "," + y))
            {
                list.Add((x + 1) + "," + y);
            }
        }
        return list;
    }
}

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值