RecorderPlayableBehaviour.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEditor.Recorder.Input;
  2. using UnityEngine.Playables;
  3. namespace UnityEditor.Recorder.Timeline
  4. {
  5. class RecorderPlayableBehaviour : PlayableBehaviour
  6. {
  7. PlayState m_PlayState = PlayState.Paused;
  8. public RecordingSession session { get; set; }
  9. WaitForEndOfFrameComponent endOfFrameComp;
  10. bool m_FirstOneSkipped;
  11. public override void OnGraphStart(Playable playable)
  12. {
  13. if (session != null)
  14. {
  15. // does not support multiple starts...
  16. session.SessionCreated();
  17. m_PlayState = PlayState.Paused;
  18. }
  19. }
  20. public override void OnGraphStop(Playable playable)
  21. {
  22. if (session != null && session.isRecording)
  23. {
  24. session.EndRecording();
  25. session.Dispose();
  26. session = null;
  27. }
  28. }
  29. public override void PrepareFrame(Playable playable, FrameData info)
  30. {
  31. if (session != null && session.isRecording)
  32. {
  33. session.PrepareNewFrame();
  34. }
  35. }
  36. public override void ProcessFrame(Playable playable, FrameData info, object playerData)
  37. {
  38. if (session != null)
  39. {
  40. if (endOfFrameComp == null)
  41. {
  42. endOfFrameComp = session.recorderGameObject.AddComponent<WaitForEndOfFrameComponent>();
  43. endOfFrameComp.m_playable = this;
  44. }
  45. }
  46. }
  47. public override void OnBehaviourPlay(Playable playable, FrameData info)
  48. {
  49. if (session == null)
  50. return;
  51. // Assumption: OnPlayStateChanged( PlayState.Playing ) ONLY EVER CALLED ONCE for this type of playable.
  52. m_PlayState = PlayState.Playing;
  53. session.BeginRecording();
  54. }
  55. public override void OnBehaviourPause(Playable playable, FrameData info)
  56. {
  57. if (session == null)
  58. return;
  59. if (session.isRecording && m_PlayState == PlayState.Playing)
  60. {
  61. session.EndRecording();
  62. session.Dispose();
  63. session = null;
  64. }
  65. m_PlayState = PlayState.Paused;
  66. }
  67. public void FrameEnded()
  68. {
  69. if (session != null && session.isRecording)
  70. session.RecordFrame();
  71. }
  72. }
  73. }