MapLodCtrl.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. public int currentMapIndex = 0;
  10. public List<LodStayLayer> MapStayList = new List<LodStayLayer>();
  11. private void Awake()
  12. {
  13. cameraBirdSec.OnDistanceChange += OnCameraDistanceChange;
  14. }
  15. private void Start()
  16. {
  17. MapList[currentMapIndex].map.ShowMap();
  18. //cameraBirdSec.currentDistance = MapList[currentMapIndex].cameraDistanceMax;
  19. //OnCameraDistanceChange(cameraBirdSec.currentDistance);
  20. }
  21. private void OnDestroy()
  22. {
  23. cameraBirdSec.OnDistanceChange -= OnCameraDistanceChange;
  24. }
  25. private void OnCameraDistanceChange(float value)
  26. {
  27. if (currentMapIndex>0&&value < MapList[currentMapIndex].cameraDistanceMin)
  28. {
  29. MapList[currentMapIndex-1].map.ShowMap();
  30. MapList[currentMapIndex].map.Hide();
  31. currentMapIndex--;
  32. }
  33. if (currentMapIndex < MapList.Count - 1 && value > MapList[currentMapIndex].cameraDistanceMax)
  34. {
  35. MapList[currentMapIndex+1].map.ShowMap();
  36. MapList[currentMapIndex].map.Hide();
  37. currentMapIndex++;
  38. }
  39. var currentLayer = MapList[currentMapIndex];
  40. float distanceRange = (value-currentLayer.cameraDistanceMin)/(currentLayer.cameraDistanceMax-currentLayer.cameraDistanceMin);//27-3
  41. float minX =currentLayer.min_X_Range.x+Mathf.Abs(currentLayer.min_X_Range.x-currentLayer.max_X_Range.x)*distanceRange;
  42. float maxX =currentLayer.min_X_Range.y-Mathf.Abs(currentLayer.min_X_Range.y-currentLayer.max_X_Range.y)*distanceRange;
  43. float minY =currentLayer.min_Y_Range.y+Mathf.Abs(currentLayer.min_Y_Range.y-currentLayer.max_Y_Range.y)*distanceRange;
  44. float maxY =currentLayer.min_Y_Range.x-Mathf.Abs(currentLayer.min_Y_Range.x-currentLayer.max_Y_Range.x)*distanceRange;
  45. // Debug.Log($"dis:{value} disRange:{distanceRange} minX:{minX} maxX:{maxX} minY:{minY} maxY:{maxY}");
  46. cameraBirdSec.SetRange(minX,maxX,minY,maxY);
  47. for (int i = 0; i < MapStayList.Count; i++)
  48. {
  49. if (value >= MapStayList[i].cameraDistanceMin && value < MapStayList[i].cameraDistanceMax)
  50. {
  51. MapStayList[i].map.ShowMap();
  52. }
  53. else
  54. {
  55. MapStayList[i].map.Hide();
  56. }
  57. }
  58. }
  59. }
  60. [Serializable]
  61. public class LodLayer
  62. {
  63. public MapGenerator map;
  64. public float cameraDistanceMin;
  65. public float cameraDistanceMax;
  66. public Vector2 min_X_Range;//最短距离时候的X范围
  67. public Vector2 max_X_Range;//最大距离时候的X范围
  68. public Vector2 min_Y_Range;
  69. public Vector2 max_Y_Range;
  70. }
  71. [Serializable]
  72. public class LodStayLayer
  73. {
  74. public MapGenerator map;
  75. public float cameraDistanceMin;
  76. public float cameraDistanceMax;
  77. }