I’m trying to create animation clips and set keys with c#. @seant_unity helped me out here to set curves on a custom clip but the same approach doesn’t work for animation clips. I’m using 2018.3.0f2
In the image below you can see the “My Animation Clip” has been created in the timeline. I recorded a clip “Recorded” and you can see the Position.y keys
However when I open My Animation Clip the position x,y,z keys are not available in the track header even though the y keys I created via c# are in the clip/not a clip.
I feel it may have something to do with the Animation Playable Asset but I’ve not been able to find much info on this.
Here is my code:
var playableDirectors = FindObjectsOfType<PlayableDirector>();
foreach (var director in playableDirectors)
{
var timelineAsset = director.playableAsset as TimelineAsset;
if (timelineAsset != null)
{
foreach (var track in timelineAsset.GetOutputTracks())
{
if (track.name == "Sprite Test Track")
{
var newCustomClip = track.CreateClip<SpriteTestClip>();
newCustomClip.displayName = "My New Clip";
newCustomClip.duration = 10;
}
if (track.name == "Animation Track")
{
var newCustomClip = track.CreateDefaultClip();
newCustomClip.displayName = "My Animation Clip";
newCustomClip.duration = 10;
Keyframe[] keys;
keys = new Keyframe[3];
keys[0] = new Keyframe(0.0f, 0.0f);
keys[1] = new Keyframe(1.1f, 1.5f);
keys[2] = new Keyframe(2.0f, 0.0f);
curve = new AnimationCurve(keys);
Debug.Log(curve.keys[1].time);
typeof(TimelineClip).GetMethod("AllocateAnimatedParameterCurves", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(newCustomClip, new object[] { });
newCustomClip.curves.SetCurve("", typeof(AnimationClip), "Position.y", curve);
AssetDatabase.SaveAssets();
// This is just me thowing mud at a wall - I have no idea what I am doing
AnimationPlayableAsset animationPlayableAsset = newCustomClip.asset as AnimationPlayableAsset;
animationPlayableAsset.clip = newCustomClip.animationClip;
}
}
}
}
Any advice would be greatly appreciated. Cheers