MapLodCtrl.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class MapLodCtrl : MonoBehaviour
  6. {
  7. public CameraBirdSec cameraBirdSec;
  8. public List<LodLayer> MapList = new List<LodLayer>();
  9. private int currentMapIndex;
  10. public List<LodStayLayer> MapStayList = new List<LodStayLayer>();
  11. private void Awake()
  12. {
  13. cameraBirdSec.OnDistanceChange += OnCameraDistanceChange;
  14. }
  15. private void Start()
  16. {
  17. currentMapIndex = MapList.Count - 1;
  18. MapList[currentMapIndex].map.ShowMap();
  19. cameraBirdSec.detailLod = false;
  20. cameraBirdSec.currentDistance = MapList[currentMapIndex].cameraDistanceMax;
  21. SetCameraRange(cameraBirdSec.currentDistance);
  22. }
  23. private void OnDestroy()
  24. {
  25. cameraBirdSec.OnDistanceChange -= OnCameraDistanceChange;
  26. }
  27. private void OnCameraDistanceChange(float value)
  28. {
  29. if (currentMapIndex == 0)
  30. {
  31. if (value > MapList[currentMapIndex].cameraDistanceMax)
  32. {
  33. MapList[currentMapIndex+1].map.ShowMap();
  34. MapList[currentMapIndex].map.Hide();
  35. currentMapIndex++;
  36. cameraBirdSec.detailLod = false;
  37. cameraBirdSec.currentDistance = MapList[currentMapIndex].cameraDistanceMax;
  38. SetCameraRange(MapList[currentMapIndex].cameraDistanceMax);
  39. return;
  40. }
  41. SetCameraRange(value);
  42. for (int i = 0; i < MapStayList.Count; i++)
  43. {
  44. if (value >= MapStayList[i].cameraDistanceMin && value < MapStayList[i].cameraDistanceMax)
  45. {
  46. MapStayList[i].map.ShowMap();
  47. }
  48. else
  49. {
  50. MapStayList[i].map.Hide();
  51. }
  52. }
  53. }
  54. else
  55. {
  56. if (value>0)
  57. {
  58. MapList[currentMapIndex-1].map.ShowMap();
  59. MapList[currentMapIndex].map.Hide();
  60. currentMapIndex--;
  61. if (currentMapIndex == 0)
  62. {
  63. cameraBirdSec.detailLod = true;
  64. }
  65. }
  66. else
  67. {
  68. if (currentMapIndex >= MapList.Count - 1)
  69. {
  70. return;
  71. }
  72. MapList[currentMapIndex+1].map.ShowMap();
  73. MapList[currentMapIndex].map.Hide();
  74. currentMapIndex++;
  75. }
  76. cameraBirdSec.currentDistance = MapList[currentMapIndex].cameraDistanceMax;
  77. SetCameraRange(MapList[currentMapIndex].cameraDistanceMax);
  78. }
  79. }
  80. private void SetCameraRange(float cameraDistance)
  81. {
  82. var currentLayer = MapList[currentMapIndex];
  83. float distanceRange = (cameraDistance-currentLayer.cameraDistanceMin)/(currentLayer.cameraDistanceMax-currentLayer.cameraDistanceMin);
  84. float minX =currentLayer.min_X_Range.x+Mathf.Abs(currentLayer.min_X_Range.x-currentLayer.max_X_Range.x)*distanceRange;
  85. float maxX =currentLayer.min_X_Range.y-Mathf.Abs(currentLayer.min_X_Range.y-currentLayer.max_X_Range.y)*distanceRange;
  86. float minY =currentLayer.min_Y_Range.y+Mathf.Abs(currentLayer.min_Y_Range.y-currentLayer.max_Y_Range.y)*distanceRange;
  87. float maxY =currentLayer.min_Y_Range.x-Mathf.Abs(currentLayer.min_Y_Range.x-currentLayer.max_Y_Range.x)*distanceRange;
  88. cameraBirdSec.SetRange(minX,maxX,minY,maxY);
  89. }
  90. }
  91. [Serializable]
  92. public class LodLayer
  93. {
  94. public MapGenerator map;
  95. public float cameraDistanceMin;
  96. public float cameraDistanceMax;
  97. public Vector2 min_X_Range;//最短距离时候的X范围
  98. public Vector2 max_X_Range;//最大距离时候的X范围
  99. public Vector2 min_Y_Range;
  100. public Vector2 max_Y_Range;
  101. }
  102. [Serializable]
  103. public class LodStayLayer
  104. {
  105. public MapGenerator map;
  106. public float cameraDistanceMin;
  107. public float cameraDistanceMax;
  108. }