前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >TimeLine⭐️三、新版TimeLine来执行脚本

TimeLine⭐️三、新版TimeLine来执行脚本

作者头像
星河造梦坊官方
发布2024-08-15 17:21:04
1620
发布2024-08-15 17:21:04
举报
文章被收录于专栏:星河造梦坊专栏

🟥 实现目标:

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

🟧 控制实现

1、给物体新建TimeLine

2、给该TimeLine创建PlayableTrack

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

注意: 老版方法不可往TimeLine轨道上的脚本拖拽物体。 新版脚本的写法,允许我们在TimeLine上往该脚本拖拽物体了。

代码语言:javascript
复制
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脚本初始化、代码控制调用。

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

代码语言:javascript
复制
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的控制方法

代码语言:javascript
复制
//暂停
GetComponent<PlayableDirector>().Pause();

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

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

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

大家还有什么问题,欢迎在下方留言!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 🟥 实现目标:
  • 🟧 控制实现
  • 🟨 附加:PlayableDirector的控制方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档