AnimatorTimeline.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using UnityEngine;
  2. namespace Chronos
  3. {
  4. public class AnimatorTimeline : ComponentTimeline<Animator>
  5. {
  6. public AnimatorTimeline(Timeline timeline, Animator component) : base(timeline, component) { }
  7. private float _speed;
  8. /// <summary>
  9. /// The speed that is applied to the animator before time effects. Use this property instead of Animator.speed, which will be overwritten by the timeline at runtime.
  10. /// </summary>
  11. public float speed
  12. {
  13. get { return _speed; }
  14. set
  15. {
  16. _speed = value;
  17. AdjustProperties();
  18. }
  19. }
  20. private int recordedFrames
  21. {
  22. get
  23. {
  24. // TODO: Proper FPS anticipation, with Application.targetFrameRate and v-sync
  25. // http://docs.unity3d.com/ScriptReference/Application-targetFrameRate.html
  26. return Mathf.Clamp((int)(timeline.recordingDuration * 60), 1, 10000);
  27. }
  28. }
  29. public override void CopyProperties(Animator source)
  30. {
  31. _speed = source.speed;
  32. }
  33. public override void AdjustProperties(float timeScale)
  34. {
  35. if (timeScale > 0)
  36. {
  37. component.speed = speed * timeScale;
  38. }
  39. else
  40. {
  41. component.speed = 0;
  42. }
  43. }
  44. public override void OnStartOrReEnable()
  45. {
  46. if (timeline.rewindable)
  47. {
  48. component.StartRecording(recordedFrames);
  49. }
  50. }
  51. public override void OnDisable()
  52. {
  53. if (timeline.rewindable)
  54. {
  55. component.StopRecording();
  56. }
  57. }
  58. public override void Update()
  59. {
  60. if (timeline.rewindable)
  61. {
  62. float timeScale = timeline.timeScale;
  63. float lastTimeScale = timeline.lastTimeScale;
  64. // TODO: Refactor and put the recorder offline when time is paused
  65. if (lastTimeScale >= 0 && timeScale < 0) // Started rewind
  66. {
  67. component.StopRecording();
  68. // There seems to be a bug in some cases in which no data is recorded
  69. // and recorder start and stop time are at -1. Can't seem to figure
  70. // when or why it happens, though. Temporary hotfix to disable playback
  71. // in that case.
  72. if (component.recorderStartTime < 0)
  73. {
  74. Debug.LogWarning("Animator timeline failed to record for unknown reasons.\nSee: http://forum.unity3d.com/threads/341203/", component);
  75. }
  76. else
  77. {
  78. component.StartPlayback();
  79. component.playbackTime = component.recorderStopTime;
  80. }
  81. }
  82. else if (component.recorderMode == AnimatorRecorderMode.Playback && timeScale > 0) // Stopped rewind
  83. {
  84. component.StopPlayback();
  85. component.StartRecording(recordedFrames);
  86. }
  87. else if (timeScale < 0 && component.recorderMode == AnimatorRecorderMode.Playback) // Rewinding
  88. {
  89. float playbackTime = Mathf.Max(component.recorderStartTime, component.playbackTime + timeline.deltaTime);
  90. component.playbackTime = playbackTime;
  91. }
  92. }
  93. }
  94. }
  95. }