| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using DG.Tweening;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace UnityCommon
- {
- public abstract class CameraState
- {
- public Action onExit;
- public CameraState preCameraState;
- protected float speedScaleFactor = 1;
- public CameraState(Camera camera)
- {
- m_camera = camera;
- }
- public virtual void Enter()
- {
- speedScaleFactor = 1;
- m_camera.enabled = true;
- }
- public virtual void Exit()
- {
- m_camera.enabled = false;
- onExit?.Invoke();
- }
- public virtual void Update() { }
- protected Camera m_camera { get; set; }
- /// <summary>
- /// 切换完毕
- /// </summary>
- public bool m_isAllReady { get; set; }
- /// <summary>
- /// 镜头前后、左右、上下移动
- /// </summary>
- /// <param name="vecDir">x前后 y左右 z上下</param>
- public virtual void OnMove(Vector3 vecDir) { }
- /// <summary>
- /// 镜头旋转
- /// </summary>
- /// <param name="vector2">x左右 y上下</param>
- public virtual void OnRotate(Vector2 vector2) { }
- /// <summary>
- /// 缩放
- /// </summary>
- /// <param name="zoom"></param>
- public virtual void OnZoom(float zoom) { }
- /// <summary>
- /// 平移
- /// </summary>
- /// <param name="vector2"></param>
- public virtual void OnTranlation(Vector2 vector2) { }
- /// <summary>
- /// 加速
- /// </summary>
- /// <param name="isAdd"></param>
- public virtual void OnAddSpeed(float addSpeed) { }
- /// <summary>
- /// 缩放速度因子,移动、旋转、镜头推拉的速度缩放比例
- /// </summary>
- /// <param name="speedScale"></param>
- public virtual void OnChangeSpeedScale(float speedScale)
- {
- if (speedScale > 0) Math.Clamp(speedScaleFactor *= 2, 0.125f, 16);
- else if (speedScale < 0) Math.Clamp(speedScaleFactor /= 2, 0.125f, 16);
- }
- public void SetAnimDuration(float duration)
- {
- animDuration = duration;
- }
- float animDuration = 1;
- Sequence sequence;
- protected void SetCameraPosition(Vector3 position, Quaternion rotation, Action onCompleted = null)
- {
- if (animDuration <= 0)
- {
- m_camera.transform.position = position;
- m_camera.transform.rotation = rotation;
- m_isAllReady = true;
- onCompleted?.Invoke();
- }
- else
- {
- m_isAllReady = false;
- sequence?.Kill();
- sequence = DOTween.Sequence();
- sequence.Join(m_camera.transform.DOMove(position, animDuration));
- sequence.Join(m_camera.transform.DORotate(rotation.eulerAngles, animDuration));
- sequence.OnComplete(() =>
- {
- m_isAllReady = true;
- onCompleted?.Invoke();
- });
- }
- }
- }
- }
|