- VisualStudio2022插件的安装及使用-编程手把手系列文章
- pprof-在现网场景怎么用
- C#实现的下拉多选框,下拉多选树,多级节点
- 【学习笔记】基础数据结构:猫树
使用自定义委托来调用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中全局变量的访问修改和添加、以及多种函数类型的调用.
先到这里了,接下来要接着完善管理器的功能,敬请期待! 。
最后此篇关于使用自定义委托来调用Lua中的多返回值和长参数类型函数的文章就讲到这里了,如果你想了解更多关于使用自定义委托来调用Lua中的多返回值和长参数类型函数的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我有一个如下所示的数据框: import pandas as pd d = {'decil': ['1. decil','1. decil','2. decil','2. decil','3. dec
我有一些数据想要添加到我的应用中...大约 650 个类别(包括名称 + ID 号),每个类别平均有 85 个项目(每个都有一个名称/ID 号)。 iPhone会支持这么大的plist吗?我想首先在
我目前正在使用 Python 从头开始实现决策树算法。我在实现树的分支时遇到了麻烦。在当前的实现中,我没有使用深度参数。 发生的情况是,要么分支结束得太快(如果我使用标志来防止无限递归),要么如果
我在 Stack 上发现了这个问题 - Measuring the distance between two coordinates in PHP 这个答案在很多方面似乎对我来说都是完美的,但我遇到了
我目前正在清理一个具有 2 个索引和 2.5 亿个事件行以及大约同样多(或更多)的死行的表。我从我的客户端计算机(笔记本电脑)向我的服务器发出命令 VACCUM FULL ANALYZE。在过去的 3
这一切都有点模糊,因为该计划是相当深入的,但坚持我,因为我会尽量解释它。我编写了一个程序,它接受一个.csv文件,并将其转换为MySQL数据库的INSERT INTO语句。例如: ID Numbe
我有一个地址示例:0x003533,它是一个字符串,但要使用它,我需要它是一个 LONG,但我不知道该怎么做:有人有解决方案吗? s 字符串:“0x003533”到长 0x003533 ?? 最佳答案
请保持友善 - 这是我的第一个问题。 =P 基本上作为一个暑期项目,我一直在研究 wikipedia page 上的数据结构列表。并尝试实现它们。上学期我参加了 C++ 类(class),发现它非常有
简单的问题。想知道长 IN 子句是否是一种代码味道?我真的不知道如何证明它。除了我认为的那样,我不知道为什么它会闻起来。 select name, code, capital, pop
我正在尝试基于 C# 中的种子生成一个数字。唯一的问题是种子太大而不能成为 int32。有什么方法可以像种子一样使用 long 吗? 是的,种子必须很长。 最佳答案 这是我移植的 Java.Util.
我一直想知道这个问题有一段时间了。在 CouchDB 中,我们有一些相当的日志 ID……例如: “000ab56cb24aef9b817ac98d55695c6a” 现在,如果我们正在搜索此项目并浏览
列的虚拟列 c和一个给定的值 x等于 1如果 c==x和 0 其他。通常,通过为列创建虚拟对象 c , 一排除一个值 x选择,因为最后一个虚拟列不添加任何信息 w.r.t.已经存在的虚拟列。 这是我如
使用 tarantool,为什么我要记录这些奇怪的消息: 2016-03-24 16:19:58.987 [5803] main/493623/http/XXX.XXX.XXX.XXX:57295 t
我显然是 GitHub 的新手,想确保在开始之前我做的事情是正确的。 我想创建一个新的存储库,它使用来自 2 个现有项目的复刻/克隆。现有项目不是我的。 假设我想使用的 repo 被称为来自开发人员“
我的应用程序名称长度为 17 个字符。当安装在设备上时,它看起来像应用程序...名称。有没有办法在多行上显示应用程序名称?请帮忙。 最佳答案 不,你不能。我认为 iPad 支持 15 个字符来完整显示
我必须编写一个程序来读取文件中的所有单词,并确定每个单词使用了多少次。我的任务是使用多线程来加快运行时间,但是单线程程序的运行速度比多线程程序快。我曾尝试研究此问题的解决方案,但很多解释只会让我更加困
假设我在给定的范围内有一个位置pos,这样: 0 = newRange*newRange : "Case not supported yet"; // Never happens in my code
我试图在 Java 中将 unix 时间四舍五入到该月的第一天,但没有成功。示例: 1314057600 (Tue, 23 Aug 2011 00:00:00 GMT) 至 1312156800
我们的项目有在 CVS 中从现有分支创建新分支的历史。几年后,这导致了每次发布时更改的文件上的这种情况: 新版本:1.145.4.11.2.20.2.6.2.20.2.1.2.11.2.3.2.4.4
我有以下数据框: DAYS7 <- c('Monday','Tuesday','Wednesday','Thursday','Friday', 'Saturday', 'Sunday') DAYS
我是一名优秀的程序员,十分优秀!