MapLodCtrl.cs 2.8 KB

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