使用自定义委托来调用Lua中的多返回值和长参数类型函数
1.Unity学习笔记--基础2.Unity学习笔记--入门3.Unity学习笔记--数据持久化之PlayerPrefs的使用4.Unity学习笔记--数据持久化XML文件(1)5.Unity学习笔记--数据持久化XML文件(2)6.Unity学习笔记--数据持久化Json7.NGUI学习笔记(1)8.NGUI学习笔记29.NGUI学习笔记3.510.NGUI学习笔记4.011.Unity 热更--AssetBundle学习笔记 0.712.Unity 热更--AssetBundle学习笔记 0.813.Unity 热更--AssetBundle学习笔记 1.0【AB包资源加载工具类的实现】14.[2]自定义Lua解析方式15.Unity热更学习toLua使用--[1]toLua的导入和默认加载执行lua脚本16.自定义Lua解析器管理器-------演化脚本V0.5
17.使用自定义委托来调用Lua中的多返回值和长参数类型函数
18.使用自定义lua解析管理器调用lua脚本中的table19.Lua热更学习--使用toLua中的协程20.toLua中Lua调用C#中的类21.热更学习笔记10~11----lua调用C#中的List和Dictionary、拓展类中的方法22.Lua中调用ref和out修饰参数的函数/重载函数23.Unity 编辑器中获取选中的文件夹、文件路径使用自定义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一下。
要调用的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中全局变量的访问修改和添加、以及多种函数类型的调用。
先到这里了,接下来要接着完善管理器的功能,敬请期待!
本文作者:畅知
本文链接:https://www.cnblogs.com/TonyCode/p/18183121
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步