MapLodCtrl.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. private void Awake()
  11. {
  12. cameraBirdSec.OnDistanceChange += OnCameraDistanceChange;
  13. }
  14. private void Start()
  15. {
  16. MapList[currentMapIndex].map.ShowMap();
  17. }
  18. private void OnDestroy()
  19. {
  20. cameraBirdSec.OnDistanceChange -= OnCameraDistanceChange;
  21. }
  22. private void OnCameraDistanceChange(float value)
  23. {
  24. if (currentMapIndex>0&&value < MapList[currentMapIndex].cameraDistanceMin)
  25. {
  26. MapList[currentMapIndex-1].map.ShowMap();
  27. MapList[currentMapIndex].map.Hide();
  28. currentMapIndex--;
  29. }
  30. if (currentMapIndex < MapList.Count - 1 && value > MapList[currentMapIndex].cameraDistanceMax)
  31. {
  32. MapList[currentMapIndex+1].map.ShowMap();
  33. MapList[currentMapIndex].map.Hide();
  34. currentMapIndex++;
  35. }
  36. var currentLayer = MapList[currentMapIndex];
  37. float distanceRange = (value-currentLayer.cameraDistanceMin)/(currentLayer.cameraDistanceMax-currentLayer.cameraDistanceMin);//27-3
  38. float minX =currentLayer.min_X_Range.x+Mathf.Abs(currentLayer.min_X_Range.x-currentLayer.max_X_Range.x)*distanceRange;
  39. float maxX =currentLayer.min_X_Range.y-Mathf.Abs(currentLayer.min_X_Range.y-currentLayer.max_X_Range.y)*distanceRange;
  40. float minY =currentLayer.min_Y_Range.y+Mathf.Abs(currentLayer.min_Y_Range.y-currentLayer.max_Y_Range.y)*distanceRange;
  41. float maxY =currentLayer.min_Y_Range.x-Mathf.Abs(currentLayer.min_Y_Range.x-currentLayer.max_Y_Range.x)*distanceRange;
  42. // Debug.Log($"dis:{value} disRange:{distanceRange} minX:{minX} maxX:{maxX} minY:{minY} maxY:{maxY}");
  43. cameraBirdSec.SetRange(minX,maxX,minY,maxY);
  44. }
  45. }
  46. [Serializable]
  47. public class LodLayer
  48. {
  49. public MapGenerator map;
  50. public float cameraDistanceMin;
  51. public float cameraDistanceMax;
  52. public Vector2 min_X_Range;//最短距离时候的X范围
  53. public Vector2 max_X_Range;//最大距离时候的X范围
  54. public Vector2 min_Y_Range;
  55. public Vector2 max_Y_Range;
  56. }