using System; using System.Collections; using System.Collections.Generic; using GameFramework.Event; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Playables; public class ParticleMgrCtrl : MonoBehaviour { public static ParticleMgrCtrl _Instance; public float rotationSpeed = 8.0f; // 摄像机绕点旋转速度 public float rotationDamping = 0.96f; // 旋转阻尼系数,控制旋转阻尼效果 public float minDistance = 2.0f; // 最小距离 public float maxDistance = 30.0f; // 最大距离 public float zoomSpeed = 3f; // 缩放速度 public float zoomDamping = 0.96f; // 缩放阻尼系数,控制缩放阻尼效果 public Vector3 targetPosition; // 旋转点 private Vector3 lastMousePosition; // 上一帧的鼠标位置 private Vector3 currentVelocity; // 当前旋转速度 private float distance = 30.0f; // 初始摄像机距离 private float targetDistance; private bool isRotating = false; // 是否正在旋转 private bool isOnUI = false; public GameObject[] particleList; public int currentModelIndex = 0; public PlayableDirector director; public int currentBaseStudyType { get; private set; } private bool havePlay = false; private void Awake() { _Instance = this; particleList = new GameObject[this.transform.childCount]; for (int i = 0; i < this.transform.childCount; i++) { particleList[i] = this.transform.GetChild(i).gameObject; } } void Start() { targetDistance = distance; GameMain.Event.Subscribe(ChangeBaseStudyTypeEvent.EventId, OnChangeBaseStudy); GameMain.Event.Subscribe(Change3DTopoTimeLineEvent.EventId, OnDealTimeline); GameMain.Event.Subscribe(Stop3DTopoTimeLineEvent.EventId, OnStopTimeline); } private void OnDealTimeline(object sender, GameEventArgs e) { Change3DTopoTimeLineEvent args = (Change3DTopoTimeLineEvent)e; switch (args.type) { case 3: director.Play(); havePlay = true; director.stopped += (director) => { if (havePlay) { havePlay = false; GameMain.Event.Fire(this,Stop3DTopoTimeLineEvent.Create()); } }; break; } } private void OnStopTimeline(object sender, GameEventArgs e) { Stop3DTopoTimeLineEvent args = (Stop3DTopoTimeLineEvent)e; if (havePlay) { director.time = director.duration - 4; havePlay = false; } } private void OnChangeBaseStudy(object sender, GameEventArgs e) { ChangeBaseStudyTypeEvent args = (ChangeBaseStudyTypeEvent)e; if (args.type != currentBaseStudyType) { switch (args.type) { case 0: this.gameObject.SetActive(false); break; case 1: this.gameObject.SetActive(true); break; case 2: this.gameObject.SetActive(false); break; case 3: this.gameObject.SetActive(false); break; } currentBaseStudyType = args.type; } } // Update is called once per frame void Update() { RotationCamera(); } private void OnDestroy() { GameMain.Event.Unsubscribe(ChangeBaseStudyTypeEvent.EventId, OnChangeBaseStudy); GameMain.Event.Unsubscribe(Change3DTopoTimeLineEvent.EventId, OnDealTimeline); GameMain.Event.Unsubscribe(Stop3DTopoTimeLineEvent.EventId, OnStopTimeline); } public void ResetCamera(Vector3 newPos) { Vector3 dir = newPos - targetPosition; targetPosition = newPos; targetDistance = distance; Camera.main.transform.position += dir; } void RotationCamera() { // 获取鼠标输入 Vector3 currentMousePosition = Input.mousePosition; if (EventSystem.current.IsPointerOverGameObject()) { isOnUI = true; } else { isOnUI = false; } if (Input.GetMouseButtonDown(0) && !isOnUI) { lastMousePosition = currentMousePosition; isRotating = true; } else if (Input.GetMouseButtonUp(0)) { isRotating = false; } // 如果正在旋转 if (isRotating) { // 计算鼠标移动量 Vector3 mouseDelta = currentMousePosition - lastMousePosition; // 计算摄像机绕点的旋转 float rotationX = mouseDelta.y * rotationSpeed * Time.deltaTime; float rotationY = -mouseDelta.x * rotationSpeed * Time.deltaTime; // 应用旋转阻尼效果 rotationX *= rotationDamping; rotationY *= rotationDamping; // 计算新的旋转速度 currentVelocity = new Vector3(rotationX, rotationY, 0); // 应用旋转 Camera.main.transform.RotateAround(targetPosition, Vector3.up, currentVelocity.y); Camera.main.transform.RotateAround(targetPosition, Camera.main.transform.right, currentVelocity.x); // 更新鼠标位置 lastMousePosition = currentMousePosition; } else { // 增强旋转阻尼效果 currentVelocity *= rotationDamping; Camera.main.transform.RotateAround(targetPosition, Vector3.up, currentVelocity.y); Camera.main.transform.RotateAround(targetPosition, Camera.main.transform.right, currentVelocity.x); } if (!isOnUI) { // 使用鼠标中键来控制摄像机距离 float scroll = Input.GetAxis("Mouse ScrollWheel"); targetDistance = Mathf.Clamp(targetDistance - scroll * zoomSpeed, minDistance, maxDistance); } // 应用缩放阻尼效果来平滑改变摄像机距离 distance = Mathf.Lerp(distance, targetDistance, Time.deltaTime * zoomDamping); // 更新摄像机位置 Vector3 dirToTarget = (Camera.main.transform.position - targetPosition).normalized; Camera.main.transform.position = targetPosition + dirToTarget * distance; } }