TimeLine丨(三)新版TimeLine来执行脚本

大家好,我是SKODE。

小提示:选中左侧目录,可快速找到所需内容

本系列博客地址:传送门

一、实现目标:

  • 使用TimeLine,控制一个脚本在TimeLine轨道上播放,并打印该脚本的生命周期
  • 实现一个小demo,让TimeLine上的脚本,在合适时机改变场景中的文字。

二、控制实现

1、给物体新建TimeLine

2、给该TimeLine创建PlayableTrack

3、新建如下脚本,拖到该条轨道上。

注意:

老版方法不可往TimeLine轨道上的脚本拖拽物体。

新版脚本的写法,允许我们在TimeLine上往该脚本拖拽物体了。

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.UI;

public class PlayableAssetTest : PlayableAsset
{
    public ExposedReference<Text> text_Asset;
    public string str_Asset;

    public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
    {
        var playableTest = ScriptPlayable<PlayableBehaviourTest>.Create(graph);

        //将 ExposedReference<Text> 类型转化成 Text 类型
        playableTest.GetBehaviour().text_Behav = text_Asset.Resolve(graph.GetResolver());
        playableTest.GetBehaviour().str_Behav = str_Asset;

        return playableTest;
    }
}

4、新建如下脚本,放在Asset即可

该脚本实现了控制TimeLine的生命周期回调,与MonoBehaviour类似。

不同的是Mono直接挂在场景物体上执行生命周期函数,PlayableBehaviour必须通过上面PlayableAsset脚本初始化、代码控制调用。

下面列举出了各个回调函数的执行时机。

using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.UI;

public class PlayableBehaviourTest : PlayableBehaviour
{
    public Text text_Behav;
    public string str_Behav;

    #region MyRegion

    //在调用以下API时执行:
    //该TimeLine Awake播放时、从头Play时
    public override void OnPlayableCreate(Playable playable)
    {
        Debug.Log("OnPlayableCreate");
    }

    //在如下场景执行:
    //当从头播放该TimeLine时
    //当时间轴在整个TimeLine:Play、Resume时
    public override void OnGraphStart(Playable playable)
    {
        Debug.Log("OnGraphStart");
    }

    //在如下场景执行:
    //当时间轴在该代码区域:Pause、Stop时
    //当从头播放该TimeLine时执行一次
    //当时间轴驶出该代码区域时执行一次
    public override void OnBehaviourPause(Playable playable, FrameData info)
    {
        Debug.Log("OnBehaviourPause");
    }

    //在如下场景执行:
    //当时间轴在该代码区域:Play、Resume时
    public override void OnBehaviourPlay(Playable playable, FrameData info)
    {
        Debug.Log("OnBehaviourPlay");

        if (text_Behav != null)
        {
            text_Behav.text = str_Behav;
        }
    }

    //当时间轴在该代码片段时,每帧执行
    public override void PrepareFrame(Playable playable, FrameData info)
    {
        Debug.Log("PrepareFrame");
    }

    //当时间轴在该代码片段时,每帧执行
    public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    {
        Debug.Log("ProcessFrame");
    }

    //在如下场景执行:
    //当时间轴整个TimeLine:Pause、Stop 时
    public override void OnGraphStop(Playable playable)
    {
        Debug.Log("OnGraphStop");
    }

    //在如下场景执行:
    //当时间轴整个TimeLine:Stop 时
    //该TimeLine播放结束时
    public override void OnPlayableDestroy(Playable playable)
    {
        Debug.Log("OnPlayableDestroy");
    }

    #endregion
}

5、制作UI

在场景中新建Text,并赋值给TimeLine的 PlayableAssetTest 脚本,运行查看效果

三、附加:PlayableDirector的控制方法

//暂停
GetComponent<PlayableDirector>().Pause();

//继续播放
GetComponent<PlayableDirector>().Resume();

//停止播放(再次播放会从起点开始播放)
GetComponent<PlayableDirector>().Stop();

//开始播放
GetComponent<PlayableDirector>().Play();

一起成长,

我是SKODE。

原创文章 347 获赞 98 访问量 23万+

猜你喜欢

转载自blog.csdn.net/weixin_38239050/article/details/105835083