当前位置:   article > 正文

简单实现无限地图_无限地图unity

无限地图unity

无限地图顾名思义就是永远也到达不了终点的地图。但并不意味着要不停的加载生成地图。

可以通过算法实现分块加载地图,并改变地图的位置来实现无限地图。

首先在场景中创建一个空对象。挂上此脚本。

 Player是主角,playerwh和terrainwh是玩家和地图的检查范围,prefab是地图预制体。

代码如下:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class TerrainMgr : MonoBehaviour
  5. {
  6. public GameObject player;
  7. public float playerWH;
  8. public float TerrainWH;
  9. public GameObject prefab;
  10. Vector3 playerpos;
  11. GameObject terrain;
  12. Dictionary<Vector2, GameObject> showDic = new Dictionary<Vector2, GameObject>();
  13. //对象池
  14. Queue<GameObject> pool = new Queue<GameObject>();
  15. void Start()
  16. {
  17. playerpos = player.transform.position;
  18. player.transform.position = new Vector3(0.1f, 0, 0);
  19. }
  20. // Update is called once per frame
  21. void Update()
  22. {
  23. if (playerpos != player.transform.position)
  24. {
  25. List<Vector2> showlist = new List<Vector2>();
  26. Rect playerRect = new Rect(player.transform.position.x, player.transform.position.z, playerWH, playerWH);
  27. int x = (int)(player.transform.position.x / TerrainWH);
  28. int z = (int)(player.transform.position.z / TerrainWH);
  29. showlist.Add(new Vector2(x, z));
  30. if (IsLap(playerRect, new Rect((x + 1) * TerrainWH, z * TerrainWH, TerrainWH, TerrainWH)))
  31. {
  32. showlist.Add(new Vector2(x + 1, z));
  33. }
  34. if (IsLap(playerRect, new Rect((x - 1) * TerrainWH, z * TerrainWH, TerrainWH, TerrainWH)))
  35. {
  36. showlist.Add(new Vector2(x - 1, z));
  37. }
  38. if (IsLap(playerRect, new Rect(x * TerrainWH, (z + 1) * TerrainWH, TerrainWH, TerrainWH)))
  39. {
  40. showlist.Add(new Vector2(x, z + 1));
  41. }
  42. if (IsLap(playerRect, new Rect(x * TerrainWH, (z - 1) * TerrainWH, TerrainWH, TerrainWH)))
  43. {
  44. showlist.Add(new Vector2(x, z - 1));
  45. }
  46. if (IsLap(playerRect, new Rect((x + 1) * TerrainWH, (z + 1) * TerrainWH, TerrainWH, TerrainWH)))
  47. {
  48. showlist.Add(new Vector2(x + 1, z + 1));
  49. }
  50. if (IsLap(playerRect, new Rect((x - 1) * TerrainWH, (z + 1) * TerrainWH, TerrainWH, TerrainWH)))
  51. {
  52. showlist.Add(new Vector2(x - 1, z + 1));
  53. }
  54. if (IsLap(playerRect, new Rect((x + 1) * TerrainWH, (z - 1) * TerrainWH, TerrainWH, TerrainWH)))
  55. {
  56. showlist.Add(new Vector2(x + 1, z - 1));
  57. }
  58. if (IsLap(playerRect, new Rect((x - 1) * TerrainWH, (z - 1) * TerrainWH, TerrainWH, TerrainWH)))
  59. {
  60. showlist.Add(new Vector2(x - 1, z - 1));
  61. }
  62. List<Vector2> deslist = new List<Vector2>();
  63. foreach (var item in showDic.Keys)
  64. {
  65. if (!showlist.Contains(item))
  66. {
  67. showDic[item].SetActive(false);
  68. pool.Enqueue(showDic[item]);
  69. deslist.Add(item);
  70. }
  71. }
  72. foreach (var item in deslist)
  73. {
  74. showDic.Remove(item);
  75. }
  76. foreach (var item in showlist)
  77. {
  78. if (!showDic.ContainsKey(item))
  79. {
  80. if (pool.Count>0)
  81. {
  82. terrain = pool.Dequeue();
  83. terrain.SetActive(true);
  84. }
  85. else
  86. {
  87. terrain = Instantiate(prefab);
  88. }
  89. terrain.transform.position = new Vector3(item.x * TerrainWH, 0, item.y * TerrainWH);
  90. showDic.Add(item, terrain);
  91. }
  92. }
  93. playerpos = player.transform.position;
  94. }
  95. }
  96. public bool IsLap(Rect a, Rect b)
  97. {
  98. float aMinX = a.x - a.width / 2;
  99. float aMaxX = a.x + a.width / 2;
  100. float aMinZ = a.y - a.height / 2;
  101. float aMaxZ = a.y + a.height / 2;
  102. float bMinX = b.x - b.width / 2;
  103. float bMaxX = b.x + b.width / 2;
  104. float bMinZ = b.y - b.height / 2;
  105. float bMaxZ = b.y + b.height / 2;
  106. if (aMinX < bMaxX &&
  107. bMinX < aMaxX &&
  108. aMinZ < bMaxZ &&
  109. bMinZ < aMaxZ
  110. )
  111. {
  112. return true;
  113. }
  114. else
  115. {
  116. return false;
  117. }
  118. }
  119. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号