Some background:
I’ve been diving into the underbelly of Timeline customization by creating my own Track and Clips. The clips are more or less just a collection of transform and sprite properties I’d like to animate together instead of on separate tracks:
As seen above, the properties I expect are all there. However, when right clicking on a clip to jump to the animation window, only the properties that I’ve already set keyframes for are present.
I’ve already written a script to generate missing properties for a clip by just creating curves for them with a default key at frame 0, which works nicely (snippet below is just an example of creating one curve, but should help for context of what I’m about to ask):
/// <summary>
/// Create a default curve on the given clip.
/// </summary>
/// <param name="clip">The timeline clip to populate.</param>
/// <param name="type">The type used for the curve (used by
/// AnimationCurve.SetCurve). This should most likely be the concrete class
/// for the PlayableAsset clip hosting these curves.</param>
/// <param name="propertyName">The name of the property the curve is for</param>
/// <param name="defaultValue">The starting value for the curve</param>
public static void CreateEmptyCurve(TimelineClip clip, Type type, string propertyName, float defaultValue = 0) {
AnimationCurve curve = new AnimationCurve(new Keyframe[] { new Keyframe(0, defaultValue)});
clip.curves.SetCurve("", type, propertyName, curve);
}
Here’s the catch:
I can only generate the missing curves if clip.curves exists, and TimelineEditor.selectedClip.curves (the TimelineClip I’m passing into the function above) is null until I add at least 1 keyframe through the timeline window, and I can’t just create the curve via TimelineEditor.selectedClip.curves = new AnimationClip()
since the property is read-only.
Props to the Unity team for the lazy initialization strategy here, but is there any way I can get Unity to initialize this property through script? Maybe through reflection, per se?