找回密码
 立即注册
查看: 202|回复: 0

Xlua的使用

[复制链接]
发表于 2023-4-10 10:36 | 显示全部楼层 |阅读模式
xlua下载地址: https://github.com/Tencent/xLua
下载后将Xlua文件夹复制到Assets下。
一、 调用C#方法。
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XLua;
  5. public class HelloWord : MonoBehaviour
  6. {
  7.     private LuaEnv luaenv;//创建虚拟机
  8.     // Start is called before the first frame update
  9.     void Start()
  10.     {
  11.         luaenv = new LuaEnv();
  12.         luaenv.DoString("print('Hello Xlua')");//使用lua进行编译
  13.         //luaenv.DoString("CS.UnityEngine.Debug.Log('Xlua')");//使用unity中的debug。
  14.       
  15.     }
  16.     private void OnDestroy()
  17.     {
  18.         luaenv.Dispose();//释放内存
  19.     }
  20. }
复制代码
二、加载lua源文件。
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XLua;
  5. using System.IO;
  6. public class mybyfile : MonoBehaviour
  7. {
  8.     // Start is called before the first frame update
  9.     void Start()
  10.     {
  11.        // TextAsset taxt = Resources.Load<TextAsset>("helloworld");
  12.         LuaEnv luaenv = new LuaEnv();
  13.         //luaenv.DoString(taxt.text);
  14.         //uaenv.DoString("require'helloworld'"); //文件名必须要使用.lua为后缀名,文件要在Resources文件夹下。
  15.         luaenv.AddLoader(MyLoader);
  16.         luaenv.DoString("require'textlua'");
  17.         luaenv.Dispose();
  18.     }
  19.     //自定义loader
  20.     private byte[] MyLoader(ref string filePath)
  21.     {
  22.         print(Application.streamingAssetsPath);
  23.         string path = Application.streamingAssetsPath + "/" + filePath + ".lua.txt";
  24.         
  25.         return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(path));
  26.     }
  27. }
复制代码
三、访问lua文件中的全局变量
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XLua;
  5. public class CSharpLua : MonoBehaviour
  6. {
  7.     void Start()
  8.     {
  9.         LuaEnv env = new LuaEnv();
  10.         env.DoString("require 'Csharplua'");
  11.         var a = env.Global.Get<int>("a");//获取lua里边的全局变量
  12.         var b = env.Global.Get<string>("str");//获取lua里边的全局变量
  13.         var c = env.Global.Get<bool>("mybool");//获取lua里边的全局变量
  14.         print(a);
  15.         print(b);
  16.         print(c);
  17.         //访问lua中的table,映射到class中
  18.         Per p = env.Global.Get<Per>("person");
  19.         print(p.name + " " + p.age);
  20.         env.Dispose();
  21.         
  22.     }
  23.    class Per
  24.     {
  25.         public string name;
  26.         public int age;
  27.     }
  28. }
复制代码
四、lua脚本修改C#中的方法
  1.     //重新定义c#方法中的内容
  2.     local engine=CS.UnityEngine
  3.         xlua.hotfix(CS.Treasour,'CreatePrize',function(self)
  4.                 print("1111")
  5.     end)//{0}要修改的函数所在类名,{1}要修改的方法名,{2}修改后的方法
  6.         //修改的脚本中需要添加[Hotfix]标签,要修改的方法添加[LuaCallCSharp]标签。
  7.     //基于原方法新增内容
  8.     local util= require 'util'
  9.         xlua.private_accessible(CS.Boss)//使用类中的私有成员变量
  10.         util.hotfix_ex(CS.Boss,'Start',function(self)
  11.                 self.Start(self)//执行一次原方法中的内容
  12.         print(self.name)//新增内容
  13.         end)//注意需要将util.lua文件提前导入工程。
复制代码
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-24 06:02 , Processed in 0.096114 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表