先上效果图。由于中间切换窗口,所以timeline卡了一下:
只需要关注最下侧 start 和 jumptoStart(其它轨道请自动忽略)
在这里插入图片描述上图主要实现timeline的控制: 共20秒,在第5秒加个标记,当播放到第10秒时根据条件决定=》继续播放 or 从第10秒跳到第5秒循环播放.

一.自定义一个轨道Track :TrackAsset 和 PlayableBehaviour
二.自定义轨道上的一个Clip: PlayableAsset 和 PlayableBehaviour
三.自定Track和Clip中的 PlayableBehaviour 可以共用同一个,也可以各用各的。
1.自定义Track中设置绑定和绑定的使用:
在这里插入图片描述
在这里插入图片描述
2.轨道TrackAsset脚本说明

当自定义Clip之间有融合时,也需要声明使用CreateTrackMixer函数。
融合是依据每个Clip的权重weight进行计算此处不再过多描述。
在这里插入图片描述
3.Track中使用的PlayableBehaviour脚本说明:
在这里插入图片描述
4.Clip中的PlayableBehaviour的说明:
在这里插入图片描述
5.Clip中PlayableAsset的
在这里插入图片描述
需要用的重要的api:
获取到PlayableDirector:
PlayableBehaviour:playable.GetGraph().GetResolver() as PlayableDirector;
PlayableAsset:graph.GetResolver() as PlayableDirecto
r
下面附上整体的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

[TrackClipType(typeof(ClipAssets))]
[TrackColor(1f,1f,1f)]
[TrackBindingType(typeof(Light))]
//[System.Serializable]
public class TrackOneAssets : TrackAsset
{
    public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
    {
        var scriptPlayable = ScriptPlayable<TrackBehaviour>.Create(graph, inputCount);
        var b = scriptPlayable.GetBehaviour();
        foreach (var c in GetClips())
        {
            var clipOne = c.asset as ClipAssets;
            if (clipOne.template.type == MarkType.StartMark)
            {
               b.markTime.Add(clipOne.template.markToJump, c.start);
            }   
        }
        return scriptPlayable;
    }
}




using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

// A behaviour that is attached to a playable
[System.Serializable]
public class TrackBehaviour : PlayableBehaviour
{
    public Dictionary<string, double> markTime = new Dictionary<string, double>();
    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        
        var clipCnt = playable.GetInputCount();
        for (int i = 0; i < clipCnt; i++)
        {
            ScriptPlayable<ClipBehaviour> inputScript =(ScriptPlayable<ClipBehaviour>) playable.GetInput(i);
            ClipBehaviour input = inputScript.GetBehaviour();
            var clipWeight = playable.GetInputWeight(i);
            if (clipWeight>0)
            {

                switch (input.type)
                {
                    case MarkType.StartMark:
                        break;
                    case MarkType.JumpToMark:
                        PlayableDirector dir = playable.GetGraph().GetResolver() as PlayableDirector;
                        dir.time = markTime[input.markToJump];
                        break;
                    default:
                        break;
                }
                //if (input.ConditionMat())
                //{
                //    PlayableDirector dir = playable.GetGraph().GetResolver() as PlayableDirector;
                //    dir.time = markTime[input.markToJump];
                //}
            }
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class ClipAssets : PlayableAsset
{
    public ClipBehaviour template;
    // Factory method that generates a playable based on this asset
    public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
    {
        return ScriptPlayable<ClipBehaviour>.Create(graph, template);
    }
    
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

[System.Serializable]
public class ClipBehaviour : PlayableBehaviour
{
    public MarkType type;
    public Condition condition;
    public string markToJump;


    public bool ConditionMat()
    {
        switch (condition)
        {
            case Condition.Always:
                return true;
            case Condition.AnyMonsterAlive:
                // 此处是条件判断
                return false;
            default:
                break;
        }
        return false;
    }
}
public enum Condition
{
    Always,
    AnyMonsterAlive,
}
public enum MarkType
{
    StartMark,
    JumpToMark,
}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐