做網(wǎng)站的IDE百度廣告電話號碼是多少
PlayableTrack 是可自定義播放的軌道。我們可以通過進(jìn)入軌道后調(diào)用自己的函數(shù)方法,使用起來也是比較順手的。
添加軌道
我們點(diǎn)擊加號添加
這樣就有一個(gè)空軌道了,然后我們創(chuàng)建兩個(gè)測試腳本。
添加腳本
分別是Playable Behaviour和PlayableAsset腳本。
Asset腳本是可以拖動(dòng)到軌道上的,通過Asset腳本來調(diào)用Behaviour腳本的方法,直接貼上腳本:
首先是PlayableAsset
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;[System.Serializable]
public class PlayableAssetTest : PlayableAsset
{public string testName;public int testInt;// Factory method that generates a playable based on this assetpublic override Playable CreatePlayable(PlayableGraph graph, GameObject go){//return Playable.Create(graph);PlayableTest t = new PlayableTest();t.testName = testName;t.testInt = testInt;return ScriptPlayable<PlayableTest>.Create(graph, t);}
}
他在CreatePlayble的時(shí)候,我們實(shí)例化Playable Behaviour腳本,并傳入想傳入的參數(shù)。
Playable Behaviour的代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;// A behaviour that is attached to a playable
public class PlayableTest : PlayableBehaviour
{public string testName;public int testInt;// 當(dāng)開始運(yùn)行Timeline的時(shí)候public override void OnGraphStart(Playable playable){Debug.Log("TimeLine 開始播放");}// 當(dāng)停止運(yùn)行Timeline的時(shí)候public override void OnGraphStop(Playable playable){Debug.Log("TimeLine 停止播放");}// 當(dāng)進(jìn)入?yún)^(qū)域內(nèi)觸發(fā)Playpublic override void OnBehaviourPlay(Playable playable, FrameData info){Debug.Log($"進(jìn)入滑塊區(qū)域內(nèi){testName},Int:{testInt}");}// 當(dāng)出了區(qū)域觸發(fā),或者開始的時(shí)候觸發(fā),或者停止運(yùn)行的時(shí)候public override void OnBehaviourPause(Playable playable, FrameData info){Debug.Log($"Pause{testName},Int:{testInt}");}// 在區(qū)域內(nèi)每個(gè)Frame地方都會(huì)觸發(fā)public override void PrepareFrame(Playable playable, FrameData info){//Debug.Log("PlayableTest PrepareFrame");}
}
然后我們把PlayableAssetTest 拖入軌道
軌道的長度位置和動(dòng)畫一樣來調(diào)整就可以了,
運(yùn)行結(jié)果和總結(jié)
我們運(yùn)行看看這些函數(shù)是如何觸發(fā)的。
我們看到在編輯器運(yùn)行后,首先進(jìn)入的就是OnGraphStart和OnBehaviourPause。
當(dāng)播放到腳本塊后,剛進(jìn)入就進(jìn)入了OnBehaviourPlay,當(dāng)播放出了腳本塊后會(huì)調(diào)用OnBehaviourPause,當(dāng)整個(gè)Timeline結(jié)束后會(huì)調(diào)用到OnGraphStop。
基本上都很好理解,只有這個(gè)OnBehaviourPause比較特殊,相當(dāng)于Timeline在調(diào)用播放激活的時(shí)候會(huì)調(diào)用一次,不管是不是在當(dāng)前滑塊范圍內(nèi)(滑塊在第一幀)。然后當(dāng)出了滑塊區(qū)域會(huì)調(diào)用一次?;蛘逿imeline被強(qiáng)制停止播放都會(huì)。
這里和信號(Signal Track)有很大分別,大家在使用的時(shí)候就知道如果某些東西只有在Timeline周期處理的用PlayableTrack比較合適,某些點(diǎn)可以用信號軌道。
PrepareFrame就更好理解了是每一幀都會(huì)進(jìn)入。