栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

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

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

功能介绍:

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, target.DOMove(pos, duration + 0.001f));
        }

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

        public static DoTweenEx RotateTo(Transform target, Vector3 angle, float duration)
        {
            return new DoTweenEx(target, target.DORotate(angle, duration + 0.001f, RotateMode.FastBeyond360));
        }

        public static DoTweenEx RotateBy(Transform target, Vector3 angle, float duration)
        {
            return new DoTweenEx(target, target.DOBlendableRotateBy(angle, duration + 0.001f, RotateMode.FastBeyond360));
        }

        public static DoTweenEx LocalRotateTo(Transform target, Quaternion angle, float duration)
        {
            return new DoTweenEx(target, target.DOLocalRotateQuaternion(angle, duration + 0.001f));
        }

        public static DoTweenEx LocalRotateBy(Transform target, Vector3 angle, float duration)
        {
            return new DoTweenEx(target, target.DOBlendableLocalRotateBy(angle, duration + 0.001f, RotateMode.FastBeyond360));
        }

        public static DoTweenEx ScaleTo(Transform target, Vector3 scale, float duration)
        {
            return new DoTweenEx(target, target.DOScale(scale, duration + 0.001f));
        }

        public static DoTweenEx ScaleBy(Transform target, Vector3 scale, float duration)
        {
            return new DoTweenEx(target, target.DOBlendableScaleBy(scale, duration + 0.001f));
        }

        public static DoTweenEx To(Transform target, int startValue, int endValue, float duration, LuaFunction setter)
        {
            int currValue = startValue;
            return new DoTweenEx(target, DOTween.To(() => startValue, delegate(int v)
            {
                if (currValue != v)
                {
                    currValue = v;
                    setter.Call(v);
                }
            }, endValue, duration + 0.001f));
        }

        public static DoTweenEx FadeTo(Transform target, float alpha, float duration)
        {
            var graphic = target.GetComponent();
            if (graphic != null)
            {
                alpha = alpha > 1 ? 1 : alpha;
                return new DoTweenEx(target, graphic.DOFade(alpha, duration + 0.001f));
            }
            return NoneTo(target, duration);
        }

        public static DoTweenEx FadeToAll(Transform target, float alpha, float duration, bool ignoreTS)
        {
            alpha = alpha > 1 ? 1 : alpha;
            var tween = FadeTo(target, alpha, duration);
            tween.current.OnStart(delegate () {
                Graphic[] graphics = target.GetComponentsInChildren();
                foreach (var g in graphics)
                {
                    g.CrossFadeAlpha(alpha, duration, ignoreTS);
                }
            });
            return tween;
        }

        public static DoTweenEx ColorTo(Transform target, Color color, float duration)
        {
            var graphic = target.GetComponent();
            if (graphic != null)
            {
                return new DoTweenEx(target, graphic.DOColor(color, duration + 0.001f));
            }
            return NoneTo(target, duration);
        }

        public static DoTweenEx ColorToAll(Transform target, Color color, float duration, bool ignoreTS)
        {
            var tween = ColorTo(target, color, duration);
            tween.current.OnStart(delegate()
            {
                Graphic[] graphics = target.GetComponentsInChildren();
                foreach (var g in graphics)
                {
                    g.CrossFadeColor(color, duration, ignoreTS, true);
                }
            });
            return tween;
        }

        public static DoTweenEx ShakeRotation(Transform target, float duration, Vector3 strength, int vibrato, float randomness)
        {
            return new DoTweenEx(target, target.DOShakeRotation(duration, strength, vibrato, randomness));
        }
    }
}

Lua

local DoTweenAction = {}

local DoTweenEx = CS.Game.Scripts.Common.DoTweenEx

DoTweenAction.Linear = 1--默认EaseType
DoTweenAction.InSine = 2
DoTweenAction.OutSine = 3
DoTweenAction.InOutSine = 4
DoTweenAction.InQuad = 5
DoTweenAction.OutQuad = 6
DoTweenAction.InOutQuad = 7
DoTweenAction.InCubic = 8
DoTweenAction.OutCubic = 9
DoTweenAction.InOutCubic = 10
DoTweenAction.InQuart = 11
DoTweenAction.OutQuart = 12
DoTweenAction.InOutQuart = 13
DoTweenAction.InQuint = 14
DoTweenAction.OutQuint = 15
DoTweenAction.InOutQuint = 16
DoTweenAction.InExpo = 17
DoTweenAction.OutExpo = 18
DoTweenAction.InOutExpo = 19
DoTweenAction.InCirc = 20
DoTweenAction.OutCirc = 21
DoTweenAction.InOutCirc = 22
DoTweenAction.InElastic = 23
DoTweenAction.OutElastic = 24
DoTweenAction.InOutElastic = 25
DoTweenAction.InBack = 26
DoTweenAction.OutBack = 27
DoTweenAction.InOutBack = 28
DoTweenAction.InBounce = 29
DoTweenAction.OutBounce = 30
DoTweenAction.InOutBounce = 31
DoTweenAction.Flash = 32
DoTweenAction.InFlash = 33
DoTweenAction.OutFlash = 34
DoTweenAction.InOutFlash = 35

DoTweenAction.LTRestart = 0 -- 默认LoopType
DoTweenAction.LTYoyo = 1
DoTweenAction.LTIncremental = 2

DoTweenAction.UpdateNormal = 0 -- 默认UpdateType
DoTweenAction.UpdateLate = 1
DoTweenAction.UpdateFixed = 2
DoTweenAction.UpdateManual = 3

local Param = {
	ease = function(tween, aEaseType)
		return tween:Ease(aEaseType)
	end,
	loop = function(tween, aTimes, aLoopType)
		return tween:Loop(aTimes, aLoopType or DoTweenAction.LTRestart)
	end,
	delay = function(tween, aSecend)
		return tween:Delay(aSecend)
	end,
	-- DoTween终止回调
	OnEnd = function(tween, ...)
		return DoTweenAction.SetOnComplete(tween, ...)
	end,
}

local Function = {
	-- 并行
	spawn = function(target, ...)
		return DoTweenAction.SequenceAction(target, {...}, "Insert")
	end,
	delay = function(target, delay, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.NoneTo(target, delay), ...)
	end,
	call = function(target, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.NoneTo(target), ...)
	end,
	to = function(target, startValue, endValue, duration, setter, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.To(target, startValue, endValue, duration, setter), ...)
	end,
	localMoveBy = function(target, vec, duration, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.LocalMoveBy(target, vec, duration), ...)
	end,
	localMoveTo = function(target, vec, duration, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.LocalMoveTo(target, vec, duration), ...)
	end,
	moveBy = function(target, vec, duration, camera, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.MoveBy(target, vec, duration, camera), ...)
	end,
	moveTo = function(target, vec, duration, camera, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.MoveTo(target, vec, duration, camera), ...)
	end,
	rotateBy = function(target, vec, duration, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.RotateBy(target, vec, duration), ...)
	end,
	rotateTo = function(target, vec, duration, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.RotateTo(target, vec, duration), ...)
	end,
	localRotateBy = function(target, vec, duration, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.LocalRotateBy(target, vec, duration), ...)
	end,
	localRotateTo = function(target, vec, duration, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.LocalRotateTo(target, vec, duration), ...)
	end,
	scaleBy = function(target, vec, duration, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.ScaleBy(target, vec, duration), ...)
	end,
	scaleTo = function(target, vec, duration, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.ScaleTo(target, vec, duration), ...)
	end,	
	fadeTo = function(target, opacity, duration, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.FadeTo(target, opacity, duration), ...)
	end,
	fadeToAll = function(target, opacity, duration, ignoreTS, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.FadeToAll(target, opacity, duration, ignoreTS or true), ...)
	end,
	colorTo = function(target, color, duration, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.ColorTo(target, color, duration), ...)
	end,
	colorToAll = function(target, color, duration, ignoreTS, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.ColorToAll(target, color, duration, ignoreTS or true), ...)
	end,
	shakeRotation = function(target, duration, vec, vibrato, randomness, ...)
		return DoTweenAction.SetOnComplete(DoTweenEx.ShakeRotation(target, duration, vec, vibrato or 10, randomness or 90), ...)
	end,
}

function DoTweenAction.SetCallBack(tween, callback, aFunc, ...)
	if aFunc and type(aFunc) == "function" then
		local args = {...}
		return tween[callback](tween, #args == 0 and aFunc or function()
			aFunc(table.unpack(args))
		end)
	end
	return tween
end

function DoTweenAction.SetOnComplete(tween, aFunc, ...)
	return DoTweenAction.SetCallBack(tween, 'OnComplete', aFunc, ...)
end

function DoTweenAction.SetParam(tween, action, funcName)

	if tween then
		for name, param in pairs(action) do
			if type(name) == "string" then
				assert(Param[name] ~= nil, "DoTweenAction SetParam not find : ".. name);
				if type(param) == "table" then
					Param[name](tween, table.unpack(param))
				else
					Param[name](tween, param)
				end
			end
		end
	else
		logerror("DoTweenAction SetParam not find : ".. funcName)
	end
	return tween
end

function DoTweenAction.SequenceAction(target, action, opt)
	local sequence = DoTweenEx.Sequence()
	for _, act in ipairs(action) do
		if type(act) == "table" then
			if opt == "Append" then
				sequence:Append(DoTweenAction.RunAction(target, act))
			elseif opt == "Insert" then
				sequence:Insert(DoTweenAction.RunAction(target, act))
			end
		end
	end
	return DoTweenAction.SetParam(sequence, action)
end

function DoTweenAction.RunAction(target, action)
	return DoTweenAction.CreateAction(target.transform or target, action, table.unpack(action))
end

function DoTweenAction.CreateAction(target, action, funcName, ...)
	-- 代表一个动作
	if type(funcName) == "string" then
		assert(Function[funcName] ~= nil, "DoTweenAction CreateAction not find : ".. funcName);
		local tween = Function[funcName](target, ...)
		tween = DoTweenAction.SetParam(tween, action, funcName)
		return tween
	-- Action是一个动作数组,需要拆分其动作并组合成数组动作
	elseif type(funcName) == "table" then
		return DoTweenAction.SequenceAction(target, action, "Append")
	-- 表示这个动作只是执行这个function
	elseif type(funcName) == "function" then
		return Function.call(target, funcName, ...)
	end
end


return DoTweenAction
local DoTweenAction = require("DoTweenAction")

local ActionTool = {}

local actionMap = {};
local actionKey = 0;

-- 运行动作
function ActionTool.RunAction(target, action, ignoreTS)

    actionKey = actionKey + 1
    local curKey = actionKey

    local function onDestory()
        ActionTool.StopAction(curKey)
	end

    local function onComplete()
		actionMap[curKey] = nil 
	end

    Helpers.AddDestroyListener(target.transform or target, onDestory)
    actionMap[curKey] = DoTweenAction.RunAction(target, {
        action,
        OnEnd = onComplete,
    })
    ActionTool.ChangeUpdateAction(curKey, DoTweenAction.UpdateNormal, ignoreTS or true) -- 默认忽略Time.timeScale
    return curKey	
end

-- 停止动作
function ActionTool.StopAction(actionKey, complete)
    local action = actionMap[actionKey];
    if action then
        action:Kill(complete or false);
        actionMap[actionKey] = nil;
    end
end

--暂停动作
function ActionTool.PauseAction(actionKey)
    if not actionKey then return end
    if not actionMap[actionKey] then return end
    actionMap[actionKey]:Pause()
end

--恢复动作
function ActionTool.ResumeAction(actionKey)
    if not actionKey then return end
    if not actionMap[actionKey] then return end
    actionMap[actionKey]:Play()
end

-- 改变速度
function ActionTool.ChangeSpeedAction(actionKey, speed)
    if not actionKey then return end
    if not actionMap[actionKey] then return end
    actionMap[actionKey]:SetPlaySpeed(speed)
end

-- 改变Update
function ActionTool.ChangeUpdateAction(actionKey, type, isAlone)
    if not actionKey then return end
    if not actionMap[actionKey] then return end
    actionMap[actionKey]:SetUpdate(type, isAlone)
end

-- 停止所有动作
function ActionTool.StopAllAction(complete)
	for _,v in pairs(actionMap) do
		v:Kill(complete or false);
	end
	actionKey = 0;
	actionMap = {};
end


return ActionTool

示例:

-- View
local UITest = ClassView("UITest")
local ActionTool = require("ActionTool")
local DoTweenAction = require("DoTweenAction")

function UITest:OnCreate() 
    local testTxt = self:FindChild("testUI"):GetComponent("Text")
    -- Test1
    do
        local action = {
            {"spawn", 
                {"localMoveTo", Vector3(-100, 0, 0), 0.25, ease = DoTweenAction.OutQuad},
                {"scaleTo", Vector3(1.1, 1.1, 1.1), 0.15, delay = 0.1, ease = DoTweenAction.OutQuad},
            },
            {"call", function()
                logdebug(string.format("Test1 call ==> testTxt:%s", testTxt))
            end},
            {"delay", 5},
            OnEnd = function()
                logdebug(string.format("Test1 OnEnd ==> testTxt:%s", testTxt))
            end,
        };
        ActionTool.RunAction(testTxt.transform, action) 
    end

    -- Test2
    do
        local maxP = 100
        local centreP = maxP / 2
        local tempColor = Color(1,1,1,1)
        local action = {
            {"to", 1, maxP, 1, function(value)
                local curValue = value >= centreP and value or -value + maxP
                tempColor.a = curValue / maxP
                testTxt.color = tempColor
                end, loop = -1
            },
        };
        ActionTool.RunAction(testTxt.transform, action)
    end

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901412.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号