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