Unity TimeLine 自定义脚本使用

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;
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值