I’m learning about Unity’s custom timeline markers using the official samples. I discovered that when I use the TrackAsset.CreateMark method to create a custom marker, the MarkEditor.OnCreate method is not called. However, if I create a custom marker by right-clicking on a track and selecting “Add Annotation,” the OnCreate method is called as expected.
After reviewing part of the timeline code, I found that the OnCreate method is triggered by the following line: CustomTimelineEditorCache.GetMarkerEditor(newMarker).OnCreate(newMarker, null);. Unfortunately, since CustomTimelineEditorCache is an internal class, I cannot call it directly.
How can I trigger the OnCreate method after using a custom CreateMark function?
Part of code:
namespace Timeline.Samples {
[MenuEntry("Create Annotation from clipboard contents")]
public class CreateAnnotationAction : TimelineAction {
// *** other methods *** //
public override bool Execute(ActionContext context) {
double time;
if (context.invocationTime.HasValue) {
time = context.invocationTime.Value;
} else {
time = TimelineEditor.inspectedDirector.time;
}
if (time < 0f) {
return false;
}
string clipboardTextContent = EditorGUIUtility.systemCopyBuffer;
IEnumerable<TrackAsset> selectedTracks = context.tracks;
foreach (TrackAsset track in selectedTracks) {
GroupTrack groupTrack = track as GroupTrack;
if (!ReferenceEquals(groupTrack, null)) {
continue;
}
AnnotationMarker annotation = track.CreateMarker<AnnotationMarker>(time); // not call OnCreate
annotation.description = clipboardTextContent;
annotation.title = "Annotation from clipboard";
annotation.name = "Annotation";
}
return true;
}
}
}
namespace Timeline.Samples {
[CustomTimelineEditor(typeof(AnnotationMarker))]
public class AnnotationMarkerMarkerEditor : MarkerEditor {
private const float k_LineOverlayWidth = 3.0f;
private const string k_OverlayPath = "timeline_annotation_overlay";
private const string k_OverlaySelectedPath = "timeline_annotation_overlay_selected";
private const string k_OverlayCollapsedPath = "timeline_annotation_overlay_collapsed";
private static readonly Texture2D s_OverlayTexture;
private static readonly Texture2D s_OverlaySelectedTexture;
private static readonly Texture2D s_OverlayCollapsedTexture;
private const string IconName = "AnnotationMarkerIcon";
private static Texture2D s_Icon;
[InitializeOnLoadMethod]
private static void InitializeOnLoad() {
s_Icon = Resources.Load<Texture2D>(IconName);
}
public override void OnCreate(IMarker marker, IMarker clonedFrom) {
base.OnCreate(marker, clonedFrom);
if (marker is not AnnotationMarker annotation) {
return;
}
EditorGUIUtility.SetIconForObject(annotation, s_Icon);
}
}
}