转至https://skode.blog.csdn.net/article/details/105835083
一、实现目标:
- 使用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();
下面是旧版 执行脚本
一、目标
在我们使用TimeLine过程中,也会希望通过TimeLine在某一时刻运行脚本方法。
注意:
- 这儿不是通过脚本控制TimeLine,是通过TimeLine来控制脚本,执行方法。
- 在TimeLine预览时,也能执行该脚本。
二、实现步骤
1、添加 PlayableTrack 轨道
TimeLine点击 “+” 添加 PlayableTrack 轨道
2、将以下代码添加到 PlayableTrack 轨道
通过继承了 BasicPlayableBehaviour 的脚本,我们根据它的调用,便能实现TimeLine控制其他脚本,也能直接控制物体。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
public class Skode_PlayableTrack : BasicPlayableBehaviour
{
public GameObject obj;
public override void OnGraphStart(Playable playable)
{
//该调用在TimeLine开始播放时执行,即使时间轴没进入该代码控制的区域
Debug.Log("Graph start");
}
public override void OnGraphStop(Playable playable)
{
//该调用在TimeLine暂停时执行,即使时间轴没进入该代码控制的区域
Debug.Log("Graph stop");
}
public override void PrepareFrame(Playable playable, FrameData info)
{
//当时间轴在该轨道片段时,便一直执行。
Debug.Log("1");
}
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
//当时间轴在该轨道片段时,便一直执行。
Debug.Log("2");
}
public override void OnBehaviourPlay(Playable playable, FrameData info)
{
//该调用在TimeLine开始播放时执行,只有时间轴进入该代码控制的区域时(在区域中暂停、进入该区域那一刻),才执行
Debug.Log("Play State Playing");
GameObject.Find("Mori").transform.GetComponent<testtt>().skode_hi();
}
public override void OnBehaviourPause(Playable playable, FrameData info)
{
//该调用在TimeLine暂停时执行,只有时间轴进入该代码控制的区域时(在区域中暂停、走出该区域那一刻),才执行
Debug.Log("Play State Paused");
}
}