123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- using System;
- using System.Collections.Generic;
- using DG.Tweening;
- using Unity.Mathematics;
- using Unity.VisualScripting;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- public class CameraBirdSec : MonoBehaviour
- {
- public Action beginDrag;
- public float smoothSpeed = 0.125f; // 摄像头平滑移动的速度
- public float scrollSensitivity = 5f; // 鼠标滚轮的灵敏度
- public float rotateSpeed = 0.25f;
- public float translateSpeed = 3f;
- public float minDistance = 5f; // 摄像头与目标对象的最小距离
- public float maxDistance = 50f; // 摄像头与目标对象的最大距离
- public float currentDistance; // 当前摄像头与目标对象的距离
- private Vector3 velocity = Vector3.zero; // 摄像头移动的速度
- private bool isDragging = false; // 是否正在拖拽
- private Vector3 lastMousePosition; // 上一帧鼠标位置
- public Canvas canvas;
- public float rotateYAngle = 0.0f;
- public float rotateXAngle = 0.0f;
- public Action<float> OnDistanceChange;
- public float min_X;
- public float max_X;
- public float min_Y;
- public float max_Y;
- public bool fixMoveRange = false;
- public bool detailLod = false;
- bool onUI = false;
- private Camera _camera;
- void Start()
- {
- _camera = this.GetComponent<Camera>();
- }
- private bool GetPointerOverUIElement(out GameObject uiElement)
- {
- uiElement = null;
- PointerEventData pointerEventData = new PointerEventData(EventSystem.current)
- {
- position = Input.mousePosition
- };
- RaycastResult raycastResult = new RaycastResult();
- List<RaycastResult> results = new List<RaycastResult>();
- GraphicRaycaster raycaster = canvas.GetComponent<GraphicRaycaster>();
- if (raycaster != null)
- {
- raycaster.Raycast(pointerEventData, results);
- if (results.Count > 0)
- {
- raycastResult = results[0];
- uiElement = raycastResult.gameObject;
- return true;
- }
- }
- return false;
- }
- private bool IsPointerOverUIElement()
- {
- bool specialUI = false;
- if (GetPointerOverUIElement(out GameObject uiElement))
- {
- // 获取 UI 元素的类型
- if (uiElement.TryGetComponent<Button>(out Button button))
- {
- if (uiElement.transform.TryGetComponent<RuntimePoint>(out RuntimePoint rp))
- {
- specialUI = true;
- }
- }
- }
- // 检查当前鼠标位置是否在 UI 上
- return EventSystem.current.IsPointerOverGameObject() && !specialUI;
- }
- void LateUpdate()
- {
- onUI = false;
- if (IsPointerOverUIElement()) onUI = true;
- translateSpeed = currentDistance / 1000.0f;
- // 鼠标滚轮控制摄像头远近
- if (Input.GetAxis("Mouse ScrollWheel") != 0 && !onUI)
- {
- //定一个鼠标在地图上的位置
- var mousePosOnMap = Input.mousePosition;
- mousePosOnMap.z = math.abs(-582 - this.transform.position.z);
- mousePosOnMap = _camera.ScreenToWorldPoint(mousePosOnMap);
- var cameraPosOnMap = this.transform.position;
- cameraPosOnMap.z = -582;
- var distance_y = Vector3.Distance(cameraPosOnMap, this.transform.position);
- var distance_x = Vector3.Distance(cameraPosOnMap, mousePosOnMap);
- var dir_x = (mousePosOnMap - cameraPosOnMap).normalized;
-
-
- if (detailLod)
- {
- scrollSensitivity = math.clamp(currentDistance, 0, 10);
- // 更新当前距离
- currentDistance -= Input.GetAxis("Mouse ScrollWheel") * scrollSensitivity;
- // 确保距离在允许的范围内
- currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);
- //this.GetComponent<DepthFog>().Height = 400000 / currentDistance;
- OnDistanceChange?.Invoke(currentDistance);
- }
- else
- {
- OnDistanceChange?.Invoke(Input.GetAxis("Mouse ScrollWheel"));
- }
- Vector3 targetPosition = transform.position;
- targetPosition.z = -582 - currentDistance;
- var distance_y_after = Vector3.Distance(targetPosition, cameraPosOnMap);
- var targetScale = distance_y_after / distance_y;
- var moveDistance = (distance_x * targetScale)-distance_x;
- var finalPos = targetPosition - dir_x * moveDistance;
-
- //限制移动范围
- if (fixMoveRange)
- {
- Vector3 finalPos_Fix = finalPos;
- finalPos_Fix.x = Mathf.Clamp(finalPos_Fix.x, min_X, max_X);
- finalPos_Fix.y = Mathf.Clamp(finalPos_Fix.y, min_Y, max_Y);
- finalPos = finalPos_Fix;
- }
-
- transform.position = finalPos;
- }
- // 鼠标左键拖拽平移摄像头
- if (Input.GetMouseButtonDown(1) && !onUI)
- {
- beginDrag?.Invoke();
- isDragging = true;
- lastMousePosition = Input.mousePosition;
- }
- else if (Input.GetMouseButtonUp(1))
- {
- isDragging = false;
- }
-
- if (isDragging)
- {
- // 计算鼠标移动的偏移量
- Vector3 mouseOffset = Input.mousePosition - lastMousePosition;
- // 根据偏移量计算摄像头的目标位置
- Vector3 moveDirection = Vector3.down * mouseOffset.y * translateSpeed +
- Vector3.left * mouseOffset.x * translateSpeed;
-
- Vector3 targetPosition=transform.position+ moveDirection;
- //限制移动范围
- if (fixMoveRange)
- {
- Vector3 finalPos = targetPosition;
- finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
- finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
- targetPosition = finalPos;
- }
- transform.position = targetPosition;
- }
- lastMousePosition = Input.mousePosition;
- }
-
- public void SetRange(float _minX, float _maxX, float _minY, float _maxY)
- {
- min_X = _minX;
- max_X = _maxX;
- min_Y = _minY;
- max_Y = _maxY;
- //限制移动范围
- if (fixMoveRange)
- {
- Vector3 finalPos = transform.position;
- finalPos.x = Mathf.Clamp(finalPos.x, min_X, max_X);
- finalPos.y = Mathf.Clamp(finalPos.y, min_Y, max_Y);
- transform.position = finalPos;
- }
- }
- public void SetCameraToCenterFade(Vector3 centerPos, float distance = 300)
- {
- MeshRenderer fader = this.transform.GetChild(0).GetComponent<MeshRenderer>();
- fader.gameObject.SetActive(true);
- fader.material.DOColor(Color.white, 0.1f).onComplete = () =>
- {
- fader.material.DOColor(Color.clear, 1.5f).onComplete = () => { fader.gameObject.SetActive(false); };
- };
- centerPos.z = -582 - currentDistance;
- transform.position = centerPos;
- }
- }
|