Unity Timeline custom learning

Make a simple displacement

1. First, you need to create a track (Track). You only need to limit the type of the clip (TrackClip) on the track (Track).

[TrackClipType(typeof(TransformPlayableAsset))]
public class TransformTrack : PlayableTrack
{

}

2. Create a PlayableAsset

[System.Serializable]
public class TransformPlayableAsset : PlayableAsset
{
    public ExposedReference<Transform> transform;
    public ExposedReference<Transform> start;
    public ExposedReference<Transform> end;
    public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        var playable = ScriptPlayable<TransformBehaviour>.Create(graph);//创建一个带有behaviour的clip
        var behaviour = playable.GetBehaviour();//获取上面的behaviour
        behaviour.transform = transform.Resolve(graph.GetResolver());
        behaviour.start = start.Resolve(graph.GetResolver());
        behaviour.end = end.Resolve(graph.GetResolver());
        return playable;
    }
}

Each fragment can be understood as an Asset resource. The object in the scene cannot be found by itself. At this time, it is necessary to

ExposedReference<Transform> This way to find objects in the scene . If the resources in Asset are linked, you can use them normally.

Then you need to create a playable, which is the logic code above the fragment (TrackClip), then obtain the above Behavior, and assign values ​​to the above attributes.

My understanding is that the responsibility of PlayableAsset is to process and modify the fragment (TrackClip) so that the fragment can be hung with objects in the scene and have logical code (behaviour).

3. Create the Behavior of the fragment

public class TransformBehaviour : PlayableBehaviour
{
    public Transform transform;
    public Transform start;
    public Transform end;
    public override void PrepareFrame(Playable playable, FrameData info)
    {
        float t = (float)(playable.GetTime() / playable.GetDuration());
        transform.position = Vector3.Lerp(start.position, end.position, t);
    }
}

Mainly write some logic code.

If you use string, int and other attributes, you need to pay attention to:

  1. Behavior script adds [System.Serializable]

  1. Properties must be public

  1. Use a template when creating a PlayableAsset. For example: public TransformBehaviour behavior = new TransformBehaviour(); ScriptPlayable<TransformBehaviour>.Create(graph,behaviour);

Here is the modified code:

[System.Serializable]
public class TransformPlayableAsset : PlayableAsset
{
    public TransformBehaviour behaviour = new TransformBehaviour();
    public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    {
        var playable = ScriptPlayable<TransformBehaviour>.Create(graph,behaviour);//创建一个带有behaviour的clip
        return playable;
    }
}
[System.Serializable]
public class TransformBehaviour : PlayableBehaviour
{
    public Vector3 pos;
    public override void PrepareFrame(Playable playable, FrameData info)
    {
        
    }
}

Guess you like

Origin blog.csdn.net/u010197227/article/details/129278506