Timeline 理解与API解读
Timeline自有组件:
- Activation Track
- Animation Track
- Audio Track
- Control Track
- Playable Track
- Track Group
比较有难于理解的是Control Track和Playable Track,建议不要使用Unity2018.2.0的版本,在使用Timeline的时候会遇到一些一样不到的bug。PlayableGraph提供了Timeline的可视化图形结构,暂不支持编辑功能。如图:
Playable Track使Timeline具备可扩展性。
using UnityEngine;
using UnityEngine.Playables;
[System.Serializable]
public class LightControlAsset : PlayableAsset
{
/// <summary>
/// 由于PlayableAsset是个资源,它不能直接引用场景中的对象。此时ExposedReference会充当一个约定,表示在调用CreatePlayable时会解析一个对象。
/// </summary>
public ExposedReference<Light> light;
public Color color = Color.gray;
public float intensity = 1f;
// Factory method that generates a playable based on this asset
public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
{
Debug.Log("CreatePlayable");
var playable = ScriptPlayable<LightControlBehaviour>.Create(graph);
var lightControlBehaviour = playable.GetBehaviour();
lightControlBehaviour.light = light.Resolve(graph.GetResolver());
lightControlBehaviour.color = color;
lightControlBehaviour.intensity = intensity;
return playable;
}
}
using UnityEngine;
using UnityEngine.Playables;
/// <summary>
/// 目的:确定需要修改Light组件的哪些属性。而且PlayableBehaviour拥有名为ProcessFrame的方法可供重写。
/// </summary>
// A behaviour that is attached to a playable
public class LightControlBehaviour : PlayableBehaviour
{
public Light light = null;
public Color color = Color.white;
public float intensity = 1;
// Called when the owning graph starts playing
public override void OnGraphStart(Playable playable)
{
Debug.Log("OnGraphStart");
}
// Called when the owning graph stops playing
public override void OnGraphStop(Playable playable)
{
Debug.Log("OnGraphStop");
}
// Called when the state of the playable is set to Play
public override void OnBehaviourPlay(Playable playable, FrameData info)
{
Debug.Log("OnBehaviourPlay");
}
// Called when the state of the playable is set to Paused
public override void OnBehaviourPause(Playable playable, FrameData info)
{
Debug.Log("OnBehaviourPause");
}
// Called each frame while the state is set to Play
public override void PrepareFrame(Playable playable, FrameData info)
{
Debug.Log("PrepareFrame");
}
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
if (light != null)
{
light.color = color;
light.intensity = intensity;
}
Debug.Log("ProcessFrame");
}
}
ControlTrack暂时没有深入研究。查阅资料说明它主要用来控制与时间有关的元素,如粒子效果、Timeline等。