做網(wǎng)站的IDE百度廣告電話號碼是多少
PlayableTrack 是可自定義播放的軌道。我們可以通過進入軌道后調(diào)用自己的函數(shù)方法,使用起來也是比較順手的。
添加軌道
我們點擊加號添加
這樣就有一個空軌道了,然后我們創(chuàng)建兩個測試腳本。
添加腳本
分別是Playable Behaviour和PlayableAsset腳本。
Asset腳本是可以拖動到軌道上的,通過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的時候,我們實例化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)開始運行Timeline的時候public override void OnGraphStart(Playable playable){Debug.Log("TimeLine 開始播放");}// 當(dāng)停止運行Timeline的時候public override void OnGraphStop(Playable playable){Debug.Log("TimeLine 停止播放");}// 當(dāng)進入?yún)^(qū)域內(nèi)觸發(fā)Playpublic override void OnBehaviourPlay(Playable playable, FrameData info){Debug.Log($"進入滑塊區(qū)域內(nèi){testName},Int:{testInt}");}// 當(dāng)出了區(qū)域觸發(fā),或者開始的時候觸發(fā),或者停止運行的時候public override void OnBehaviourPause(Playable playable, FrameData info){Debug.Log($"Pause{testName},Int:{testInt}");}// 在區(qū)域內(nèi)每個Frame地方都會觸發(fā)public override void PrepareFrame(Playable playable, FrameData info){//Debug.Log("PlayableTest PrepareFrame");}
}
然后我們把PlayableAssetTest 拖入軌道
軌道的長度位置和動畫一樣來調(diào)整就可以了,
運行結(jié)果和總結(jié)
我們運行看看這些函數(shù)是如何觸發(fā)的。
我們看到在編輯器運行后,首先進入的就是OnGraphStart和OnBehaviourPause。
當(dāng)播放到腳本塊后,剛進入就進入了OnBehaviourPlay,當(dāng)播放出了腳本塊后會調(diào)用OnBehaviourPause,當(dāng)整個Timeline結(jié)束后會調(diào)用到OnGraphStop。
基本上都很好理解,只有這個OnBehaviourPause比較特殊,相當(dāng)于Timeline在調(diào)用播放激活的時候會調(diào)用一次,不管是不是在當(dāng)前滑塊范圍內(nèi)(滑塊在第一幀)。然后當(dāng)出了滑塊區(qū)域會調(diào)用一次?;蛘逿imeline被強制停止播放都會。
這里和信號(Signal Track)有很大分別,大家在使用的時候就知道如果某些東西只有在Timeline周期處理的用PlayableTrack比較合適,某些點可以用信號軌道。
PrepareFrame就更好理解了是每一幀都會進入。