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

xLua学习

xLua教程:https://github.com/Tencent/xLua/blob/master/Assets/XLua/Doc/XLua%E6%95%99%E7%A8%8B.md
xLua配置:https://github.com/Tencent/xLua/blob/master/Assets/XLua/Doc/configure.md
FAQ:https://github.com/Tencent/xLua/blob/master/Assets/XLua/Doc/faq.md

 开始学习

private LuaEnv luaenv;// Start is called before the first frame updatevoid Start(){luaenv = new LuaEnv();luaenv.DoString("print('Hello World!')");}private void OnDestroy(){luaenv.Dispose();}

 在lua中调用C#中的方法

luaenv.DoString("CS.UnityEngine.Debug.Log('Hello World!')");

 加载运行Lua源文件

在Resources文件夹中建一个 helloworld.lua.txt 文件

里面写着lua 程序

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;public class helloworld02 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){TextAsset textAsset = Resources.Load<TextAsset>("helloworld.lua");LuaEnv luaEnv = new LuaEnv();luaEnv.DoString(textAsset.ToString());luaEnv.Dispose();}

通过内置的 loader 加载lua 源文件

luaenv.DoString(" require 'helloworld'");  //loader  会直接执行里面的代码

 添加自定义的 loader 方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;public class defineloader03 : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){LuaEnv env = new LuaEnv();env.AddLoader(MyLoader);env.DoString("require 'helloworld'");env.Dispose();}private byte[] MyLoader(ref string filepath){print(filepath);string s = "print(123)";// 将字符串转成字节数组return System.Text.Encoding.UTF8.GetBytes(s);}

 require 会将路径传递给每一个 loader 去执行,直到找到或者确定完全找不到为止(报异常)

通过自定义Loader 加载指定目录的Lua脚本

http://www.lryc.cn/news/112414.html

相关文章:

  • ​Web3到底是个啥?
  • pycharm、idea、golang等JetBrains其他IDE修改行分隔符(换行符)
  • ThinkPHP函数深度解析
  • 【java】【maven】【高级】MAVEN聚合继承属性等
  • LeetCode150道面试经典题-合并两个有序数组(简单)
  • 记录 运维三剑客一件部署的的docker-compose,yml文件
  • Xposed框架开发
  • 2.13 Android ebpf非网络相关帮助函数API汇总(十二 本章完)
  • 关于游戏的笔记
  • vue diff 前后缀+最长递增子序列算法
  • 【Python】Locust持续优化:InfluxDB与Grafana实现数据持久化与可视化分析
  • 数组模拟循环链表
  • 第三章 图论 No.5最小生成树之虚拟源点,完全图与次小生成树
  • RESTful API的讲解以及用PHP实现RESTful API
  • Spring中@Component和@Bean的区别
  • 【问题解决】mysql 数据库字符串分割之后多行输出方法
  • flutter开发实战-时间显示刚刚几分钟前几小时前
  • 导出LLaMA等LLM模型为onnx
  • 回顾 OWASP 机器学习十大风险
  • ENSP软件的基本使用命令(第三十一课)
  • 五、FreeRTOS数据类型和编程规范
  • 码出高效_第二章 | 面向对象_上
  • 大学生课设实训|基于springboot的在线拍卖系统
  • 论文阅读 - Social bot detection in the age of ChatGPT: Challenges and opportunities
  • FPGA优质开源项目 - UDP RGMII千兆以太网
  • 学C的第三十二天【动态内存管理】
  • 聊聊elasticsearch的data-streams
  • unreal engine c++ 创建tcp server, tcp client
  • 24届华东理工大学近5年自动化考研院校分析
  • 初识集合和背后的数据结构