使用自定义委托来调用Lua中的多返回值和长参数类型函数
  1wMBnbixARwC 2024年05月17日 44 0

使用自定义lua解析管理器调用函数

使用自定义委托来调用lua脚本中的多返回值函数和长参数类型的函数。

先看代码,依旧是上篇文章中所贴的脚本。新增调用两个函数testFunc

using System;
using BaseFramework;
using LuaInterface;
using UnityEngine;
using UnityEngine.Events;
using Object = System.Object;

namespace CallLua
{
    public class CallLuaEntrance:MonoBehaviour
    {
        //+ 委托
        public delegate int CustomCallFunc(int a, out int b, out int c, out string d, out bool e);
        public delegate void CustomCallParams(int a, params Object[] objects);
        private void Start()
        {
            CallLuaManager.Instance().Init();
            CallLuaManager.Instance().Require("Main");
            //获取全局变量
            Debug.Log(CallLuaManager.Instance().LuaState["string1"]);
            //无法获取lua脚本中的局部变量
            CallLuaManager.Instance().LuaState["string1"] = "我被修改了!";
            Debug.Log(CallLuaManager.Instance().LuaState["string1"]);
            //可以理解LuaState中存储的所有全局变量列表
            //如果有则可以查看并修改
            //如果没有则新建
            CallLuaManager.Instance().LuaState["newGloString"] = "我是新来的,是Lua全局变量";
            
            //获取执行无参无返回值的lua函数
            LuaFunction luaFunction = CallLuaManager.Instance().LuaState.GetFunction("testFunc");
            luaFunction.Call();
            luaFunction.Dispose(); 
            
            //直接获取
            luaFunction = CallLuaManager.Instance().LuaState["testFunc"] as LuaFunction;
            luaFunction.Call();
            luaFunction.Dispose();
            
            //存入委托中再使用
            luaFunction = CallLuaManager.Instance().LuaState.GetFunction("testFunc");
            UnityAction action = luaFunction.ToDelegate<UnityAction>();
            action();
            
            //-------------------------------------------------------------------------------------------------
            //有参有返回值函数获取调用 方式1
            luaFunction = CallLuaManager.Instance().LuaState.GetFunction("testFunc1");
            luaFunction.BeginPCall();
            luaFunction.Push(66);
            luaFunction.PCall();
            int res = (int)luaFunction.CheckNumber();
            Debug.Log("参数为"+66+" ,返回值为"+res);
            luaFunction.EndPCall();
            
            //通过函数的Invoke方法来调用  方式2
            //<参数类型,返回值类型>
            res = luaFunction.Invoke<int, int>(88);
            Debug.Log("参数为"+88+" ,返回值为"+res);
            
            //通过委托调用              方式3
            Func<int, int> func = luaFunction.ToDelegate<Func<int, int>>();
            res = func(99);
            Debug.Log("参数为"+99+" ,返回值为"+res);
            
            //通过解析器直接调用          方式4  和2本质上是一样的掉用方式
            res = CallLuaManager.Instance().LuaState.Invoke<int, int>("testFunc1", 166, true);
            Debug.Log("参数为"+166+" ,返回值为"+res);
            
            //+ 新增内容
            //----------------------------多返回值函数----------------------------------------------------
            //001直接获取 执行结果 传统方式
            luaFunction = CallLuaManager.Instance().LuaState.GetFunction("testFunc2");
            luaFunction.BeginPCall();
            luaFunction.Push(566);
            luaFunction.PCall();
            int res1 = (int)luaFunction.CheckNumber();
            int res2 = (int)luaFunction.CheckNumber();
            int res3 = (int)luaFunction.CheckNumber();
            string res4 = luaFunction.CheckString();
            bool res5 = luaFunction.CheckBoolean();
            Debug.Log("多返回值函数数值结果--->"+res1+","+res2+","+res3+","+res4+","+res5);
            
            //002使用委托方式调用函数
            CustomCallFunc customCallFunc = luaFunction.ToDelegate<CustomCallFunc>();
            int b2, b3;
            string s2;
            bool bl;
            //注意 res接收第一个返回值 其它都按照out 变量赋值出
            int res0 = customCallFunc(788, out b2, out b3, out s2, out bl);
            Debug.Log("多返回值函数数值结果--->"+res0+","+b2+","+b3+","+","+s2+","+bl);
            
            //--------------------------------------------长参数函数调用--------------------------------
            luaFunction = CallLuaManager.Instance().LuaState.GetFunction("testFunc3");
            CustomCallParams customCallParams = luaFunction.ToDelegate<CustomCallParams>();
            customCallParams(1, 2, "tony", true, 666.66);
            
            //也可以直接调用 call用来调用void 类型函数
            luaFunction.Call<int,bool,float,string>(56,false,88.88f,"Chang");
            luaFunction.Call(98,365,false,88.88f,"Chang");//不给泛型也可以!

            CallLuaManager.Instance().Dispose();
        }
    }
}

注意!在tolua中使用自定义委托时候,需要在Seting脚本中添加自定义委托,之后再重新Generate一下。

image-20240509205251581

image-20240509205422368

要调用的Main.lua

--主入口函数。从这里开始lua逻辑
function Main()					
	print("logic start")	 		
end

Main()
--场景切换通知
function OnLevelWasLoaded(level)
	collectgarbage("collect")
	Time.timeSinceLevelLoad = 0
end

--全局变量
string1 = "我是全局变量"

function testFunc()
	print("无参无返回值函数调用成功!")
end
--有参数有返回值的函数
function testFunc1(a)  
	return a + 100
end
--多返回值函数
function testFunc2(e)  
	print("多返回值函数执行")
	return e,e+100,e+200,"yes!",true
end
--变长参数函数
function testFunc3(a,...)  
	print("变长参数函数---")
	print(a)
	args = {...}
	for k,v in pairs(args) do
		print(k,v)
	end
end

function OnApplicationQuit()
	
end

好了,现在自定义的lua解析管理器已经完善对lua中全局变量的访问修改和添加、以及多种函数类型的调用。

先到这里了,接下来要接着完善管理器的功能,敬请期待!

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年05月17日 0

暂无评论

推荐阅读
  oXKBKZoQY2lx   2024年05月17日   54   0   0 游戏开发
  1wMBnbixARwC   2024年05月17日   50   0   0 游戏开发
  1wMBnbixARwC   2024年05月08日   77   0   0 游戏开发
1wMBnbixARwC