IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> 如何在Lua上使用Dotween【便捷、易读性高】 -> 正文阅读

[游戏开发]如何在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<Graphic>();
            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<Graphic>();
                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<Graphic>();
            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<Graphic>();
                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

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-05-05 11:52:21  更:2022-05-05 11:53:57 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 10:43:31-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码