Timeline安装
使用 Unity 的时间轴可创建影片内容、游戏序列、音频序列和复杂的粒子效果。
安装:
编辑器版本:
- 2019.1 and later(推荐)
时间轴资源
创建Timeline
Assets>Create>Timeline
录制的帧动画会会保存为时间轴资源的子项
时间轴实例
在场景中将时间轴资源与 Playable Director
组件相关联可创建时间轴实例
时间轴编辑器窗口
轨道属性
- 录制按钮,用于关键帧录制
- 标记按钮
- 锁轨按钮,锁住之后,当前轨道不可以编辑
- 禁用轨道,禁用之后,当前轨道绑定的动画、声音等不会起作用
轨道类型
Track Group
轨道打组,类似文件夹
Activation Track
控制一个物体的显示隐藏
Animation Track
动画轨道,剪辑动画片段
Audio Track
音频轨道,控制音频的播放暂停
Control Track
控制物体的显示与隐藏,控制粒子系统的播放,可以拖拽大部分场景中的物体,也可以拖拽大部分Assets目录下面的prefab
Signal Track
信号轨道,本质是一个事件发射器,需要在Assets目录下面创建Signal
资源,然后在场景中需要接收信号的对象上面添加Signal Receiver
组件
一个游标,表示一个触发信号
Playable Track
用户自定义轨道,自定义轨道脚本是成对使用的,分别是PlayableAsset
,PlayableBehaviour
PlayableAsset 负责 PlayableBehaviour对象的实例化
PlayableBehaviour 负责监听动画播放、暂停等,可以在对应的事件中编写逻辑
[System.Serializable]
public class BoxPlayableAsset : PlayableAsset
{
public ExposedReference<Transform> target;
// Factory method that generates a playable based on this asset
public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
{
var p = ScriptPlayable<BoxPlayableBehaviour>.Create(graph);
p.GetBehaviour().target = target.Resolve(graph.GetResolver());
return p;
//return Playable.Create(graph);
}
}
public class BoxPlayableBehaviour : PlayableBehaviour
{
public Transform target;
// Called when the owning graph starts playing
public override void OnGraphStart(Playable playable)
{
}
// Called when the owning graph stops playing
public override void OnGraphStop(Playable playable)
{
}
// Called when the state of the playable is set to Play
public override void OnBehaviourPlay(Playable playable, FrameData info)
{
target.localPosition = new Vector3(target.localPosition.x, target.localPosition.y, -0.0014f);
Debug.Log(target.localPosition);
}
// Called when the state of the playable is set to Paused
public override void OnBehaviourPause(Playable playable, FrameData info)
{
}
// Called each frame while the state is set to Play
public override void PrepareFrame(Playable playable, FrameData info)
{
}
}
Playable Director组件
Playable
时间轴资源
Update Method
DSP Lock: 数字信号处理器,使用处理音频的时钟
Game Time: 与Game时间相同,受时间缩放影响
Unscaled Game Time: 与Game时间相同,不受时间缩放影响
Manual: 不使用Game时间,脚本设置
Play On Awake
启动程序时自动播放
Wrap Mode
Hold: 只播放一次,并停留在最后一帧
Loop: 循环播放
None: 只播放一次
Initial Time
时间轴开始播放的时间(以秒为单位)
Bindings
绑定的资产
设置播放速度
using UnityEngine.Playables;
void PlayAtSpeed(PlayableDirector playableDirector, float speed)
{
playableDirector.RebuildGraph(); // the graph must be created before getting the playable graph
playableDirector.playableGraph.GetRootPlayable(0).SetSpeed(speed);
playableDirector.Play();
}