How to manually name a TrackAsset on creation?

Hello,
I’ve been trying to customize a TrackAsset’s name on creation.
As far as I can tell, there is no way to actually do that?

I checked Timeline’s source code to try and verify my assumption, and…
TimelineContextMenu calls “TimelineHelpers.CreateTrack((Type)trackItemType, …” with a null name argument.
TimelineHelpers.CreateTrack calls “var track = asset.CreateTrack(type, parent, name);”, and…
TimelineAsset.CreateTrack calls the following

 var baseName = name;
            if (string.IsNullOrEmpty(baseName))
            {
                baseName = type.Name;
#if UNITY_EDITOR
                baseName = UnityEditor.ObjectNames.NicifyVariableName(baseName);
#endif
            }

            var trackName = baseName;
            if (parent != null)
                trackName = TimelineCreateUtilities.GenerateUniqueActorName(parent.subTracksObjects, baseName);
            else
                trackName = TimelineCreateUtilities.GenerateUniqueActorName(trackObjects, baseName);

It’s a shame that at no point neither of [DisplayName], [CreateAssetMenu] are used to determine the name, or any callbacks are made to let users author the name via script.

None of the scriptableobject calls seem to be useful for this, either. Awake/OnEnable/OnValidate don’t get called until later, and will reoccur more than once per the asset’s lifetime after creation.

Is there any way to rename the track via script without having to declare a contextmenu option manually?

There are no built-in callbacks that get called every time a track is added, but the TrackEditor class has an OnCreate method you can override. You can change the dynamically generated name there.

You need to add the [CustomTimelineEditor(typeof(MY_TRACK_TYPE))] attribute to your editor.

This can be done even for built-in classes, but it either needs to be done for each track type (if you need some custom editors for some tracks), or once for TrackAsset.

Finally, you can also check this thread for examples

1 Like

Thank you very much for the response. This worked well.
To others who may struggle figuring it out, here’s a working snippet

    [CustomTimelineEditor(typeof(MyCustomTrack))]
    public class MyCustomTrackEditor : TrackEditor
    {
        public override void OnCreate(TrackAsset track, TrackAsset copiedFrom) => track.name = "Test 123";
    }
1 Like