【Unity3D开发】DOTween 插件仿写
2017-07-16 22:43
211 查看
原文链接 : https://damondu.github.io/2017/06/08/unity3D-learn5/
using UnityEngine;
namespace myTween
{
public class TweenID
{
internal string tid;
internal Vector3 target;
internal float duration;
internal bool isPause = false;
internal bool isAutoKill = true;
internal Coroutine coroutine = null;
internal Transform transform = null;
public delegate void OnComplete();
internal OnComplete onComplete = null;
//constructor
public Tween(string tid, Vector3 target, float duration, Transform transform, Coroutine coroutine)
{
this.tid = tid;
this.target = target;
this.duration = duration;
this.transform = transform;
this.coroutine = coroutine;
IDOTween.getInstance().Add(this);
}
//set the coroutine of tween
public void setCoroutine(Coroutine coroutine)
{
this.coroutine = coroutine;
}
public void setAutoKill(bool isAutoKill)
{
this.isAutoKill = isAutoKill;
}
public void Play()
{
isPause = false;
}
public void Pause()
{
isPause = true;
}
public void Kill()
{
MonoBehaviour mono = transform.GetComponent<MonoBehaviour>();
mono.StopCoroutine(coroutine);
IDOTween.getInstance().Remove(this);
}
public void runOnComplete()
{
if (onComplete != null)
{
onComplete();
}
if (isAutoKill)
{
Kill();
}
}
public void setOnComplete(OnComplete fun)
{
onComplete += fun;
}
}
}
using System.Collections.Generic;
using UnityEngine;
namespace myTween {
public class IDOTween
{
private static List<Tween> dotweenList = new List<Tween>();
private static IDOTween dotween = null;
//控制单实例
public static IDOTween getInstance()
{
if (dotween == null)
{
dotween = new IDOTween();
}
return dotween;
}
public void Add(Tween d)
{
dotweenList.Add(d);
}
public void Remove(Tween d)
{
dotweenList.Remove(d);
}
public static void Play(string id)
{
foreach (Tween t in dotweenList)
{
if (t.tid == id)
{
t.Play();
}
}
}
public static void Play(Transform transform)
{
foreach (Tween d in dotweenList)
{
if (d.transform == transform)
{
d.Play();
}
}
}
public static void PlayAll()
{
foreach (Tween d in dotweenList)
{
d.Play();
}
}
public static void Pause(string id)
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
if (dotweenList[i].tid == id)
{
dotweenList[i].Pause();
}
}
}
public static void Pause(Transform transform)
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
if (dotweenList[i].transform == transform)
{
dotweenList[i].Pause();
}
}
}
public static void PauseAll()
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
dotweenList[i].Pause();
}
}
public static void Kill(string id)
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
if (dotweenList[i].tid == id)
{
dotweenList[i].Kill();
}
}
}
public static void Kill(Transform transform)
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
if (dotweenList[i].transform == transform)
{
dotweenList[i].Pause();
}
}
}
public static void KillAll()
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
dotweenList[i].Kill();
}
}
}
}
using System.Collections;
using UnityEngine;
namespace myTween {
public static class Extension
{
//MonoBehaviour的DoMove方法拓展
public static IEnumerator DoMove(this MonoBehaviour mono, Tween tween)
{
Vector3 speed = (tween.target - tween.transform.position) / tween.duration;
for (float f = tween.duration; f >= 0.0f; f -= Time.deltaTime)
{
tween.transform.Translate(speed * Time.deltaTime);
yield return null;
while (tween.isPause == true)
{
yield return null;
}
}
tween.runOnComplete();
}
//Transform的DoMove方法拓展
public static Tween DoMove(this Transform transform, Vector3 target, float duration)
{
MonoBehaviour mono = transform.GetComponents<MonoBehaviour>()[0];
Tween tween = new Tween("DoMove",target, duration, transform,null);
Coroutine coroutine = mono.StartCoroutine(mono.DoMove(tween));
tween.setCoroutine(coroutine);
return tween;
}
//MonoBehaviour的DoScale方法拓展
public static IEnumerator DoScale(this MonoBehaviour mono, Tween tween)
{
Vector3 speed = (tween.target - tween.transform.localScale) / tween.duration;
for (float f = tween.duration; f >= 0.0f; f -= Time.deltaTime)
{
tween.transform.localScale = tween.transform.localScale + speed * Time.deltaTime;
yield return null;
while (tween.isPause == true)
{
yield return null;
}
}
tween.runOnComplete();
}
//Transform的DoScale方法拓展
public static Tween DoScale(this Transform transform, Vector3 target, float time)
{
MonoBehaviour mono = transform.GetComponents<MonoBehaviour>()[0];
Tween tween = new Tween("DoScale", target, time, transform, null);
Coroutine coroutine = mono.StartCoroutine(mono.DoScale(tween));
tween.setCoroutine(coroutine);
return tween;
}
}
}
using UnityEngine;
namespace myTween
{
public class TweenID
{
internal string tid;
internal Vector3 target;
internal float duration;
internal bool isPause = false;
internal bool isAutoKill = true;
internal Coroutine coroutine = null;
internal Transform transform = null;
public delegate void OnComplete();
internal OnComplete onComplete = null;
//constructor
public Tween(string tid, Vector3 target, float duration, Transform transform, Coroutine coroutine)
{
this.tid = tid;
this.target = target;
this.duration = duration;
this.transform = transform;
this.coroutine = coroutine;
IDOTween.getInstance().Add(this);
}
//set the coroutine of tween
public void setCoroutine(Coroutine coroutine)
{
this.coroutine = coroutine;
}
public void setAutoKill(bool isAutoKill)
{
this.isAutoKill = isAutoKill;
}
public void Play()
{
isPause = false;
}
public void Pause()
{
isPause = true;
}
public void Kill()
{
MonoBehaviour mono = transform.GetComponent<MonoBehaviour>();
mono.StopCoroutine(coroutine);
IDOTween.getInstance().Remove(this);
}
public void runOnComplete()
{
if (onComplete != null)
{
onComplete();
}
if (isAutoKill)
{
Kill();
}
}
public void setOnComplete(OnComplete fun)
{
onComplete += fun;
}
}
}
using System.Collections.Generic;
using UnityEngine;
namespace myTween {
public class IDOTween
{
private static List<Tween> dotweenList = new List<Tween>();
private static IDOTween dotween = null;
//控制单实例
public static IDOTween getInstance()
{
if (dotween == null)
{
dotween = new IDOTween();
}
return dotween;
}
public void Add(Tween d)
{
dotweenList.Add(d);
}
public void Remove(Tween d)
{
dotweenList.Remove(d);
}
public static void Play(string id)
{
foreach (Tween t in dotweenList)
{
if (t.tid == id)
{
t.Play();
}
}
}
public static void Play(Transform transform)
{
foreach (Tween d in dotweenList)
{
if (d.transform == transform)
{
d.Play();
}
}
}
public static void PlayAll()
{
foreach (Tween d in dotweenList)
{
d.Play();
}
}
public static void Pause(string id)
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
if (dotweenList[i].tid == id)
{
dotweenList[i].Pause();
}
}
}
public static void Pause(Transform transform)
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
if (dotweenList[i].transform == transform)
{
dotweenList[i].Pause();
}
}
}
public static void PauseAll()
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
dotweenList[i].Pause();
}
}
public static void Kill(string id)
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
if (dotweenList[i].tid == id)
{
dotweenList[i].Kill();
}
}
}
public static void Kill(Transform transform)
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
if (dotweenList[i].transform == transform)
{
dotweenList[i].Pause();
}
}
}
public static void KillAll()
{
for (int i = dotweenList.Count - 1; i >= 0; i--)
{
dotweenList[i].Kill();
}
}
}
}
using System.Collections;
using UnityEngine;
namespace myTween {
public static class Extension
{
//MonoBehaviour的DoMove方法拓展
public static IEnumerator DoMove(this MonoBehaviour mono, Tween tween)
{
Vector3 speed = (tween.target - tween.transform.position) / tween.duration;
for (float f = tween.duration; f >= 0.0f; f -= Time.deltaTime)
{
tween.transform.Translate(speed * Time.deltaTime);
yield return null;
while (tween.isPause == true)
{
yield return null;
}
}
tween.runOnComplete();
}
//Transform的DoMove方法拓展
public static Tween DoMove(this Transform transform, Vector3 target, float duration)
{
MonoBehaviour mono = transform.GetComponents<MonoBehaviour>()[0];
Tween tween = new Tween("DoMove",target, duration, transform,null);
Coroutine coroutine = mono.StartCoroutine(mono.DoMove(tween));
tween.setCoroutine(coroutine);
return tween;
}
//MonoBehaviour的DoScale方法拓展
public static IEnumerator DoScale(this MonoBehaviour mono, Tween tween)
{
Vector3 speed = (tween.target - tween.transform.localScale) / tween.duration;
for (float f = tween.duration; f >= 0.0f; f -= Time.deltaTime)
{
tween.transform.localScale = tween.transform.localScale + speed * Time.deltaTime;
yield return null;
while (tween.isPause == true)
{
yield return null;
}
}
tween.runOnComplete();
}
//Transform的DoScale方法拓展
public static Tween DoScale(this Transform transform, Vector3 target, float time)
{
MonoBehaviour mono = transform.GetComponents<MonoBehaviour>()[0];
Tween tween = new Tween("DoScale", target, time, transform, null);
Coroutine coroutine = mono.StartCoroutine(mono.DoScale(tween));
tween.setCoroutine(coroutine);
return tween;
}
}
}
using UnityEngine; using System.Collections; public class DoTweenTest : MonoBehaviour { Tween tween; void Start() { transform.DoMove(new Vector3(5f, 5f, 5f), 5f); transform.DoScale(new Vector3(5f, 5f, 5f), 5f); } }
相关文章推荐
- Unity3D游戏开发之使用Unity3D开发2D游戏(二)(2DTookit插件亲测)
- Unity3D开发(三):场景管理插件Scene Manager
- Unity3d UnityEditor编辑器定制和开发插件相关博文等
- 【转载】Unity3d UnityEditor编辑器定制和开发插件
- Unity3d 开发(二)Unity 制作可发布插件
- 【Unity3d游戏开发】UGUI插件入门之游戏菜单
- Unity3d UnityEditor编辑器定制和开发插件
- Unity3D开发HTC VIVE项目中插件HTC.Input.Utility.Plugin插件的运用
- Unity3d的ios插件开发
- Unity3d的ios插件开发
- unity基础开发----Unity3D的uniSWF插件动态加载SWF UI资源
- Unity3d UnityEditor编辑器定制和开发插件
- Unity3D项目开发热门必备插件
- unity3d实现插件快速开发对话系统
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(九)----使用PreviewLabs插件提速与完整的项目源码
- Unity3D+免费工具开发2D游戏之一:工具和插件介绍
- Unity3D游戏开发之使用Unity3D开发2D游戏(二)(2DTookit插件亲测)
- unity3d插件之 DoTween
- Unity3D开发小贴士(十六)SVN插件
- Unity3D常用游戏开发插件测评总结