上图是利用代码制作的,下图是原来的样子:
如下代码是动态创建各种Timeline轨道的代码:(控制角色碰撞到Cube 触发以下的Timeline动画)
using Cinemachine;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Playables;
using UnityEngine.Timeline;
public class DTTimeLine : MonoBehaviour
{
//相机二跟随和看向的对象
public GameObject enemy;
//添加TimeLine导演组件
PlayableDirector director;
// Start is called before the first frame update
//[System.Obsolete] 是一个特性(attribute),用于标记方法、属性、类或字段已经过时,
不推荐使用的注解。
[System.Obsolete]
void Start()
{
if (GetComponent<PlayableDirector>())
{
director = GetComponent<PlayableDirector>();
}
else
{
director = gameObject.AddComponent<PlayableDirector>();
}
//用于创建一个新的时间轴资源(TimelineAsset)实例的方法
TimelineAsset asset = TimelineAsset.CreateInstance<TimelineAsset>();
//将创建的时间轴资源(asset)赋值给 PlayableDirector 组件的 playableAsset 属性,
//从而将时间轴资源与 PlayableDirector 关联起来。
director.playableAsset = asset;
//在游戏对象激活时不会自动播放与 PlayableDirector 关联的时间轴
director.playOnAwake = false;
//创建动画轨道
AnimationTrack animationTrack = asset.CreateTrack<AnimationTrack>();
//animationTrack.trackOffset 用于指定轨道偏移的方式
//TrackOffset.ApplySceneOffsets 将轨道偏移设置为应用场景偏移
animationTrack.trackOffset = TrackOffset.ApplySceneOffsets;
//通过导演组件把动画轨道跟人物绑定
director.SetGenericBinding(animationTrack, gameObject);
//给轨道上添加内容
TimelineClip timelineClip = animationTrack.CreateClip<AnimationPlayableAsset>();
AnimationPlayableAsset apa = timelineClip.asset as AnimationPlayableAsset;
apa.clip = Resources.Load<AnimationClip>("Day8/Love01");//填写动画路径
apa.position = gameObject.transform.position;
//设置动画开始播放时间
timelineClip.start =0;
//设置动画播放时长
timelineClip.duration = 5;
//给轨道上添加内容2
TimelineClip timelineClip1 = animationTrack.CreateClip<AnimationPlayableAsset>();
AnimationPlayableAsset apa1 = timelineClip1.asset as AnimationPlayableAsset;
apa1.clip = Resources.Load<AnimationClip>("Day8/Catch03");//填写动画路径
apa1.position = gameObject.transform.position;
//设置动画开始播放时间
timelineClip1.start = 5;
//设置动画播放时长
timelineClip1.duration = 10;
//创建Control轨道
ControlTrack control = asset.CreateTrack<ControlTrack>("control");
//创建Control内容
var timeclip = control.CreateClip<ControlPlayableAsset>();
var clip = timeclip.asset as ControlPlayableAsset;
// 创建一个用于容纳粒子系统的空GameObject
GameObject particleObject = new GameObject("ParticleEffect");
// 在空GameObject上添加粒子特效组件(例如ParticleSystem)
ParticleSystem particleSystem = particleObject.AddComponent<ParticleSystem>();
// 获取粒子系统的Renderer组件
ParticleSystemRenderer particleRenderer = particleObject.GetComponent<ParticleSystemRenderer>();
// 创建一个基本的材质球
Material particleMaterial = new Material(Shader.Find("Standard"));
//material 赋值
particleRenderer.material = particleMaterial;
//重复播放关闭
particleObject.GetComponent<ParticleSystem>().loop = false;
// 将粒子特效的Prefab或GameObject赋值给粒子特效组件
particleSystem.gameObject.GetComponent<ParticleSystem>().Play(); // 播放粒子特效
// 将粒子系统的GameObject赋值给Control Clip的sourceGameObject
clip.sourceGameObject.exposedName = "ParticleObject";
clip.sourceGameObject.defaultValue = particleObject;
//设置开始时间
timeclip.start = 0;
//设置时长
timeclip.duration = 15;
//创建声音轨道
AudioTrack audioTrack = asset.CreateTrack<AudioTrack>();
//创建声音内容
TimelineClip timelineClip2 = audioTrack.CreateClip<AudioPlayableAsset>();
AudioPlayableAsset audio2 = timelineClip2.asset as AudioPlayableAsset;
//为内容赋值
audio2.clip = Resources.Load<AudioClip>("Day8/music");
//设置播放开始时间和时长
timelineClip2.start = 0;
timelineClip2.duration = 10;
//创建Signal事件轨道
SignalTrack signalTrack = asset.CreateTrack<SignalTrack>();
//添加组件
SignalReceiver receiver = gameObject.AddComponent<SignalReceiver>();
//把组件绑定到轨道上
director.SetGenericBinding(signalTrack, gameObject);
//创建事件
SignalAsset signalAsset = new SignalAsset();
signalAsset.name = "buff";
//在轨道上指定时间添加事件(用于在 signalTrack 事件轨道上创建一个 SignalEmitter 标记,
//并将其放置在时间轴上的时间点 5 处。)
SignalEmitter signal = signalTrack.CreateMarker<SignalEmitter>(5);
signal.asset = signalAsset;
//创建委托绑定事件
receiver.AddReaction(signal.asset, new UnityEvent());
//给委托添加监听
receiver.GetReaction(signalAsset).AddListener(() =>
{
print(111);
});
//创建虚拟相机轨道
CinemachineTrack cinemachineTrack = asset.CreateTrack<CinemachineTrack>();
//绑定控制的摄像机
director.SetGenericBinding(cinemachineTrack, Camera.main.gameObject.GetComponent<CinemachineBrain>());
//给轨道上添加内容(跟随主角)
TimelineClip timelineClip3 = cinemachineTrack.CreateDefaultClip() as TimelineClip;
CinemachineShot cinemachineShot = timelineClip3.asset as CinemachineShot;
GameObject go1 = GameObject.Find("Cinemachine");
//跟随看向
CinemachineVirtualCamera cinemachineVirtualCamera = go1.GetComponent<CinemachineVirtualCamera>();
cinemachineShot.VirtualCamera.defaultValue = cinemachineVirtualCamera;
cinemachineVirtualCamera.LookAt = transform;
cinemachineVirtualCamera.Follow = transform;
timelineClip3.start = 0;
timelineClip3.duration = 5;
//给轨道上添加内容2(跟随玩家)
TimelineClip timelineClip4 = cinemachineTrack.CreateDefaultClip() as TimelineClip;
CinemachineShot cinemachineShot2 = timelineClip4.asset as CinemachineShot;
GameObject go2 = GameObject.Find("VirtualCamera");
//跟随看向
CinemachineVirtualCamera cinemachineVirtualCamera2 = go2.GetComponent<CinemachineVirtualCamera>();
cinemachineShot2.VirtualCamera.defaultValue = cinemachineVirtualCamera2;
cinemachineVirtualCamera2.LookAt = enemy.transform;
cinemachineVirtualCamera2.Follow = enemy.transform;
timelineClip4.start = 5;
timelineClip4.duration = 10;
}
// Update is called once per frame
void Update()
{
//移动控制主角
transform.Translate(Vector3.forward * 10 * Time.deltaTime * Input.GetAxis("Vertical"));
transform.Rotate(Vector3.up * 100 * Time.deltaTime * Input.GetAxis("Horizontal"));
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Cube")
{
director.Play();
}
}
}
标签:片段,创建,Timeline,gameObject,director,添加,asset,轨道,GameObject
From: https://blog.csdn.net/djrjnfjdjejb/article/details/140109391