本文主要是介绍【TimeLine】PlayableTrack的使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
版本unity2017.2.2+之后,TimeLine中添加的PlayableTrack只能是Playable Asset C# Script
TimeLine中的PlayableTrack可添加的clip类型
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;[System.Serializable]
public class NewPlayableAsset : PlayableAsset
{// ExposedReference类型使其变量可以选择Hirearchy面板中的对象// 在PlayableAsset中,仅仅使用public修饰的变量,只可以选择Project文件夹中的对象public ExposedReference<PlayableDirector> playableDir;public ExposedReference<Animator> ani;// 继承PlayableAsset后默认创建的工厂方法public override Playable CreatePlayable(PlayableGraph graph, GameObject go) {// 继承PlayableBehaviour C# Script的类对象NewPlayableBehaviour npb = new NewPlayableBehaviour();// graph.GetResolver():Returns the table used by the graph to resolve ExposedReferences.npb.playableDir = playableDir.Resolve(graph.GetResolver());npb.ani = ani.Resolve(graph.GetResolver());// 自定以的返回结果需要使用ScriptPlayable<T>来闯将对象return ScriptPlayable<NewPlayableBehaviour>.Create(graph, npb);// 默认返回的是由graph创建的Playable对象//return Playable.Create(graph);}
}
PlayableTrack的具体调用实现,还需要另外一个脚本来实现,具体功能和调用时机如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
// A behaviour that is attached to a playable
public class NewPlayableBehaviour : PlayableBehaviour
{public PlayableDirector playableDir;public Animator ani;// Called when the owning graph starts playingpublic override void OnGraphStart(Playable playable) {}// Called when the owning graph stops playingpublic override void OnGraphStop(Playable playable) {}// Called when the state of the playable is set to Playpublic override void OnBehaviourPlay(Playable playable, FrameData info) {}// Called when the state of the playable is set to Pausedpublic override void OnBehaviourPause(Playable playable, FrameData info) {}// Called each frame while the state is set to Playpublic override void PrepareFrame(Playable playable, FrameData info) {// 遍历playableDir中的TimeLine中所有的Trackforeach (PlayableBinding item in playableDir.playableAsset.outputs){// 所有track的名字//Debug.Log(item.streamName);if (item.streamName == "Animation Track1"){// 绑定一个对象,绑定到该TrackplayableDir.SetGenericBinding(item.sourceObject, playableDir.gameObject);}}}
}
这篇关于【TimeLine】PlayableTrack的使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!