动态创建TimeLine
//添加TimeLine导演组件
PlayableDirector director;
if (player.GetComponent<PlayableDirector>())
{
director = player.GetComponent<PlayableDirector>();
}
else
{
director = player.AddComponent<PlayableDirector>();
}
//创建TimeLine并赋值给导演类
TimelineAsset asset = TimelineAsset.CreateInstance<TimelineAsset>();
director.playableAsset = asset;
director.playOnAwake = false;
动态创建动画轨道并添加内容
//创建动画轨道
AnimationTrack animationTrack = asset.CreateTrack<AnimationTrack>();
animationTrack.trackOffset = TrackOffset.ApplySceneOffsets;
//通过导演组件把动画轨道跟人物绑定
director.SetGenericBinding(animationTrack, player);
//给轨道上添加内容
TimelineClip timelineClip = animationTrack.CreateClip<AnimationPlayableAsset>();
AnimationPlayableAsset apa = timelineClip.asset as AnimationPlayableAsset;
apa.clip = Resources.Load<AnimationClip>(skill.animPath);
apa.position = player.transform.position;
//设置动画开始播放时间
timelineClip.start = skill.animBeginTime;
//设置动画播放时长
timelineClip.duration = skill.animDurationTime;
动态创建Control轨道并添加内容
//创建Control轨道
ControlTrack control = asset.CreateTrack<ControlTrack>("control");
//创建Control内容
var timeclip = control.CreateClip<ControlPlayableAsset>();
var clip = timeclip.asset as ControlPlayableAsset;
//为Control内容赋值一个有继承ITimeControl组件的GameObject
clip.sourceGameObject.defaultValue = effectMgr;
//(具体内容写在继承ITimeControl组件里)
//设置开始时间
timeclip.start = effect.effectBeginTime;
//设置时长
timeclip.duration = effect.effectDurationTime;
动态创建声音轨道并添加内容
//创建声音轨道
AudioTrack audioTrack = asset.CreateTrack<AudioTrack>();
//通过导演组件为声音轨道绑定播放器
//(我们发现如果不为声音轨道绑定播放器,他在执行时会自己创建并在播放完后销毁)
//director.SetGenericBinding(audioTrack, player);
//创建声音内容
TimelineClip timelineClip = audioTrack.CreateClip<AudioPlayableAsset>();
AudioPlayableAsset audio = timelineClip.asset as AudioPlayableAsset;
//为内容赋值
audio.clip = Resources.Load<AudioClip>(skill.audioPath);
//设置播放开始时间和时长
timelineClip.start = skill.audioBeginTime;
timelineClip.duration = skill.audioDurationTime;
动态创建Signal事件轨道并添加内容
//创建Signal事件轨道
SignalTrack signalTrack = asset.CreateTrack<SignalTrack>();
//添加组件
SignalReceiver receiver = player.AddComponent<SignalReceiver>();
//把组件绑定到轨道上
director.SetGenericBinding(signalTrack, player);
//创建事件
SignalAsset signalAsset = new SignalAsset();
signalAsset.name = "buff";
//在轨道上指定时间添加事件
SignalEmitter signal = signalTrack.CreateMarker<SignalEmitter>(skill.buffTime);
signal.asset = signalAsset;
//创建委托绑定事件
receiver.AddReaction(signal.asset, new UnityEvent());
//给委托添加监听
receiver.GetReaction(signalAsset).AddListener(() =>
{
//这里写具体内容哦
});
动态添加虚拟相机轨道并添加内容
//创建虚拟相机轨道
CinemachineTrack cinemachineTrack = timelineAssets.CreateTrack<CinemachineTrack>();
//绑定控制的摄像机
playableDirector.SetGenericBinding(cinemachineTrack, cinemachineBrain);
//给轨道上添加内容
TimelineClip timelineClip1 = cinemachineTrack.CreateDefaultClip() as TimelineClip;
CinemachineShot cinemachineShot = timelineClip1.asset as CinemachineShot;
//创建虚拟相机
var setCam1 = new ExposedReference<CinemachineVirtualCameraBase>();
GameObject go1 = new GameObject("Cinemachine");
go1.AddComponent<CinemachineVirtualCamera>();
setCam1.defaultValue = go1.GetComponent<CinemachineVirtualCamera>();
//赋值
cinemachineShot.VirtualCamera = setCam1;