当前位置: 首页 > news >正文

使用XLua在Unity中获取lua全局变量和函数

1、Lua脚本 

入口脚本

print("OK")
--也会执行重定向
require("Test")

测试脚本

print("TestScript")
testNum = 1
testBool = true
testFloat = 1.2
testStr = "123"function testFun()print("无参无返回")
endfunction testFun2(a)print("有参有返回")return a
end

2、C#脚本

(1)获取全局变量

public class L4 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){//自己编写的Lua管理器//初始化管理器LuaMgr.GetInstance().Init();//执行Main脚本(调用了Test脚本)LuaMgr.GetInstance().DoLuaFile("Main");//得到全局变量(只是复制到C#,改不了)int i = LuaMgr.GetInstance().Global.Get<int>("testNum");print(i);//修改全局变量LuaMgr.GetInstance().Global.Set("testNum", 2);i = LuaMgr.GetInstance().Global.Get<int>("testNum");print(i);}
}

执行结果

(2)获取全局函数

using XLua;
public class L5 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){LuaMgr.GetInstance().Init();LuaMgr.GetInstance().DoLuaFile("Main");#region 无参无返回//第一种方法 通过 GetFunction方法获取(需要引用命名空间)LuaFunction function = LuaMgr.GetInstance().Global.Get<LuaFunction>("testFun");//调用 无参无返回function.Call();//执行完过后 function.Dispose();#endregion#region 有参有返回//第一种方式 通过luafunction 的 call来访问LuaFunction function2 = LuaMgr.GetInstance().Global.Get<LuaFunction>("testFun2");Debug.Log("有参有返回值 Call:" + function2.Call(10)[0]);#endregion}
}
http://www.lryc.cn/news/191100.html

相关文章:

  • springboot项目集成kafka,并创建kafka生成消息线程池
  • PreScan与MATLAB联合仿真报错
  • ros学习笔记(1)Mac本地安装虚拟机,安装Ros2环境
  • 史上最强,Jmeter性能测试-性能场景设计实例(详全)
  • 【vim 学习系列文章 7-- vim vnormap nnoremap nmap map inoremap 区别】
  • [ERROR] COLLATION ‘utf8_unicode_ci‘ is not valid for CHARACTER SET ‘latin1‘
  • 基于rancher安装部署k8s
  • 保姆级微服务部署教程
  • springboot 定时任务
  • 【大数据】HBase入门指南
  • 零售数据分析模板鉴赏-品类销售结构报表
  • 竞赛 深度学习 机器视觉 车位识别车道线检测 - python opencv
  • MySQL中使用函数会使索引失效?
  • 金和OA C6任意文件读取漏洞 复现[附POC]
  • IP地址技术原理
  • 【网络安全】密码学基础
  • AWS AD Connector 的网络配置
  • kafka详解(三)
  • [Spring] SpringMVC 简介(二)
  • idea一些不太常用但是能提升编码效率的快捷键
  • vsto word属性信息 并读取
  • android之TextView自由选择复制
  • 【mysql】 bash: mysql: command not found
  • 鲲山科技:引入和鲸 ModelWhale,实现量化策略的高效迭代
  • PFSK152 3BSE018877R1 有源滤波器的定义
  • WebDAV之π-Disk派盘 + 恒星播放器
  • 亚马逊,速卖通,敦煌产品测评补单攻略:低成本、高安全实操指南
  • 常用linux解压命令
  • TensorFlow入门(二十二、梯度下降)
  • WPF中的多重绑定