MapLodCtrl.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. cameraBirdSec.SetRange(MapList[currentMapIndex].max_X_Range.x,MapList[currentMapIndex].max_X_Range.y,MapList[currentMapIndex].max_Y_Range.x,MapList[currentMapIndex].max_Y_Range.y);
  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. cameraBirdSec.SetRange(MapList[currentMapIndex].max_X_Range.x,MapList[currentMapIndex].max_X_Range.y,MapList[currentMapIndex].max_Y_Range.x,MapList[currentMapIndex].max_Y_Range.y);
  39. return;
  40. }
  41. var currentLayer = MapList[currentMapIndex];
  42. float distanceRange = (value-currentLayer.cameraDistanceMin)/(currentLayer.cameraDistanceMax-currentLayer.cameraDistanceMin);
  43. float minX =currentLayer.min_X_Range.x+Mathf.Abs(currentLayer.min_X_Range.x-currentLayer.max_X_Range.x)*distanceRange;
  44. float maxX =currentLayer.min_X_Range.y-Mathf.Abs(currentLayer.min_X_Range.y-currentLayer.max_X_Range.y)*distanceRange;
  45. float minY =currentLayer.min_Y_Range.y+Mathf.Abs(currentLayer.min_Y_Range.y-currentLayer.max_Y_Range.y)*distanceRange;
  46. float maxY =currentLayer.min_Y_Range.x-Mathf.Abs(currentLayer.min_Y_Range.x-currentLayer.max_Y_Range.x)*distanceRange;
  47. cameraBirdSec.SetRange(minX,maxX,minY,maxY);
  48. for (int i = 0; i < MapStayList.Count; i++)
  49. {
  50. if (value >= MapStayList[i].cameraDistanceMin && value < MapStayList[i].cameraDistanceMax)
  51. {
  52. MapStayList[i].map.ShowMap();
  53. }
  54. else
  55. {
  56. MapStayList[i].map.Hide();
  57. }
  58. }
  59. }
  60. else
  61. {
  62. if (value>0)
  63. {
  64. MapList[currentMapIndex-1].map.ShowMap();
  65. MapList[currentMapIndex].map.Hide();
  66. currentMapIndex--;
  67. if (currentMapIndex == 0)
  68. {
  69. cameraBirdSec.detailLod = true;
  70. }
  71. }
  72. else
  73. {
  74. if (currentMapIndex >= MapList.Count - 1)
  75. {
  76. return;
  77. }
  78. MapList[currentMapIndex+1].map.ShowMap();
  79. MapList[currentMapIndex].map.Hide();
  80. currentMapIndex++;
  81. }
  82. cameraBirdSec.currentDistance = MapList[currentMapIndex].cameraDistanceMax;
  83. cameraBirdSec.SetRange(MapList[currentMapIndex].max_X_Range.x,MapList[currentMapIndex].max_X_Range.y,MapList[currentMapIndex].max_Y_Range.x,MapList[currentMapIndex].max_Y_Range.y);
  84. }
  85. }
  86. }
  87. [Serializable]
  88. public class LodLayer
  89. {
  90. public MapGenerator map;
  91. public float cameraDistanceMin;
  92. public float cameraDistanceMax;
  93. public Vector2 min_X_Range;//最短距离时候的X范围
  94. public Vector2 max_X_Range;//最大距离时候的X范围
  95. public Vector2 min_Y_Range;
  96. public Vector2 max_Y_Range;
  97. }
  98. [Serializable]
  99. public class LodStayLayer
  100. {
  101. public MapGenerator map;
  102. public float cameraDistanceMin;
  103. public float cameraDistanceMax;
  104. }