CameraState.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using DG.Tweening;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace UnityCommon
  7. {
  8. public abstract class CameraState
  9. {
  10. public Action onExit;
  11. public CameraState preCameraState;
  12. protected float speedScaleFactor = 1;
  13. public CameraState(Camera camera)
  14. {
  15. m_camera = camera;
  16. }
  17. public virtual void Enter()
  18. {
  19. speedScaleFactor = 1;
  20. m_camera.enabled = true;
  21. }
  22. public virtual void Exit()
  23. {
  24. m_camera.enabled = false;
  25. onExit?.Invoke();
  26. }
  27. public virtual void Update() { }
  28. protected Camera m_camera { get; set; }
  29. /// <summary>
  30. /// 切换完毕
  31. /// </summary>
  32. public bool m_isAllReady { get; set; }
  33. /// <summary>
  34. /// 镜头前后、左右、上下移动
  35. /// </summary>
  36. /// <param name="vecDir">x前后 y左右 z上下</param>
  37. public virtual void OnMove(Vector3 vecDir) { }
  38. /// <summary>
  39. /// 镜头旋转
  40. /// </summary>
  41. /// <param name="vector2">x左右 y上下</param>
  42. public virtual void OnRotate(Vector2 vector2) { }
  43. /// <summary>
  44. /// 缩放
  45. /// </summary>
  46. /// <param name="zoom"></param>
  47. public virtual void OnZoom(float zoom) { }
  48. /// <summary>
  49. /// 平移
  50. /// </summary>
  51. /// <param name="vector2"></param>
  52. public virtual void OnTranlation(Vector2 vector2) { }
  53. /// <summary>
  54. /// 加速
  55. /// </summary>
  56. /// <param name="isAdd"></param>
  57. public virtual void OnAddSpeed(float addSpeed) { }
  58. /// <summary>
  59. /// 缩放速度因子,移动、旋转、镜头推拉的速度缩放比例
  60. /// </summary>
  61. /// <param name="speedScale"></param>
  62. public virtual void OnChangeSpeedScale(float speedScale)
  63. {
  64. if (speedScale > 0) Math.Clamp(speedScaleFactor *= 2, 0.125f, 16);
  65. else if (speedScale < 0) Math.Clamp(speedScaleFactor /= 2, 0.125f, 16);
  66. }
  67. public void SetAnimDuration(float duration)
  68. {
  69. animDuration = duration;
  70. }
  71. float animDuration = 1;
  72. Sequence sequence;
  73. protected void SetCameraPosition(Vector3 position, Quaternion rotation, Action onCompleted = null)
  74. {
  75. if (animDuration <= 0)
  76. {
  77. m_camera.transform.position = position;
  78. m_camera.transform.rotation = rotation;
  79. m_isAllReady = true;
  80. onCompleted?.Invoke();
  81. }
  82. else
  83. {
  84. m_isAllReady = false;
  85. sequence?.Kill();
  86. sequence = DOTween.Sequence();
  87. sequence.Join(m_camera.transform.DOMove(position, animDuration));
  88. sequence.Join(m_camera.transform.DORotate(rotation.eulerAngles, animDuration));
  89. sequence.OnComplete(() =>
  90. {
  91. m_isAllReady = true;
  92. onCompleted?.Invoke();
  93. });
  94. }
  95. }
  96. }
  97. }