BindingUtility.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Timeline;
  5. using Object = UnityEngine.Object;
  6. namespace UnityEditor.Timeline
  7. {
  8. static class BindingUtility
  9. {
  10. public static Type GetRequiredBindingType(PlayableBinding binding)
  11. {
  12. return binding.outputTargetType;
  13. }
  14. public static void Bind(PlayableDirector director, TrackAsset bindTo, Object objectToBind)
  15. {
  16. if (director == null || bindTo == null || TimelineWindow.instance == null)
  17. return;
  18. TimelineWindow.instance.state.previewMode = false; // returns all objects to previous state
  19. TimelineUndo.PushUndo(director, "PlayableDirector Binding");
  20. director.SetGenericBinding(bindTo, objectToBind);
  21. TimelineWindow.instance.state.rebuildGraph = true;
  22. }
  23. public static BindingAction GetBindingAction(Type requiredBindingType, Object objectToBind)
  24. {
  25. if (requiredBindingType == null || objectToBind == null)
  26. return BindingAction.DoNotBind;
  27. // prevent drag and drop of prefab assets
  28. if (PrefabUtility.IsPartOfPrefabAsset(objectToBind))
  29. return BindingAction.DoNotBind;
  30. if (requiredBindingType.IsInstanceOfType(objectToBind))
  31. return BindingAction.BindDirectly;
  32. var draggedGameObject = objectToBind as GameObject;
  33. if (!typeof(Component).IsAssignableFrom(requiredBindingType) || draggedGameObject == null)
  34. return BindingAction.DoNotBind;
  35. if (draggedGameObject.GetComponent(requiredBindingType) == null)
  36. return BindingAction.BindToMissingComponent;
  37. return BindingAction.BindToExistingComponent;
  38. }
  39. }
  40. enum BindingAction
  41. {
  42. DoNotBind,
  43. BindDirectly,
  44. BindToExistingComponent,
  45. BindToMissingComponent
  46. }
  47. }