基于LeanTween的封装

灵感来自于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<float> callback;
	private float from, to, time;
	
	public WQPValue(GameObject obj, Action<float> 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();
			}
		}
	}	
}


LeanTween的下载可以网上搜索,这里我给一个我上面用的。

链接: 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 ();
	}



是不是很方便。。。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nmg10

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值