TweenFOV.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. /// <summary>
  3. /// Tween the camera's field of typeView.
  4. /// </summary>
  5. [RequireComponent(typeof(Camera))]
  6. public class TweenFOV : Tweener
  7. {
  8. public float from = 45f;
  9. public float to = 45f;
  10. Camera mCam;
  11. public Camera cachedCamera {
  12. get {
  13. if (mCam == null)
  14. mCam = GetComponent<Camera>();
  15. return mCam;
  16. }
  17. }
  18. /// <summary>
  19. /// Tween's current value.
  20. /// </summary>
  21. public float value {
  22. get {
  23. return cachedCamera.fieldOfView;
  24. }
  25. set {
  26. cachedCamera.fieldOfView = value;
  27. }
  28. }
  29. /// <summary>
  30. /// Tween the value.
  31. /// </summary>
  32. protected override void OnUpdate(float factor, bool isFinished)
  33. {
  34. value = from * (1f - factor) + to * factor;
  35. }
  36. /// <summary>
  37. /// Start the tweening operation.
  38. /// </summary>
  39. static public TweenFOV Begin(GameObject go, float duration, float to)
  40. {
  41. TweenFOV comp = Tweener.Begin<TweenFOV>(go, duration);
  42. comp.from = comp.value;
  43. comp.to = to;
  44. if (duration <= 0f)
  45. {
  46. comp.Sample(1f, true);
  47. comp.enabled = false;
  48. }
  49. return comp;
  50. }
  51. [ContextMenu("Set 'From' to current value")]
  52. public override void SetStartToCurrentValue()
  53. {
  54. from = value;
  55. }
  56. [ContextMenu("Set 'To' to current value")]
  57. public override void SetEndToCurrentValue()
  58. {
  59. to = value;
  60. }
  61. [ContextMenu("Assume value of 'From'")]
  62. void SetCurrentValueToStart()
  63. {
  64. value = from;
  65. }
  66. [ContextMenu("Assume value of 'To'")]
  67. void SetCurrentValueToEnd()
  68. {
  69. value = to;
  70. }
  71. }