Files
JJBB/Assets/Project/Script/SceneMovie/ControlMove.cs

115 lines
3.8 KiB
C#
Raw Normal View History

2024-08-23 15:49:34 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Cinemachine;
using Games.Events;
using UnityEngine.Playables;
public class ControlMove : MonoBehaviour
{
public float speed;
public CinemachineSmoothPath m_paths;
public bool UseMainCameraPos = false;
public bool NeedBackMainCamera = false; //是否需要回到主摄像机
public GameObject LinkCamera;
public CinemachineBrain Brain;
public bool UpdateLookAt = true;
private CinemachineDollyCart _dolly;
private MovieEventSend _EventSend;
private void Awake()
{
_EventSend = GetComponent<MovieEventSend>();
}
private void CopyMainCameraData()
{
if (LinkCamera == null)
return;
CinemachineVirtualCamera Virtualcamera = LinkCamera.GetComponent<CinemachineVirtualCamera>();
if (Virtualcamera == null)
return;
Camera mainCamera = Games.Scene.SceneLogic.CameraController != null ? Games.Scene.SceneLogic.CameraController.MainCamera : Camera.main;
if (mainCamera == null)
return;
if(Brain!=null)
{
Brain.transform.position = mainCamera.transform.position;
Brain.transform.rotation = mainCamera.transform.rotation;
Brain.OutputCamera.cullingMask = mainCamera.cullingMask;
Brain.OutputCamera.depth = mainCamera.depth + 1;
}
LinkCamera.transform.position = mainCamera.transform.position;
LinkCamera.transform.rotation = mainCamera.transform.rotation;
Virtualcamera.m_Lens.FieldOfView = mainCamera.fieldOfView;
Virtualcamera.m_Lens.NearClipPlane = mainCamera.nearClipPlane;
Virtualcamera.m_Lens.FarClipPlane = mainCamera.farClipPlane;
}
private void Start()
{
if (m_paths == null)
{
m_paths = GetComponent<CinemachineSmoothPath>();
}
if (m_paths == null || m_paths.m_Waypoints.Length <= 0)
return;
List<CinemachineSmoothPath.Waypoint> points = new List<CinemachineSmoothPath.Waypoint>(m_paths.m_Waypoints);
CinemachineSmoothPath.Waypoint point = new CinemachineSmoothPath.Waypoint();
if (LinkCamera == null)
return;
Camera camera = LinkCamera.GetComponent<Camera>();
Camera mainCamera = Games.Scene.SceneLogic.CameraController != null ? Games.Scene.SceneLogic.CameraController.MainCamera : null;
if (camera != null && mainCamera != null)
{
camera.depth = mainCamera.depth + 1;
}
if (UseMainCameraPos)
{
if (mainCamera == null)
return;
CopyMainCameraData();
point.position = m_paths.transform.InverseTransformPoint(mainCamera.transform.position);
}
else
point.position = m_paths.transform.InverseTransformPoint(LinkCamera.transform.position);
points.Insert(0, point);
if (NeedBackMainCamera)
points.Add(point);
m_paths.m_Waypoints = points.ToArray();
_dolly = LinkCamera.gameObject.EnsureComponent<CinemachineDollyCart>();
if (_dolly == null)
{
return;
}
_dolly.m_Path = m_paths;
_dolly.UpdateLookAt = UpdateLookAt;
_dolly.m_Speed = speed;
float Time = _dolly.m_Path.PathLength / speed;
StopCoroutine(SendEvent(0));
StartCoroutine(SendEvent(Time));
if(transform.parent!=null)
{
SceneMovie scenMovie = transform.parent.GetComponent<SceneMovie>();
if (scenMovie != null)
scenMovie.AddDurTime(Time);
}
}
IEnumerator SendEvent(float Time)
{
yield return new WaitForSeconds(Time);
if (_EventSend == null)
yield break ;
_EventSend.Send(true);
}
}