灵感来自于cocos2d-x的动画类。
完整代码:
using UnityEngine;
using System.Collections;
using System;
//动画基类
abstract public class WQPAnimation
{
public WQPAnimation()
{
}
public void play()
{
internalPlay();
}
public static void cancel(GameObject obj)
{
LeanTween.cancel (obj);
}
abstract protected void internalPlay();
public Callback _finishCallback;
public GameObject target;
public void finishCallback()
{
if(_finishCallback != null)
{
_finishCallback();
}
}
}
//等待
public class WQPADelay : WQPAnimation
{
private float delayTime;
public WQPADelay(GameObject obj, float delay)
{
target = obj;
delayTime = delay;
}
override protected void internalPlay()
{
LeanTween.delayedCall(target, delayTime, internalFinishCallback);
}
void internalFinishCallback(object obj)
{
finishCallback();
}
}
//移动
public class WQPAMoveTo : WQPAnimation
{
public WQPAMoveTo(GameObject obj, Vector3 v, float t)
{
if(obj == null)
{
obj = new GameObject();
}
target = obj;
to = v;
time = t;
}
private Vector3 to;
private float speed = 1.0f;
private float time;
override protected void internalPlay()
{
LeanTween.moveLocal(target, to, time).setOnComplete(internalCallback);
}
void internalCallback(object obj)
{
finishCallback();
}
}
//旋转
public class WQPARotateTo : WQPAnimation
{
public WQPARotateTo(GameObject obj, Vector3 t)
{
if(obj == null)
{
obj = new GameObject();
}
target = obj;
to = t;
}
private Vector3 to;
override protected void internalPlay()
{
float time = 0.5f;
LeanTween.rotate(target, to, time).setOnComplete(internalCallback);
}
void internalCallback(object obj)
{
finishCallback();
}
}
//回调
public class WQPACallback : WQPAnimation
{
private Callback callback;
public WQPACallback(Callback call)
{
callback = call;
}
override protected void internalPlay()
{
callback ();
finishCallback ();
}
}
//value (透明度)
public class WQPValue : WQPAnimation
{
private GameObject target;
private Action callback;
private float from, to, time;
public WQPValue(GameObject obj, Action call, float from, float to, float time)
{
target = obj;
callback = call;
this.from = from;
this.to = to;
this.time = time;
}
override protected void internalPlay()
{
LeanTween.value (target, callback, from, to, time).setOnComplete(internalCallback);
}
void internalCallback(object obj)
{
finishCallback();
}
}
//顺序
public class WQPASequence : WQPAnimation
{
ArrayList animArray;
int playIndex = 0;
public WQPASequence()
{
animArray = new ArrayList();
playIndex = 0;
}
public void addAnim(WQPAnimation anim)
{
animArray.Add(anim);
}
override protected void internalPlay()
{
playIndex = 0;
playNext();
}
void playNext()
{
if(playIndex >= animArray.Count)
{
finishCallback();
}
else
{
WQPAnimation anim = (WQPAnimation)animArray[playIndex];
playIndex ++;
anim._finishCallback = internalFinishCallback;
anim.play();
}
}
void internalFinishCallback()
{
playNext();
}
}
//同时
public class WQPASpawn : WQPAnimation
{
ArrayList animArray;
int playingAnimCount = 0;
public WQPASpawn()
{
animArray = new ArrayList();
playingAnimCount = 0;
}
public void addAnim(WQPAnimation anim)
{
animArray.Add(anim);
}
override protected void internalPlay()
{
playingAnimCount = animArray.Count;
if(playingAnimCount == 0)
{
finishCallback();
return;
}
for(int i = 0; i < animArray.Count; ++i)
{
WQPAnimation anim = (WQPAnimation)animArray[i];
anim._finishCallback = internalFinishCallback;
anim.play();
}
}
void internalFinishCallback()
{
playingAnimCount --;
if(playingAnimCount < 0)
{
Debug.LogError("Spawn : playing anim count < 0");
}
if(playingAnimCount == 0)
{
finishCallback();
}
}
}
//循环
public class WQPARepeat : WQPAnimation
{
WQPAnimation m_anim;
int repeatCount;
public WQPARepeat(WQPAnimation anim, int c)
{
m_anim = anim;
repeatCount = c;
}
override protected void internalPlay()
{
m_anim._finishCallback = internalFinishCallback;
m_anim.play ();
}
void internalFinishCallback()
{
if(repeatCount < 0)
{
internalPlay();
}
else
{
repeatCount--;
if(repeatCount > 0)
{
internalPlay();
}
}
}
}
链接: http://pan.baidu.com/s/1o84KqRw 密码: jmqh
例子:
//0.5秒渐现-显示1.5秒-消失0.5秒-间隔0.5秒-0.5秒渐现-显示1.5秒-消失0.5秒-间隔9.5秒
void AnimBegin()
{
WQPASequence sequ = new WQPASequence ();
WQPValue value1 = new WQPValue (gameObject, ObjValue, 0f, 1f, 0.5f);
WQPADelay del1 = new WQPADelay (gameObject, 1.5f);
WQPValue value2 = new WQPValue (gameObject, ObjValue, 1f, 0f, 0.5f);
WQPADelay del2 = new WQPADelay (gameObject, 0.5f);
WQPValue value3 = new WQPValue (gameObject, ObjValue, 0f, 1f, 0.5f);
WQPADelay del3 = new WQPADelay (gameObject, 1.5f);
WQPValue value4 = new WQPValue (gameObject, ObjValue, 1f, 0f, 0.5f);
WQPADelay del4 = new WQPADelay (gameObject, 9.5f);
sequ.addAnim (value1);
sequ.addAnim (del1);
sequ.addAnim (value2);
sequ.addAnim (del2);
sequ.addAnim (value3);
sequ.addAnim (del3);
sequ.addAnim (value4);
sequ.addAnim (del4);
WQPARepeat rep = new WQPARepeat (sequ, -1);
rep.play ();
}