Xlua热更新

た 入场券 2021-09-16 10:34 741阅读 0赞

目录

1.导入xlua插件(下载地址:https://github.com/Tencent/xLua)

2.通过xlua插件运行lua程序

3.加载运行Lua源文件

4.添加自定义的Loader方法

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

6.C#访问Lua中的全局变量

7.C#访问Lua中的table(映射到Class,不推荐使用此方式)

8.C#访问Lua中的table(推荐使用此方式,即映射到interface)

9.C#访问Lua中的全局函数(推荐使用此方式,即映射到delegate)

10.在Lua中new C#对象(创建一个游戏物体gameobject)

11.Lua访问C#静态属性和静态方法

12.Lua访问C#成员属性和成员方法


1.导入xlua插件(下载地址:https://github.com/Tencent/xLua)

70 1" class="reference-link">7070 1

2.通过xlua插件运行lua程序

70 2

  1. using UnityEngine;
  2. using XLua;
  3. public class helloworld01 : MonoBehaviour {
  4. private LuaEnv luaenv;
  5. void Start () {
  6. luaenv = new LuaEnv();
  7. luaenv.DoString("print('hello world')");
  8. luaenv.DoString("CS.UnityEngine.Debug.Log('你好')");
  9. }
  10. private void OnDestroy()
  11. {
  12. luaenv.Dispose();
  13. }
  14. }

70 3

3.加载运行Lua源文件

70 470 5

  1. using UnityEngine;
  2. public class Helloworld02 : MonoBehaviour {
  3. void Start () {
  4. TextAsset ta = Resources.Load<TextAsset>("HelloWorld.lua");
  5. print(ta);
  6. }
  7. }

70 6 70 7

  1. using UnityEngine;
  2. using XLua;
  3. public class Helloworld02 : MonoBehaviour {
  4. void Start () {
  5. TextAsset ta = Resources.Load<TextAsset>("HelloWorld.lua");
  6. LuaEnv env = new LuaEnv();
  7. env.DoString(ta.text);
  8. env.Dispose();
  9. }
  10. }

70 870 9

4.添加自定义的Loader方法

  1. using UnityEngine;
  2. using XLua;
  3. public class CreateLoader : MonoBehaviour {
  4. void Start () {
  5. LuaEnv env = new LuaEnv();
  6. env.AddLoader(MyLoader);
  7. env.DoString("require 'HelloWorld'");
  8. env.Dispose();
  9. }
  10. private byte [] MyLoader(ref string filePath)
  11. {
  12. return null;
  13. }
  14. }

70 9

  1. using UnityEngine;
  2. using XLua;
  3. public class CreateLoader : MonoBehaviour {
  4. void Start () {
  5. LuaEnv env = new LuaEnv();
  6. env.AddLoader(MyLoader);
  7. env.DoString("require 'HelloWorld'");
  8. env.Dispose();
  9. }
  10. private byte [] MyLoader(ref string filePath)
  11. {
  12. string s = "print(123)";
  13. return System.Text.Encoding.UTF8.GetBytes(s);
  14. }
  15. }

70 10

  1. using UnityEngine;
  2. using XLua;
  3. public class CreateLoader : MonoBehaviour {
  4. void Start () {
  5. LuaEnv env = new LuaEnv();
  6. env.DoString("require 'HelloWorld'");
  7. env.AddLoader(MyLoader);
  8. env.Dispose();
  9. }
  10. private byte [] MyLoader(ref string filePath)
  11. {
  12. string s = "print(123)";
  13. return System.Text.Encoding.UTF8.GetBytes(s);
  14. }
  15. }

70 11

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

  1. using System.IO;
  2. using UnityEngine;
  3. using XLua;
  4. public class CreateLoader : MonoBehaviour {
  5. void Start () {
  6. LuaEnv env = new LuaEnv();
  7. env.AddLoader(MyLoader);
  8. env.DoString("require 'test007'");
  9. env.Dispose();
  10. }
  11. private byte [] MyLoader(ref string filePath)
  12. {
  13. print(filePath);//test007
  14. print(Application.streamingAssetsPath);//E:/unityPractice/XluaPractice/Assets/StreamingAssets
  15. string absPath = Application.streamingAssetsPath + "/" + filePath + ".lua.txt";
  16. return System .Text .Encoding .UTF8 .GetBytes (File.ReadAllText (absPath ));
  17. }
  18. }

70 1270 13

6.C#访问Lua中的全局变量

  1. using UnityEngine;
  2. using XLua;
  3. public class CSharpCallLua : MonoBehaviour {
  4. void Start () {
  5. LuaEnv env = new LuaEnv();
  6. env.DoString("require 'CSharpCallLua'");
  7. int a = env.Global.Get<int>("a");
  8. print(a);
  9. string str = env.Global.Get<string >("str");
  10. print(str );
  11. bool isDie = env.Global.Get<bool >("isDie");
  12. print(isDie );
  13. env.Dispose();
  14. }
  15. }

70 1470 15

70 16 70 17

7.C#访问Lua中的table(映射到Class,不推荐使用此方式)

  1. using UnityEngine;
  2. using XLua;
  3. public class CSharpCallLua : MonoBehaviour {
  4. void Start () {
  5. LuaEnv env = new LuaEnv();
  6. env.DoString("require 'CSharpCallLua'");
  7. Person p = env.Global.Get<Person>("person");
  8. print(p.name + ":" + p.age);
  9. p.name = "nihao";
  10. env.DoString("print(person.name)");//LUA:lijiang
  11. env.Dispose();
  12. }
  13. class Person
  14. {
  15. public string name;
  16. public int age;
  17. }
  18. }

70 1870 19

8.C#访问Lua中的table(推荐使用此方式,即映射到interface)

  1. using UnityEngine;
  2. using XLua;
  3. public class CSharpCallLua : MonoBehaviour {
  4. void Start () {
  5. LuaEnv env = new LuaEnv();
  6. env.DoString("require 'CSharpCallLua'");
  7. IPerson p = env.Global.Get<IPerson>("person");
  8. print(p.name + ":" + p.age);
  9. p.name = "nihao";
  10. env.DoString("print(person.name)");//LUA:lijiang
  11. p.Eat(12, 34);
  12. env.Dispose();
  13. }
  14. [CSharpCallLua ]
  15. interface IPerson
  16. {
  17. string name { get; set; }
  18. int age { get; set; }
  19. void Eat(int a, int b);
  20. }
  21. }
  22. person={name="lijiang",age=20}
  23. function person.Eat(self,a,b)
  24. print(a+b)
  25. print("I am eating")
  26. end
  27. --[[function person:Eat(a,b) 这种方式也是可以的,默认带一个self参数,代表当前table
  28. print(a+b)
  29. print("I am eating")
  30. end
  31. --]]

70 1470 15

70 20

9.C#访问Lua中的全局函数(推荐使用此方式,即映射到delegate)

  • 不含参函数

    using UnityEngine;
    using XLua;
    using System;

    public class CSharpCallLua : MonoBehaviour {

    1. void Start () {
    2. LuaEnv env = new LuaEnv();
    3. env.DoString("require 'CSharpCallLua'");
    4. Action A= env.Global.Get<Action>("Add");
    5. A ();
    6. A = null;
    7. env.Dispose();
    8. }

    }

    function Add() —不含参函数
    print(“A”)
    end

70 21

  • 含参函数

    using UnityEngine;
    using XLua;

    public class CSharpCallLua : MonoBehaviour {

    1. void Start () {
    2. LuaEnv env = new LuaEnv();
    3. env.DoString("require 'CSharpCallLua'");
    4. Add A= env.Global.Get<Add >("Add");
    5. A (1,2);
    6. A = null;
    7. env.Dispose();
    8. }
    9. [CSharpCallLua]
    10. delegate void Add(int a, int b);

    }

    function Add(a,b) —含参函数
    print(a+b)
    end

    70 22

  • 含参函数,多返回值的情况(用ref,out都可以)

    using UnityEngine;
    using XLua;

    public class CSharpCallLua : MonoBehaviour {

    1. void Start()
    2. {
    3. LuaEnv env = new LuaEnv();
    4. env.DoString("require 'CSharpCallLua'");
    5. Add A = env.Global.Get<Add>("Add");
    6. int resa; int resb;
    7. int a = A(1, 2, out resa, out resb);
    8. print(a);
    9. print(resa);
    10. print(resb);
    11. A = null;
    12. env.Dispose();
    13. }
    14. [CSharpCallLua]
    15. delegate int Add(int a, int b, out int resa, out int resb);
    16. //void Start()
    17. //{
    18. // LuaEnv env = new LuaEnv();
    19. // env.DoString("require 'CSharpCallLua'");
    20. // Add A = env.Global.Get<Add>("Add");
    21. // int resa=0; int resb=0;
    22. // int a = A(1, 2, ref resa, ref resb);//用ref也可以,不过上面的resa要赋值,这里赋值为0
    23. // print(a);
    24. // print(resa);
    25. // print(resb);
    26. // A = null;
    27. // env.Dispose();
    28. //}
    29. //[CSharpCallLua]
    30. //delegate int Add(int a, int b, ref int resa, ref int resb);

    }

    function Add(a,b) —含参函数
    print(a+b)
    return a+b,a,b
    end

70 23

10.在Lua中new C#对象(创建一个游戏物体gameobject)

70 24

  1. using UnityEngine;
  2. using XLua;
  3. public class LuaCallCSharp : MonoBehaviour {
  4. void Start()
  5. {
  6. LuaEnv env = new LuaEnv();
  7. env.DoString("require 'LuaCallCSharp'");
  8. env.Dispose();
  9. }
  10. }

70 25

70 26

11.Lua访问C#静态属性和静态方法

  1. using UnityEngine;
  2. using XLua;
  3. public class LuaCallCSharp : MonoBehaviour {
  4. void Start()
  5. {
  6. LuaEnv env = new LuaEnv();
  7. env.DoString("require 'LuaCallCSharp'");
  8. env.Dispose();
  9. }
  10. }
  11. print(CS.UnityEngine.Time .deltaTime ) --读静态属性
  12. CS.UnityEngine.Time.timeScale=0.5 --写静态属性
  13. local gameObject=CS.UnityEngine.GameObject --调用静态方法
  14. local camera=gameObject.Find("Main Camera")
  15. camera.name="update by lua"

70 27

12.Lua访问C#成员属性和成员方法

  1. using UnityEngine;
  2. using XLua;
  3. public class LuaCallCSharp : MonoBehaviour {
  4. void Start()
  5. {
  6. LuaEnv env = new LuaEnv();
  7. env.DoString("require 'LuaCallCSharp'");
  8. env.Dispose();
  9. }
  10. }
  11. local gameObject=CS.UnityEngine.GameObject
  12. local camera=gameObject.Find("Main Camera")
  13. local cameraCom=camera:GetComponent("Camera")
  14. --用local cameraCom=camera.GetComponent(camera,"Camera")也是可以的,注意和上面用冒号的区别
  15. cameraCom.enabled = false

70 28

发表评论

表情:
评论列表 (有 0 条评论,741人围观)

还没有评论,来说两句吧...

相关阅读

    相关 更新

    单来说我的理解就是:当你的文件发现改变的时候页面自己就更新了。不用你再手动刷新页面。无刷新更新 常见的需求如赛事网页推送比赛结果、网页实时展示投票或点赞数据、在线评论或...

    相关 更新

    介绍下 webpack 热更新原理,是如何做到在不刷新浏览器的前提下更新页面   1.当修改了一个或多个文件; 2.文件系统接收更改并通知webpack; 3.we

    相关 更新

    虽然有插件开发,但热更新少不了。 ![Center][] 当我们需要更新插件的时候使用的是插件开发,当我们需要更新宿主程序就需要使用热更新 热更新使用框架bsdif