在项目开发中,我们通常都需要用到缓动动画系统,控制我们的目标对象从当前位置沿着特定曲线路径运动到指定的目标坐标点,在Unity 5.x中最常用的就是DoTween这个动画插件。
下面是我常用的一个工具接口,其功能就是实现trans对象在time时间内,到达目标坐标点aimPos,并且在到达目标点之后回调callback回调方法,可以在回调方法中进行动画结束后相应的处理操作:
public delegate void AnimationCompeletCallBack(Transform mTrans);
/// <summary>
/// 缓动动画
/// </summary>
/// <param name="trans">移动物体</param>
/// <param name="aimPos">目标坐标</param>
/// <param name="time">耗时长度(单位s)</param>
/// <param name="callback">动画完成之后的回调</param>
public void FlyTo(Transform trans,Vector3 aimPos,float time,AnimationCompeletCallBack callback){
Tweener tweener = trans.DOMove(aimPos, time);
//设置这个Tween不受Time.scale影响
tweener.SetUpdate(true);
//设置移动类型
tweener.SetEase(Ease.Linear);
if (callback != null)
{
tweener.onComplete = delegate()
{
callback(trans);
};
}
else {
tweener.onComplete = null;
}
}
此接口的使用方法如下:
FlyTo(transform, new Vector3(0, 8f, 0), 0.6f, AnimationFinishCallback);
/// <summary>
/// 动画结束回调
/// </summary>
/// <param name="mTrans"></param>
private void AnimationFinishCallback(Transform mTrans)
{
//动画结束的操作
}
运动路径的类型从
Ease枚举中进行挑选,可以有匀速运动、加速运动、减速运动等,具体每种类型的运动情况可以在一下网页中选择然后进行浏览:
http://robertpenner.com/easing/easing_demo.html