CameraBirdSec.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Collections.Generic;
  3. using DG.Tweening;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. public class CameraBirdSec : MonoBehaviour
  9. {
  10. public Action beginDrag;
  11. public Transform target; // 目标对象,摄像头将朝向此对象
  12. public float smoothSpeed = 0.125f; // 摄像头平滑移动的速度
  13. public float scrollSensitivity = 5f; // 鼠标滚轮的灵敏度
  14. public float rotateSpeed = 0.25f;
  15. public float translateSpeed = 3f;
  16. public float minDistance = 5f; // 摄像头与目标对象的最小距离
  17. public float maxDistance = 50f; // 摄像头与目标对象的最大距离
  18. public float currentDistance; // 当前摄像头与目标对象的距离
  19. private Vector3 offset; // 摄像头与目标对象的偏移量
  20. private Vector3 velocity = Vector3.zero; // 摄像头移动的速度
  21. private bool isDragging = false; // 是否正在拖拽
  22. private Vector3 lastMousePosition; // 上一帧鼠标位置
  23. public Canvas canvas;
  24. public float rotateYAngle = 0.0f;
  25. public float rotateXAngle = 0.0f;
  26. public Action<float> OnDistanceChange;
  27. public float min_X;
  28. public float max_X;
  29. public float min_Y;
  30. public float max_Y;
  31. public bool fixMoveRange = false;
  32. bool isRotate = false;
  33. bool onUI = false;
  34. void Start()
  35. {
  36. // 初始化当前距离为初始偏移量的距离
  37. currentDistance = Vector3.Distance(transform.position, target.position);
  38. Debug.Log(currentDistance);
  39. // 确保初始距离在允许的范围内
  40. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  41. // 计算偏移量
  42. offset = transform.position - target.position;
  43. OnDistanceChange?.Invoke(currentDistance);
  44. }
  45. private bool GetPointerOverUIElement(out GameObject uiElement)
  46. {
  47. uiElement = null;
  48. PointerEventData pointerEventData = new PointerEventData(EventSystem.current)
  49. {
  50. position = Input.mousePosition
  51. };
  52. RaycastResult raycastResult = new RaycastResult();
  53. List<RaycastResult> results = new List<RaycastResult>();
  54. GraphicRaycaster raycaster = canvas.GetComponent<GraphicRaycaster>();
  55. if (raycaster != null)
  56. {
  57. raycaster.Raycast(pointerEventData, results);
  58. if (results.Count > 0)
  59. {
  60. raycastResult = results[0];
  61. uiElement = raycastResult.gameObject;
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. private bool IsPointerOverUIElement()
  68. {
  69. bool specialUI = false;
  70. if (GetPointerOverUIElement(out GameObject uiElement))
  71. {
  72. // 获取 UI 元素的类型
  73. if (uiElement.TryGetComponent<Button>(out Button button))
  74. {
  75. if (uiElement.transform.TryGetComponent<RuntimePoint>(out RuntimePoint rp)) {
  76. specialUI = true;
  77. }
  78. }
  79. }
  80. // 检查当前鼠标位置是否在 UI 上
  81. return EventSystem.current.IsPointerOverGameObject() && !specialUI;
  82. }
  83. void LateUpdate()
  84. {
  85. onUI = false;
  86. if (IsPointerOverUIElement()) onUI = true;
  87. translateSpeed = currentDistance / 1000.0f;
  88. // 鼠标滚轮控制摄像头远近
  89. if (Input.GetAxis("Mouse ScrollWheel") != 0 && !onUI)
  90. {
  91. // 更新当前距离
  92. currentDistance -= Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity;
  93. // 确保距离在允许的范围内
  94. currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
  95. //this.GetComponent<DepthFog>().Height = 400000 / currentDistance;
  96. OnDistanceChange?.Invoke(currentDistance);
  97. }
  98. //if (Input.GetMouseButton(2))
  99. //{
  100. // rotateXAngle -= Input.GetAxis("Mouse Y") * rotateSpeed;
  101. // rotateXAngle = Mathf.Clamp(rotateXAngle, -36f, 36f);
  102. // isRotate = true;
  103. //}
  104. //else {
  105. // isRotate = false;
  106. //}
  107. // 鼠标左键拖拽平移摄像头
  108. if (Input.GetMouseButtonDown(0) && !onUI)
  109. {
  110. beginDrag?.Invoke();
  111. isDragging = true;
  112. lastMousePosition = Input.mousePosition;
  113. }
  114. else if (Input.GetMouseButtonUp(0))
  115. {
  116. isDragging = false;
  117. }
  118. if (isDragging)
  119. {
  120. // 计算鼠标移动的偏移量
  121. Vector3 mouseOffset = Input.mousePosition - lastMousePosition;
  122. // 根据偏移量计算摄像头的目标位置
  123. Vector3 moveDirection = Vector3.down * mouseOffset.y * translateSpeed + Vector3.left * mouseOffset.x * translateSpeed;
  124. // 平滑移动摄像头到目标位置
  125. target.position += moveDirection;
  126. //限制移动范围
  127. if (fixMoveRange)
  128. {
  129. Vector3 finalPos=target.localPosition;
  130. finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
  131. finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
  132. target.localPosition = finalPos;
  133. }
  134. // 计算目标位置
  135. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, 0, 0)) * offset.normalized * currentDistance;
  136. // 摄像头平滑移动到目标位置
  137. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed * 2);
  138. }
  139. else
  140. {
  141. if (isRotate)
  142. {
  143. Blink();
  144. }
  145. else
  146. {
  147. // 计算目标位置
  148. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  149. // 摄像头平滑移动到目标位置
  150. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothSpeed);
  151. //if (Vector3.Distance(transform.position, targetPosition) < 1) {
  152. // transform.position = targetPosition;
  153. //}
  154. }
  155. }
  156. lastMousePosition = Input.mousePosition;
  157. }
  158. public void Blink()
  159. {
  160. // 计算目标位置
  161. Vector3 targetPosition = target.position + Quaternion.Euler(new Vector3(rotateXAngle, rotateYAngle, 0)) * offset.normalized * currentDistance;
  162. // 摄像头平滑移动到目标位置
  163. transform.position = targetPosition;
  164. //摄像头朝向目标对象
  165. transform.rotation = Quaternion.LookRotation(target.position - this.transform.position, Vector3.up);
  166. }
  167. public void SetRange(float _minX,float _maxX,float _minY,float _maxY)
  168. {
  169. min_X = _minX;
  170. max_X = _maxX;
  171. min_Y = _minY;
  172. max_Y = _maxY;
  173. //限制移动范围
  174. if (fixMoveRange)
  175. {
  176. Vector3 finalPos=target.localPosition;
  177. finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
  178. finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
  179. target.localPosition = finalPos;
  180. }
  181. }
  182. public void SetCameraToCenterFade(Vector3 centerPos,float distance = 300)
  183. {
  184. MeshRenderer fader = this.transform.GetChild(0).GetComponent<MeshRenderer>();
  185. fader.gameObject.SetActive(true);
  186. fader.material.DOColor(Color.white, 0.1f).onComplete = () =>
  187. {
  188. fader.material.DOColor(Color.clear, 1.5f).onComplete = () =>
  189. {
  190. fader.gameObject.SetActive(false);
  191. };
  192. };
  193. this.target.localPosition = centerPos;
  194. DOTween.To(()=>this.currentDistance,x=>this.currentDistance=x, distance,0.5f);
  195. this.Blink();
  196. }
  197. void Update()
  198. {
  199. }
  200. }