CameraBirdSec.cs 7.7 KB

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