xLua_001 Lua 文件加载

server/2025/3/28 15:57:23/

xLua下载

1、HelloWrold 代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;   // 引入XLua命名空间 public class Helloworld01 : MonoBehaviour
{//声明LuaEnv对象  private LuaEnv luaenv;void Start(){//实例化LuaEnv对象luaenv = new LuaEnv();//执行lua代码 外面的双引号里面的是lua代码luaenv.DoString("print('Hello world')");}private void OnDestroy(){//释放LuaEnv对象luaenv.Dispose();}
}

输出结果:

2、环境管理规范

一个unity 项目最好只有一个 LuaEnv 实例

输出结果:

3、建立单独的Lua文件

单独的lua文件:
lua程序放到resources文件夹里面,来加载这个程序,获取里面的字符串,把字符串当做一个参数放在C#文件中执行 

  • resources文件中

 引用脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;   // 引入XLua命名空间 public class Helloworld02 : MonoBehaviour
{void Start(){   //文件名:helloworld.lua.txtTextAsset ta = Resources.Load<TextAsset>("helloworld.lua");     LuaEnv env = new LuaEnv();env.DoString(ta.text);   // env.DoString(ta.ToString());env.Dispose();}
}

输出结果:

4、使用系统内置加载Lua的方式

5、自定义加载器Loader

1) 加载一个不存在的lua文件(利用系统内置的加载方式,自定义Loader为空)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;public class Createloader : MonoBehaviour
{void Start(){LuaEnv env = new LuaEnv();env.AddLoader(MyLoader);env.DoString("require 'XXXXXX'");env.Dispose();    }private byte[] MyLoader(ref string filePath){return null;}}

输出结果:

2) 加载helloworld  lua文件(利用系统内置的加载方式,自定义Loader为空)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;public class Createloader : MonoBehaviour
{void Start(){LuaEnv env = new LuaEnv();env.AddLoader(MyLoader);env.DoString("require 'helloworld'");env.Dispose();    }private byte[] MyLoader(ref string filePath){//输出文件名  helloworldprint(filePath);//自定义loader为空return null;}}

输出结果:

 输出顺序:自定义Loader ->>  系统内置的Loader

3) 加载  自定义Loader (首先执行自定义Loader程序   找到字节数组,执行自定义Loader,系统内置的Loader程序,不支执行)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;public class Createloader : MonoBehaviour
{void 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)";//字符串转为字节数组//通过System.Text.Encoding.UTF8转为字节数组  //通过GetBytes() 来获取数组return System.Text.Encoding.UTF8.GetBytes(s);}}

输出结果:

6、通过 路径 加载Lua文件

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using XLua;public class Createloader : MonoBehaviour
{void Start(){LuaEnv env = new LuaEnv();env.AddLoader(MyLoader);env.DoString("require 'test007'");env.Dispose();    }private byte[] MyLoader(ref string filePath){print(filePath);//return System.Text.Encoding.UTF8.GetBytes(s);print(Application.streamingAssetsPath);//路径构建    拼接完整路径<Project>/Assets/StreamingAssets/test007.lua.textstring absPath = Application.streamingAssetsPath + "/" + filePath + ".lua.text";//File.ReadAllText(absPath):文件读取(读取指定路径的文本文件内容)//字节转换return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));}}


http://www.ppmy.cn/server/177122.html

相关文章

二项式分布(Binomial Distribution)

二项式分布&#xff08;Binomial Distribution&#xff09; 定义 让我们来看看玩板球这个例子。假设你今天赢了一场比赛&#xff0c;这表示一个成功的事件。你再比了一场&#xff0c;但你输了。如果你今天赢了一场比赛&#xff0c;但这并不表示你明天肯定会赢。我们来分配一个…

怎么查看linux是Ubuntu还是centos

要确定你的Linux系统是基于Ubuntu还是CentOS&#xff0c;可以通过几种不同的方法来进行判断。下面是一些常用的方法&#xff1a; 要快速判断 Linux 系统是 Ubuntu 还是 CentOS&#xff0c;可通过以下方法综合验证&#xff1a; 一、查看系统信息文件 1. /etc/os-release 文件…

Linux面试题

&#x1f9d1; 博主简介&#xff1a;CSDN博客专家&#xff0c;历代文学网&#xff08;PC端可以访问&#xff1a;https://literature.sinhy.com/#/?__c1000&#xff0c;移动端可微信小程序搜索“历代文学”&#xff09;总架构师&#xff0c;15年工作经验&#xff0c;精通Java编…

libaom 源码分析:scalable_decoder.c 文件

libaom 基本特性 开放和免版税&#xff1a;libaom 提供了一个开放源代码的编码器&#xff0c;任何个人和组织都可以免费使用&#xff0c;无需支付版税&#xff0c;这促进了它在各种应用中的广泛采用。高效的编码&#xff1a;旨在提供高效的视频压缩&#xff0c;以适应不同的网络…

doris:审计日志

Doris 提供了对于数据库操作的审计能力&#xff0c;可以记录用户对数据库的登陆、查询、修改操作。在 Doris 中&#xff0c;可以直接通过内置系统表查询审计日志&#xff0c;也可以直接查看 Doris 的审计日志文件。 开启审计日志​ 通过全局变量 enable_audit_plugin 可以随时…

GCC 预定义宏:解锁编译器的隐藏信息

GCC 预定义宏&#xff1a;解锁编译器的隐藏信息 在 GCC 编译器中&#xff0c;有许多内置的预定义宏&#xff0c;它们可以提供编译环境的信息&#xff0c;如文件名、行号、时间、版本等。这些宏在调试、日志记录、条件编译等场景中非常有用。本文将介绍常见的 GCC 预定义宏&…

SpringSecurity——前后端分离登录认证

SpringSecurity——前后端分离登录认证的整个过程 前端&#xff1a; 使用Axios向后端发送请求 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>登录</title><script src"https://cdn…

基于DeepSeek的智能体搭建

智能体是什么&#xff1f; 智能体是一种生成式人工智能应用程 序&#xff0c;其核心是通过观察环境并利用工 具自主采取行动以实现特定目标。 智能体架构 •大模型&#xff08;Language Model&#xff09;&#xff1a;作为agent流程的集中决策者使用的语言模型&#xff0c;能…