Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CreateAnnotationAction.cs 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor;
  4. using UnityEditor.Timeline;
  5. using UnityEditor.Timeline.Actions;
  6. using UnityEngine.Timeline;
  7. namespace Timeline.Samples
  8. {
  9. // Adds an additional item in context menus that will create a new annotation
  10. // and sets its description field with the clipboard's contents.
  11. [MenuEntry("Create Annotation from clipboard contents")]
  12. public class CreateAnnotationAction : TimelineAction
  13. {
  14. // Specifies the action's prerequisites:
  15. // - Invalid (grayed out in the menu) if no text content is in the clipboard;
  16. // - NotApplicable (not shown in the menu) if no track is selected;
  17. // - Valid (shown in the menu) otherwise.
  18. public override ActionValidity Validate(ActionContext context)
  19. {
  20. // get the current text content of the clipboard
  21. string clipboardTextContent = EditorGUIUtility.systemCopyBuffer;
  22. if (clipboardTextContent.Length == 0)
  23. {
  24. return ActionValidity.Invalid;
  25. }
  26. // Timeline's current selected items can be fetched with `context`
  27. IEnumerable<TrackAsset> selectedTracks = context.tracks;
  28. if (!selectedTracks.Any() || selectedTracks.All(track => track is GroupTrack))
  29. {
  30. return ActionValidity.NotApplicable;
  31. }
  32. return ActionValidity.Valid;
  33. }
  34. // Creates a new annotation and add it to the selected track.
  35. public override bool Execute(ActionContext context)
  36. {
  37. // to find at which time to create a new marker, we need to consider how this action was invoked.
  38. // If the action was invoked by a context menu item, then we can use the context's invocation time.
  39. // If the action was invoked through a keyboard shortcut, we can use Timeline's playhead time instead.
  40. double time;
  41. if (context.invocationTime.HasValue)
  42. {
  43. time = context.invocationTime.Value;
  44. }
  45. else
  46. {
  47. time = TimelineEditor.inspectedDirector.time;
  48. }
  49. string clipboardTextContent = EditorGUIUtility.systemCopyBuffer;
  50. IEnumerable<TrackAsset> selectedTracks = context.tracks;
  51. foreach (TrackAsset track in selectedTracks)
  52. {
  53. if (track is GroupTrack)
  54. continue;
  55. AnnotationMarker annotation = track.CreateMarker<AnnotationMarker>(time);
  56. annotation.description = clipboardTextContent;
  57. annotation.title = "Annotation";
  58. }
  59. return true;
  60. }
  61. }
  62. }