您的位置:首页 > 移动开发 > Unity3D

【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;
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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: