[4]自定义Lua解析器管理器-------演化脚本V0.7

使用自定义委托来调用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中全局变量的访问修改和添加、以及多种函数类型的调用。

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

[4]自定义Lua解析器管理器-------演化脚本V0.7的更多相关文章

  1. WinServer-服务器管理器-从入门到放弃

    WIN7 远程服务器管理工具 看看这篇帖子,他们说可以在WIN7上通过服务器管理工具来管理服务器上的软件 https://social.technet.microsoft.com/Forums/zh- ...

  2. Django Managers管理器

    Managers class Manager 管理器是向Django模型提供数据库查询操作的接口.Django应用程序中每个模型至少有一个管理器. Manager names 默认情况下管理器的名字为 ...

  3. with和上下文管理器

    with和上下文管理器 如果你有时间阅读源码的习惯,可能会看到一些优秀的代码会出现带有with关键字的语句. 对于系统资源如文件,数据库连接,socket而言,应用程序打开这些资源并执行完业务逻辑之后 ...

  4. [置顶] Android布局管理器 - 详细解析布局实现

    布局管理器都是以ViewGroup为基类派生出来的; 使用布局管理器可以适配不同手机屏幕的分辨率,尺寸大小; 布局管理器之间的继承关系 : 在上面的UML图中可以看出, 绝对布局 帧布局 网格布局 相 ...

  5. python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。

    本次python爬虫百步百科,里面详细分析了爬虫的步骤,对每一步代码都有详细的注释说明,可通过本案例掌握python爬虫的特点: 1.爬虫调度入口(crawler_main.py) # coding: ...

  6. 【Android 应用开发】AndroidUI设计之 布局管理器 - 详细解析布局实现

    写完博客的总结 : 以前没有弄清楚的概念清晰化 父容器与本容器属性 : android_layout...属性是本容器的属性, 定义在这个布局管理器的LayoutParams内部类中, 每个布局管理器 ...

  7. SpringMVC自动封装List对象 —— 自定义参数解析器

    前台传递的参数为集合对象时,后台Controller希望用一个List集合接收数据. 原生SpringMVC是不支持,Controller参数定义为List类型时,接收参数会报如下错误: org.sp ...

  8. win2008以上的系统,在vmware esxi5.5里怎么使用自定义规范管理器?sysprep

    经过测试,原来08以上的系统自带了sysprep.exe,所以vcenter对08以上的系统直接使用自定义规范管理器即可,跟linux一样了.注意不要跟03一样写入了sn即可. vCenter可使用s ...

  9. AndroidUI设计之 布局管理器 - 详细解析布局实现

    写完博客的总结 : 以前没有弄清楚的概念清晰化 父容器与本容器属性 : android_layout...属性是本容器的属性, 定义在这个布局管理器的LayoutParams内部类中, 每个布局管理器 ...

  10. Django中自定义模型管理器(Manager)及方法

    1.自定义管理器(Manager) 在语句Book.objects.all()中,objects是一个特殊的属性,通过它来查询数据库,它就是模型的一个Manager.每个Django模型至少有一个ma ...

随机推荐

  1. 服创杯 【A15】智能信号灯-交通流疏导控制系统【融创软通】数据流图

  2. 《写给程序员的Python教程》阅读随笔---python禅学(Zen_of_python)

    Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Comp ...

  3. Go 语言变量类型和声明详解

    在Go中,有不同的变量类型,例如: int 存储整数(整数),例如123或-123 float32 存储浮点数字,带小数,例如19.99或-19.99 string - 存储文本,例如" H ...

  4. Numpy结构化数组

    Numpy结构化数组 Numpy的结构化数组和记录数组为复合的.异构的的数据提供了非常有效的存储. 结构化数组 In [1]: import numpy as np In [2]: name = [' ...

  5. Bash下切换conda环境

    背景:很多时候实验命令都是基于Linux系统的,但是很多人的电脑是window系统的. 使用git自带的Bash可以运行linux命令,不过有时候在bash中想使用conda环境的时候比较麻烦,具体做 ...

  6. MVC 测试action的运行速度

    前言 网络很多文章有关于action的测试机制,本文主要是整理一下思路. 正文 假如有一个acion: public ActionResult Index() { return View(); } 当 ...

  7. node excel采集数据

    前言 个人写过无数的脚本,但是一直没有整理,后续整理脚本. 需求: 生成一堆激活码. 业务: 需要拿到一个token, 然后调用某个api获取激活码. 正文 思路: 1.http请求 axios 2. ...

  8. locust常用的配置参数【locust版本:V1.1.1】

    locust 官网文档地址:https://docs.locust.io/en/stable/configuration.html Locust QQ 群: 执行命令如: master: locust ...

  9. 第11課-Channel Study For Create Custom Restful Service

    这节课我们一起学习利用Mirth Connect的HTTP Listener源通道与JavaScript Writer目的通道搭建自定义Restful风格webapi服务. 1.新建名为'Custom ...

  10. 力扣619(MySQL)-只出现一次的最大数字(简单)

    题目: MyNumbers 表: 单一数字 是在 MyNumbers 表中只出现一次的数字. 请你编写一个 SQL 查询来报告最大的 单一数字 .如果不存在 单一数字 ,查询需报告 null . 查询 ...