Unity3d:实现自己的Dotween,C#扩展方法,插值旋转,插值移动

C#扩展方法

C#扩展方法第一个参数指定该方法作用于哪个类型,并且该参数以 this 修饰符为前缀。

public static class ExtendMethods
    {
    public static tween DoRotate(this Transform transform, Vector3 target, float time)
        {
            tween myTween = new tween("DoRotate", transform, target, time);
            Coroutine coroutine = DOTweenMgr.Instance.StartCoroutine(DOTweenMgr.Instance.YieldRotate(myTween));
            myTween.SetCoroutine(coroutine);
            return myTween;
        }
 }

以此实现
m_rotate.DoRotate(dir, 3);

tween信息

public class tween
    {
        public string tweenType;
        public int loops;
        public int currentLoop;

        public Transform transform;
        public Material material;
        public Vector3 originalPosition;
        public Vector3 originalRotation;
        public Vector3 originalScale;

        public Vector3 origin;
        public Vector3 target;

        public float time;
        public bool isPause;
        public bool autoKill;
        public Coroutine coroutine;

        public delegate void Callback();
        public Callback onComplete;
        public Callback onKill;
        public Callback onPause;

        public Quaternion m_rotation;
        public Quaternion m_tarRotation;
        public tween(string type, Transform trans, Vector3 tar, float ti,int ploops = 1)

把每次dotween要操作的tranform,tween类型(移动,旋转,缩放等),目标位置(角度),总共运动时间组装成tween返回

Mono单例类中开启协程做插值

旋转插值

  1. 在协程中插值运算,float f = myTween.time; f >= 0.0f; f -= Time.deltaTime,每帧递减运动时间
  2. myTween.transform.rotation = Quaternion.Lerp(myTween.m_rotation, myTween.m_tarRotation, 1.0f-f/myTween.time); tranfrom当前四元数 = 运动开始时 与 目标的差值 ,1.0f-f/myTween.time 的值在每帧越来越靠近 1,说明越来越向目标
        public static IEnumerator YieldRotate(this MonoBehaviour mono, tween myTween)
        {
            for (; myTween.currentLoop < myTween.loops; myTween.currentLoop++)
            {
                myTween.Reset();


                for (float f = myTween.time; f >= 0.0f; f -= Time.deltaTime)
                {
                    //changeEveryFrame(myTween, distance * Time.deltaTime);
                    myTween.transform.rotation = Quaternion.Lerp(myTween.m_rotation, myTween.m_tarRotation, 1.0f-f/myTween.time);
                    yield return null;
                    while (myTween.isPause == true)
                    {
                        yield return null;
                    }
                }
            }
            myTween.OnComplete();
        }

移动插值

//总长度/时间 = 每秒要移动的长度  ,然后每帧移动长度 = 每秒要移动的长度 *Time.deltaTime
        public static IEnumerator UniversalVector3Iter(this MonoBehaviour mono, tween myTween)
        {
            for (; myTween.currentLoop < myTween.loops; myTween.currentLoop++)
            {
                myTween.Reset();
                Vector3 distance = (myTween.target - myTween.origin) / myTween.time;
                for (float f = myTween.time; f >= 0.0f; f -= Time.deltaTime)
                {
                    myTween.transform.Translate(myTween, distance * Time.deltaTime);
                    yield return null;
                    while (myTween.isPause == true)
                    {
                        yield return null;
                    }
                }
            }
            myTween.OnComplete();
        }

源码

https://github.com/luoyikun/UnityForTest
DotweenDemo场景
请添加图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

四夕立羽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值