Timeline PlayableBehaviour和PlayableAsset

做个例子,在timeline中,控制某text在不同时间,更新text内容并显示

 

Timeline PlayableBehaviour和PlayableAsset_第1张图片

上图的PlayableTrack即是PlayableBehaviour

而后面的内容,既是PlayableAsset

 

先将PlayableTrack拖到timeline前面

然后将创建好的PlayableAsset拖拽到PlayableTrack轨道上

 

接下来分别创建这两个脚本

Timeline PlayableBehaviour和PlayableAsset_第2张图片

 

 

在创建PlayableBehaviour时,会自动生成一些方法,如下

// Called when the owning graph starts playing

public override void OnGraphStart(Playable playable) {

 

}

 

// Called when the owning graph stops playing

public override void OnGraphStop(Playable playable) {

}

 

// Called when the state of the playable is set to Play

public override void OnBehaviourPlay(Playable playable, FrameData info) {

}

 

// Called when the state of the playable is set to Paused

public override void OnBehaviourPause(Playable playable, FrameData info) {

}

 

// Called each frame while the state is set to Play

public override void PrepareFrame(Playable playable, FrameData info) {

}

 

我们创建文件PlayableTrackTalk修改内容如下

using UnityEngine.Playables;

using UnityEngine.UI;

 

// A behaviour that is attached to a playable

public class PlayableTrackTalk : PlayableBehaviour

{

public Text talkText1;

public string talkStr1;

 

// Called when the state of the playable is set to Play

public override void OnBehaviourPlay(Playable playable, FrameData info) {

talkText1.text = talkStr1;

}

}

 

 

接下来创建PlayableAsset时会自动生产以下内容

using UnityEngine.Playables;

 

[System.Serializable]

public class NewPlayableAsset : PlayableAsset

{

// Factory method that generates a playable based on this asset

public override Playable CreatePlayable(PlayableGraph graph, GameObject go) {

return Playable.Create(graph);

}

}

 

我们创建文件PlayableAssetTalk并修改内容如下

using UnityEngine.Playables;

using UnityEngine.UI;

 

[System.Serializable]

public class PlayableAssetTalk : PlayableAsset

{

public ExposedReference talkText;

public string talkStr;

 

// Factory method that generates a playable based on this asset

public override Playable CreatePlayable(PlayableGraph graph, GameObject go) {

return Playable.Create(graph);

}

}

 

我们要通过ExposedReference来将一些unity控件,显示在inspector上,并获取它

可以看见,我们可以将场景内的控件拖进来了,

Timeline PlayableBehaviour和PlayableAsset_第3张图片

 

然而此时,将这个PlayableAsset拖到轨道上并为控件赋值,点运行,结果并没有什么卵用=。=

 

接下来我们需要通过在PlayableAsset中实例化对应PlayableBehaviour【即PlayableTrackTalk】,并将,PlayableAsset的值赋予PlayableBehaviour,

 

using UnityEngine.Playables;

using UnityEngine.UI;

 

[System.Serializable]

public class PlayableAssetTalk : PlayableAsset

{

public ExposedReference talkText;

public string talkStr;

 

// Factory method that generates a playable based on this asset

public override Playable CreatePlayable(PlayableGraph graph, GameObject go) {

PlayableTrackTalk talkPlayable = new PlayableTrackTalk();

talkPlayable.talkText1 = talkText.Resolve(graph.GetResolver());

talkPlayable.talkStr1 = talkStr;

return ScriptPlayable.Create(graph, talkPlayable);

}

}

 

 

同时,要使控件在轨道上可以被playableTrack控制,我们需要在PlayableAsset的CreatePlayable中,实例化对应playableTrack【即PlayableTrackTalk】,并将PlayableAsset中的对应值赋予playableTrack,

赋值时要使用Resolve(graph.GetResolver())来为unity的控件赋值

talkPlayable.talkText1 = talkText.Resolve(graph.GetResolver());

接下来,将PlayableTrackTalk拖到Timeline上,在将PlayableAssetTalk拖到PlayableTrackTalk的轨道上,赋值,运行即可

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Timeline PlayableBehaviour和PlayableAsset)