UITweener

参考官网:http://www.tasharen.com/forum/index.php?topic=6760.0

参考文章:http://dsqiu.iteye.com/blog/1974528

基于 NGUI3.0.7


NGUI自带了一套动画脚本,个人感觉比iTween好用(NGUI的Tween图形化并且使用局部坐标,而iTween的哈希表和绝对坐标比较反人类)。乍看可以用的动画脚本很多(下图),但他们的作用只是设置特定参数 (from -> to),真正执行动画变换的是它们的老爸--UITweener

UITweener_第1张图片


以TweenPosition脚本为例,该脚本提供了两个参数设置:from to(右图),也就是动画的起始位置。

先看下它的脚本:

using UnityEngine;

public class TweenPosition : UITweener
{
    //起点坐标设置
    public Vector3 from;
    //终点坐标设置
    public Vector3 to;

    
    //该物体的Transform  
	public Transform cachedTransform 
    {
        get { if (mTrans == null) mTrans = transform; return mTrans; } 
    }
    //该物体的位置
	public Vector3 position 
    {
        get { return cachedTransform.localPosition; }
        set { cachedTransform.localPosition = value; } 
    } 


	Transform mTrans;


    //执行动画: 根据此时曲线的value改变该物体的localPosition (OnUpdate是重载函数,由UITweener-Update-Sample调用, factor是曲线此时的value)
	protected override void OnUpdate (float factor, bool isFinished) 
    { 
        cachedTransform.localPosition = from * (1f - factor) + to * factor; 
    }


    //类方法,让物体go在 duration秒 从 当前位置-> pos
	static public TweenPosition Begin (GameObject go, float duration, Vector3 pos)
	{
		TweenPosition comp = UITweener.Begin(go, duration);
		comp.from = comp.position;
		comp.to = pos;

        //如果duration<0, 直接从当前位置跳到pos而不播放动画
		if (duration <= 0f)
		{
			comp.Sample(1f, true);
			comp.enabled = false;
		}
		return comp;
	}
}


再简略看下UITweener的脚本:

    void Update ()
	{		
        //可理解为累计播放时间(百分比)          
        mFactor += amountPerDelta * delta;
		//.............(根据Play Style 决定 mFactor的值)
 
		//当Play Style为Once 且 播放完毕,disable该脚本             
        if ((style == Style.Once) && (mFactor > 1f || mFactor < 0f))			
            enabled = false;		
        else Sample(mFactor, false);	
    }	
    public void Sample (float factor, bool isFinished)	
    {
        float val = Mathf.Clamp01(factor);	
        //.............(根据 factor 和 曲线形状 ,计算出曲线当前时间对应的val)	
        //调用子类的OnUpdate,开始动画播放
        OnUpdate((animationCurve != null) ? animationCurve.Evaluate(val) : val, isFinished);
    }


知道了动画播放的原理之后,看下TweenPosition继承UITweener脚本中控制动画的设置:UITweener_第2张图片

Animation Curve  动画曲线,控制动画的播放。需要注意的是UITweener已经帮你转换好数值,其中X轴(0 ~ 1)表示(0 ~ Duration),Y轴(0 ~ 1)表示(from - to)。

Play Stype  动画的播放方式

Once  只播放一次。播放完成后该脚本自动disable

Loop  循环,也就是从头开始播放。

PingPong  来回播放,像乒乓球一样

Star Delay  n秒后才播放动画

Tween Group  把当前脚本归类到特定组,然后用UIPlayTween控制该组播放

Ignore TimeScale  不会因为TimeScale而改变动画播放速度。(TimeScale在Edit - Project Setting - Time 或 Time.timeScale设置,通常用于加减速或暂停游戏)

除了手动设置播放动画外,还能通过UITweener提供的方法控制:

	public void PlayForward () { Play(true); }
	public void PlayReverse () { Play(false); }
	public void Play (bool forward)
	{
		mAmountPerDelta = Mathf.Abs(amountPerDelta);
		if (!forward) mAmountPerDelta = -mAmountPerDelta;
		enabled = true;
		Update();
	}  
                 
	public void ResetToBeginning ()
	{
		mStarted = false;
		mFactor = (mAmountPerDelta < 0f) ? 1f : 0f;
		Sample(mFactor, false);
	}


Play方法用于设置动画播放方向并激活TweenPositon等子类,然后在Update里进行动画变换了

ResetToBeginning是直接跳回到原点:Forward跳到from, Reverse跳到to。



PS:假如你需要多次进行同方向的动画(以上次的位置为基础),比如多次PlayForward, 需要先用ResetToBeginning方法重置一下,否则物体就会直接从from跳到to而不播放动画。


















你可能感兴趣的:(NGUI,学习笔记,ngui)