Unity TimeLine 自定义脚本使用
选择场景一个物体 选择创建一个 TimeLine (playable director)
1.自动脚本的基类脚本:
a.PlayableAsset基类脚本的创建:
using UnityEngine;
using UnityEngine.Playables;
[System.Serializable]
public class BaseAsset : PlayableAsset
{
public ExposedReference<GameObject> target_;
public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
{
var baseBehaviour = GetBehaviour(graph, go);
return ScriptPlayable<BaseBehaviour>.Create(graph, baseBehaviour);
}
public virtual BaseBehaviour GetBehaviour(PlayableGraph graph, GameObject go)
{
PositionBehaviour baseBehaviour = new PositionBehaviour();
return baseBehaviour;
}
}
b.PlayableBehaviour 基类脚本的创建:
using UnityEngine;
using UnityEngine.Playables;
public class BaseBehaviour : PlayableBehaviour
{
public GameObject target_;
private float durtion_;
private float time_;
private float progress_;
//public override void OnPlayableCreate(Playable playable)
//{
// //director = playable.GetGraph().GetResolver() as PlayableDirector;
//}
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
durtion_ = (float)(playable.GetDuration());
time_ = (float)(playable.GetTime());
progress_ = (time_ / durtion_);
//Debug.LogError("durtion_" + durtion_);
//Debug.LogError("time_" + time_);
//Debug.LogError("progress_" + progress_);
OnProgress(progress_);
}
protected virtual void OnProgress(float pProgress){ }
public override void OnBehaviourPlay(Playable playable, FrameData info)
{
//Debug.LogError("OnBehaviourPlay" + info.deltaTime);
//target.transform.localPosition = start;
OnStart();
}
protected virtual void OnStart(){
}
//public override void OnBehaviourPause(Playable playable, FrameData info)
//{
// //Debug.LogError("OnBehaviourPause");
// target.transform.localPosition = end;
//}
}
2.扩展脚本(Position)
using UnityEngine;
public class PositionBehaviour : BaseBehaviour
{
public Vector3 start;
public Vector3 end;
protected override void OnProgress(float pProgress)
{
target_.transform.localPosition = start + (end - start) * pProgress;
}
}
using UnityEngine;
using UnityEngine.Playables;
[System.Serializable]
public class PositionAsset : BaseAsset
{
public Vector3 start;
public Vector3 end;
public override BaseBehaviour GetBehaviour(PlayableGraph graph, GameObject go)
{
PositionBehaviour baseBehaviour = new PositionBehaviour();
baseBehaviour.target_ = target_.Resolve(graph.GetResolver());
baseBehaviour.start = start;
baseBehaviour.end = end;
return baseBehaviour;
}
}
3.扩展脚本(Scale)
using UnityEngine;
public class ScaleBehaviour : BaseBehaviour
{
public Vector3 start;
public Vector3 end;
protected override void OnProgress(float pProgress)
{
target_.transform.localScale = start + (end - start) * pProgress;
}
}
using UnityEngine;
using UnityEngine.Playables;
[System.Serializable]
public class ScaleAsset : BaseAsset
{
public Vector3 start;
public Vector3 end;
public override BaseBehaviour GetBehaviour(PlayableGraph graph, GameObject go)
{
ScaleBehaviour baseBehaviour = new ScaleBehaviour();
baseBehaviour.target_ = target_.Resolve(graph.GetResolver());
baseBehaviour.start = start;
baseBehaviour.end = end;
return baseBehaviour;
}
}