AnimationPreviewUpdateCallback.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Collections.Generic;
  2. using UnityEngine.Animations;
  3. using UnityEngine.Experimental.Animations;
  4. using UnityEngine.Playables;
  5. namespace UnityEngine.Timeline
  6. {
  7. class AnimationPreviewUpdateCallback : ITimelineEvaluateCallback
  8. {
  9. AnimationPlayableOutput m_Output;
  10. PlayableGraph m_Graph;
  11. List<IAnimationWindowPreview> m_PreviewComponents;
  12. public AnimationPreviewUpdateCallback(AnimationPlayableOutput output)
  13. {
  14. m_Output = output;
  15. Playable playable = m_Output.GetSourcePlayable();
  16. if (playable.IsValid())
  17. {
  18. m_Graph = playable.GetGraph();
  19. }
  20. }
  21. public void Evaluate()
  22. {
  23. if (!m_Graph.IsValid())
  24. return;
  25. if (m_PreviewComponents == null)
  26. FetchPreviewComponents();
  27. foreach (var component in m_PreviewComponents)
  28. {
  29. if (component != null)
  30. {
  31. component.UpdatePreviewGraph(m_Graph);
  32. }
  33. }
  34. }
  35. private void FetchPreviewComponents()
  36. {
  37. m_PreviewComponents = new List<IAnimationWindowPreview>();
  38. var animator = m_Output.GetTarget();
  39. if (animator == null)
  40. return;
  41. var gameObject = animator.gameObject;
  42. m_PreviewComponents.AddRange(gameObject.GetComponents<IAnimationWindowPreview>());
  43. }
  44. }
  45. }