public static class PlayableExtend
{
/// <summary>
/// 获取绑定的轨道
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="playableDirector"></param>
/// <param name="trackname"></param>
/// <returns></returns>
public static PlayableBinding GetPlayableBinding(this PlayableDirector playableDirector, string trackname)
{
foreach (PlayableBinding pb in playableDirector.playableAsset.outputs)//每次都用循环获取 这里性能可能会有影响
{
if (pb.streamName.Equals(trackname))
{
return pb;
}
}
throw new NullReferenceException($"未找到对应的[{trackname}]名称!");
}
/// <summary>
/// 一次性获取所有的轨道信息
///
/// </summary>
/// <param name="playableDirector"></param>
/// <returns></returns>
public static Dictionary<string, PlayableBinding> GetPlayableBindings(this PlayableDirector playableDirector)
{
Dictionary<string, PlayableBinding> bindingDict = new Dictionary<string, PlayableBinding>(); //获取TimeLine的轨道
foreach (PlayableBinding pb in playableDirector.playableAsset.outputs)
{
if (!bindingDict.ContainsKey(pb.streamName))
{
bindingDict.Add(pb.streamName, pb);
}
}
return bindingDict;
}
/// <summary>
/// 设置对应的轨道mute
/// </summary>
/// <param name="playableDirector"></param>
/// <param name="trackname"></param>
/// <param name="value"></param>
public static void SetTackMute(this PlayableDirector playableDirector,string trackname,bool value)
{
var ass = playableDirector.GetPlayableBinding(trackname).sourceObject as TrackAsset;
ass.muted = value;
}
/// <summary>
/// 设置对应的轨道mute,并重新绘制
/// 在PlayableBehaviour下 会没有效果并且报错
/// </summary>
/// <param name="playableDirector"></param>
/// <param name="trackname"></param>
/// <param name="value"></param>
public static void SetTackMuteRebuildGraph(this PlayableDirector playableDirector, string trackname, bool value)
{
var ass = playableDirector.GetPlayableBinding(trackname).sourceObject as TrackAsset;
ass.muted = value;
double t0 = playableDirector.time;
playableDirector.RebuildGraph();
playableDirector.time = t0;
playableDirector.Play();
}
/// <summary>
/// 获取轨道对应轨道类型
/// 比如 获取轨道中 AnimationTrackAsset 对其操作
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="playableDirector"></param>
/// <param name="trackName"></param>
/// <returns></returns>
public static T GetTrackAsset<T>(this PlayableDirector playableDirector,string trackName) where T: TrackAsset
{
return playableDirector.GetPlayableBinding(trackName).sourceObject as T;
}
/// <summary>
/// 绑定对应的物体
/// 比如说重新绑定 animatortrack 中的对象
/// 传入 轨道名称 和对象对应的动画器(Animator)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="playableDirector"></param>
/// <param name="trackName"></param>
/// <param name="_object"></param>
public static void SetBind<T>(this PlayableDirector playableDirector,string trackName,T _object) where T : Behaviour
{
playableDirector.SetGenericBinding(playableDirector.GetPlayableBinding(trackName).sourceObject, _object);
}
}
使用的版本 是2019.4 LTS
TimeLine 版本是 1.6.2
刚好用到 TimeLine 来做一下特殊击杀
整理了一下本次常用的方法,写成了扩展类
有些地方不是很了解,在这里抛砖引玉,有问题一起讨论。