MapLodCtrl.cs 4.1 KB

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