shubozhang 2014/12/22oop
564534003@qq.compost
1 基本插值動畫
說明:ui
HOTween 只能對物體的任何非靜態共有屬性或者字段作插值spa
TweenParms.Prop(propName,endValue) 設定被插值的屬性和最後的值(能夠屢次調用指定多個被插值的屬性)代理
TweenParms.Ease(easeType) 指定漸變類型,這裏使用Linear表示線性漸變orm
HOTween.To(object,time,params) To表示從如今的值通過插值後變換到endValue,time表示整個變換進行的時間,params定義了其餘參數對象
(HOTween.From(object,time,params) 把To改用From表示從被指定的endValue通過變換後,成爲當前的值)blog
代碼:遊戲
void Start ()
{
TweenParms params = new TweenParms();
parms.Prop("postion", new Vector3(2.0f,0.0f,0.0f)); //座標
parms.Ease(EaseType.Linear); //加速度類型
HOTween.To(gameObject.transform, 2, params);
}
效果:
綁定到該遊戲的對象從原來的位置通過兩秒移動到了位置(2.0f,0.0f,0.0f) 最後中止在(2.0f,0.0f,0.0f)
移動的速度爲勻速,改變移動的速度能夠選用其餘的漸變類型
其餘可選的漸變類型包括:
EaseInSine = 1,EaseOutSine = 2,EaseInOutSine = 3,
EaseInQuad = 4,EaseOutQuad = 5,EaseInOutQuad = 6,
EaseInCubic = 7,EaseOutCubic = 8,EaseInOutCubic = 9,
EaseInQuart = 10,EaseOutQuart = 11,EaseInOutQuart = 12,
EaseInQuint = 13,EaseOutQuint = 14,EaseInOutQuint = 15,
EaseInExpo = 16,EaseOutExpo = 17,EaseInOutExpo = 18,
EaseInCirc = 19,EaseOutCirc = 20,EaseInOutCirc = 21,
EaseInElastic = 22,EaseOutElastic = 23,EaseInOutElastic = 24,
EaseInBack = 25,EaseOutBack = 26,EaseInOutBack = 27,
EaseInBounce = 28,EaseOutBounce = 29,EaseInOutBounce = 30,
AnimationCurve = 31
2 使用自定義的曲線關鍵點進行插值
代碼:
void Start ()
{
Vector3[] points = new Vector3[2];
points[0] = new Vector3(2.0f, 0.0f, 0.0f);
points[1] = new Vector3(2.0f, 1.0f, 0.0f);
TweenParms parms = new TweenParms();
parms.Prop("position", new PlugVector3Path(points,PathType.Linear)); //座標
parms.Ease(EaseType.Linear); //加速度類型
HOTween.To(gameObject.transform, 2, parms);
}
效果:
遊戲對象先移動到了(2.0f, 0.0f, 0.0f)而後當即移動到了(2.0f, 1.0f, 0.0f)一共花了兩秒
3 將插值的動畫進行連續播放
void Start ()
{
Sequence seq = new Sequence(new SequenceParms().Loops(1,LoopType.Yoyo));
TweenParms parms = null;
parms = new TweenParms()
.Prop("position",new Vector3(1.0f,0.0f,0.0f))
.Ease(EaseType.Linear);
seq.Append(HOTween.To(gameObject.transform,1,parms));
parms = new TweenParms()
.Prop("position", new Vector3(1.0f, 1.0f, 0.0f))
.Ease(EaseType.Linear);
seq.Append(HOTween.To(gameObject.transform, 1, parms));
seq.Play();
}
4 在動畫結束時進行方法回調
代碼:沒有參數的
void Start ()
{
TweenParms parms = null;
parms = new TweenParms()
.Prop("position", new Vector3(1.0f, 0.0f, 0.0f))
.OnComplete(OnComplete);
HOTween.To(gameObject.transform, 1, parms);
}
private void OnComplete()
{
Debug.Log("OnComplete Called!");
}
代碼:有參數的
void Start ()
{
TweenParms parms = null;
parms = new TweenParms()
.Prop("position", new Vector3(1.0f, 0.0f, 0.0f))
.OnComplete(OnComplete, 50, Vector3.one);
HOTween.To(gameObject.transform, 1, parms);
}
private void OnComplete(TweenEvent e)
{
Debug.Log(e.parms[0]);//第一個參數
Debug.Log(e.parms[1]);//第二個參數
}
5 Loops方法
TweenParms.Loops(times,loopType)
經過該方法能夠指定一次插值動畫的循環屬性
包括如下幾種loopType
public enum LoopType
{
Restart = 0,Yoyo = 1,YoyoInverse = 2,Incremental = 3,
}
Restart代碼:
void Start ()
{
TweenParms parms = null;
parms = new TweenParms()
.Prop("position", new Vector3(1.0f, 0.0f, 0.0f))
.Loops(2, LoopType.Restart);
HOTween.To(gameObject.transform, 1, parms);
}
效果:一共播放動畫兩次,第一次對象從起點運動到了(1.0f, 0.0f, 0.0f),
而後瞬間移動到原點播放第二次動畫,效果和第二次相同,最後物體中止在(1.0f, 0.0f, 0.0f)。
Yoyo 代碼:
void Start ()
{
TweenParms parms = null;
parms = new TweenParms()
.Prop("position", new Vector3(1.0f, 0.0f, 0.0f))
.Loops(2, LoopType.Yoyo);
HOTween.To(gameObject.transform, 1, parms);
}
效果:一共播放動畫兩次,第一次對象從起點運動到了(1.0f, 0.0f, 0.0f),
而後開始播放第二次動畫,從(1.0f, 0.0f, 0.0f)運動到原點,最後中止在原點。
YoyoInverse代碼:
void Start ()
{
TweenParms parms = null;
parms = new TweenParms()
.Prop("position", new Vector3(1.0f, 0.0f, 0.0f))
.Loops(2, LoopType.YoyoInverse);
HOTween.To(gameObject.transform, 1, parms);
}
效果:和Yoyo運動的方式相同先運動到(1.0f, 0.0f, 0.0f)在運動回原點,
可是,Yoyo對EaseType不產生影響,YoyoInverse還會對EaseType產生相反的影響。
6 處理對靜態變量的插值
有時一些靜態變量也須要插值操做,可是HOTween不能直接對其進行操做,因此須要作一個代理,這裏使用Time.timeScale進行舉例。
代碼:
class TimeScaleTweenProxy
{
public float Value
{
get
{
return Time.timeScale;
}
set
{
Time.timeScale = value;
}
}
}
void Start ()
{
TimeScaleTweenProxy proxy = new TimeScaleTweenProxy();
TweenParms parms = null;
parms = new TweenParms()
.Prop("Value", 0.4)
.Ease(EaseType.EaseOutCubic)
HOTween.To(proxy, duration, parms);
}
7 其餘: delay方法,屢次設置prop,from的使用
使用Delay動畫將被延時播放
void Start ()
{
TweenParms params = new TweenParms();
parms.Prop("postion", new Vector3(2.0f,0.0f,0.0f)); //座標
parms.Ease(EaseType.Linear); //加速度類型
parms.Delay(1.0f);
HOTween.To(gameObject.transform, 2, params);
}
能夠設置多個prop同時進行插值
void Start ()
{
TweenParms params = new TweenParms();
parms.Prop("postion", new Vector3(2.0f,0.0f,0.0f)); //座標
parms.Prop("rotation", new Vector3(0.0f,180.0ff,0.0f));
parms.Ease(EaseType.Linear); //加速度類型
HOTween.From(gameObject.transform, 2, params);
}
使用From播放動畫運動路徑是和To相反的
void Start ()
{
TweenParms params = new TweenParms();
parms.Prop("postion", new Vector3(2.0f,0.0f,0.0f)); //座標
parms.Ease(EaseType.Linear); //加速度類型
HOTween.From(gameObject.transform, 2, params);
}