ControlPlayableUtility.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections.Generic;
  2. using UnityEngine.Playables;
  3. using UnityEngine.Timeline;
  4. namespace UnityEditor.Timeline
  5. {
  6. static class ControlPlayableUtility
  7. {
  8. public static bool DetectCycle(
  9. ControlPlayableAsset asset, PlayableDirector director, HashSet<PlayableDirector> set = null)
  10. {
  11. if (director == null || asset == null || !asset.updateDirector)
  12. return false;
  13. if (set == null)
  14. set = new HashSet<PlayableDirector>();
  15. if (set.Contains(director))
  16. return true;
  17. var gameObject = asset.sourceGameObject.Resolve(director);
  18. if (gameObject == null)
  19. return false;
  20. set.Add(director);
  21. foreach (var subDirector in asset.GetComponent<PlayableDirector>(gameObject))
  22. {
  23. foreach (var childAsset in GetPlayableAssets(subDirector))
  24. {
  25. if (DetectCycle(childAsset, subDirector, set))
  26. return true;
  27. }
  28. }
  29. set.Remove(director);
  30. return false;
  31. }
  32. public static IEnumerable<ControlPlayableAsset> GetPlayableAssets(PlayableDirector director)
  33. {
  34. var timeline = director != null ? (director.playableAsset as TimelineAsset) : null;
  35. if (timeline != null)
  36. {
  37. foreach (var t in timeline.GetOutputTracks())
  38. {
  39. var controlTrack = t as ControlTrack;
  40. if (controlTrack != null)
  41. {
  42. foreach (var c in t.GetClips())
  43. {
  44. var asset = c.asset as ControlPlayableAsset;
  45. if (asset != null)
  46. yield return asset;
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }