如何在Lua上使用Dotween【便捷、易读性高】

本文介绍了如何在Unity中利用DOTween库创建一个名为DoTweenEx的扩展,实现了更简洁易读的动画控制接口。DoTweenEx支持序列化与并行动画,自动关闭,以及各种缓动类型、循环模式、更新方式等功能。同时,提供了Lua绑定,方便在Lua脚本中调用。示例展示了如何进行位移、缩放、旋转、颜色变化等动画操作,并且支持自定义回调函数。
摘要由CSDN通过智能技术生成

功能介绍:

1.整合Append与Insert接口,支持串行与并行动画

2.支持随物体/界面等销毁而自动关闭动画

3.易读性高,action的写法很直观看出具体操作内容

PS:该文章部分源码依据于此在lua里实现类似unity生命周期的监听事件_Le_Sam的博客-CSDN博客

源码:

c#


namespace Game.Scripts.Common
{
    [XLua.LuaCallCSharp]
    public class DoTweenEx
    {
        private Tweener tweener;
        private Sequence sequence;
        private Tween current;

        public static DoTweenEx Sequence()
        {
            return new DoTweenEx(DOTween.Sequence());
        }

        private DoTweenEx(Sequence sequence)
        {
            this.sequence = sequence;
            current = sequence;
            this.Ease(1); //默认EaseType为DoTweenAction.Linear
        }

        private DoTweenEx(Transform target, Tweener tweener)
        {
            this.tweener = tweener;
            current = tweener;
            this.Ease(1); //默认EaseType为DoTweenAction.Linear
        }

        public DoTweenEx Append(DoTweenEx tweener)
        {
            sequence.Append(tweener.current);
            return this;
        }

        public DoTweenEx Insert(DoTweenEx tweener)
        {
            sequence.Insert(0, tweener.current);
            return this;
        }

        public DoTweenEx Play()
        {
            current.Play();
            return this;
        }

        public DoTweenEx Pause()
        {
            current.Pause();
            return this;
        }

        public DoTweenEx Kill(bool complete)
        {
            current.Kill(complete);
            return this;
        }

        public DoTweenEx Ease(uint ease)
        {
            current.SetEase((Ease)ease);
            return this;
        }

        public DoTweenEx Loop(int loops, uint type)
        {
            loops = loops == -1 ? 99999 : loops; // Temp:循环为-1时,并没有生效,强制改为固定值
            if (sequence != null)
                sequence.SetLoops(loops);
            if (tweener != null)
                tweener.SetLoops(loops, (LoopType)type);
            return this;
        }

        public DoTweenEx Delay(float delay)
        {
            current.SetDelay(delay);
            return this;
        }

        public DoTweenEx Restart()
        {
            current.Restart();
            return this;
        }

        public DoTweenEx OnComplete(LuaFunction func)
        {
            current.OnComplete(delegate() { func.Call(this); });
            return this;
        }

        public DoTweenEx SetUpdate(uint type, bool isAlone)
        {
            current.SetUpdate((UpdateType)type, isAlone);
            return this;
        }

        public DoTweenEx SetPlaySpeed(float speed)
        {
            current.timeScale = speed;
            return this;
        }

        public static DoTweenEx NoneTo(Transform target, float duration)
        {
            return new DoTweenEx(target, DOTween.To(() => 0, v => v = 0, 0, duration + 0.001f));
        }

        public static DoTweenEx LocalMoveTo(Transform target, Vector3 pos, float duration)
        {
            return new DoTweenEx(target, target.DOLocalMove(pos, duration + 0.001f));
        }

        public static DoTweenEx LocalMoveBy(Transform target, Vector3 pos, float duration)
        {
            return new DoTweenEx(target, target.DOBlendableLocalMoveBy(pos, duration + 0.001f));
        }

        public static DoTweenEx MoveTo(Transform target, Vector3 pos, float duration)
        {
            return new DoTweenEx(target, t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值