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;
}
}
unity下九宫格加载地形
最新推荐文章于 2024-05-06 15:08:55 发布