TimeLine 中新增自定义轨道的话,需要重写三个类
1.TrackAsset
[TrackColor(0.6f, 0.9f, 0.2f)] //轨道颜色
[TrackClipType(typeof(Audio))] //轨道中可以添加的具体资源类型,这里也是自己重写的类
public class AudioTrack : TrackAsset
{
public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
{
return ScriptPlayable<AudioTrackBehaviour>.Create(graph, inputCount);
}
}
对应的timeline中如图所指
2.PlayableAsset
[Serializable]
public class Audio : PlayableAsset
{
public new string name;
// Factory method that generates a playable based on this asset
public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
{
var behaviour = new AudioBehaviour();
behaviour.asset = this;
put = outputs;
return ScriptPlayable<AudioBehaviour>.Create(graph, behaviour);
}
public IEnumerable<PlayableBinding> put;
}
3.PlayableBehaviour
具体的功能实现都在这个类中
public class AudioBehaviour : PlayableBehaviour
{
public Audio asset;
public bool isInit = false;
private static FightEditorSoundManager soundManager;
/// <summary>
/// 当timeline播放,进入到该Audio中的时候 可重写方法还有其他,可以依据自己的需求写
/// </summary>
/// <param name="playable"></param>
/// <param name="info"></param>
public override void OnBehaviourPlay(Playable playable, FrameData info)
{
if (asset.name == null)
{
return;
}
if (soundManager == null)
soundManager = new FightEditorSoundManager();
soundManager.PlayAudioByEvent(asset.name);
}
/// <summary>
/// 当timeline丢失的时候
/// </summary>
/// <param name="playable"></param>
public override void OnPlayableDestroy(Playable playable)
{
base.OnPlayableDestroy(playable);
if (soundManager != null)
{
soundManager.Dispose();
soundManager = null;
}
}
}
接口:
PrepareFrame //循环播放刷新时调用
OnBehaviourPlay //进入该行为片段时调用
OnBehaviourPause //退出该行为片段时调用
OnPlayableCreate //创建时、编辑器模式选中tiemline时
OnGraphStart //创建后、播放时
OnGraphStop //停止时,编辑器模式选中其他对象时
OnPlayableDestroy //同上,在停止后调用