Unity_Lua全局变量与全局函数的获取

小鸟游星野
小鸟游星野
发布于 2025-04-03 / 6 阅读
0
0

Unity_Lua全局变量与全局函数的获取

变量的获取

本质是通过获取大G表来得到引用变量

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua.LuaDLL;

public class Lesson04CallVariable : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        LuaMgr.GetInstance().InitLuaEnv();
        LuaMgr.GetInstance().DoFile("LuaMainLogic");
        //当xlua去执行require时会自动的去调用我们写的重定向函数

        int num =LuaMgr.GetInstance().luaGlobalTable.Get<int>("num");
        bool flag = LuaMgr.GetInstance().luaGlobalTable.Get<bool>("luaBool");
        Debug.Log(num);
        Debug.Log(flag);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

函数的获取

我们只需要用对应的委托去存储即可

多返回值的除了第一个值以外,其余的返回值均采用out返回

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using XLua;

public class Lesson05_ReturnFun : MonoBehaviour
{
    [CSharpCallLua]
    public delegate void OneParam(int a);
    [CSharpCallLua]
    public delegate int multiFunc(int a , out int b, out int c, out int d, out int e, out int f, out int g, out int h);
    [CSharpCallLua]
    public delegate void muuuu(params int[] a);
    
    // Start is called before the first frame update
    void Start()
    {
        LuaMgr.GetInstance().InitLuaEnv();
        LuaMgr.GetInstance().DoFile("LuaMainLogic");


        UnityAction ua = LuaMgr.GetInstance().luaGlobalTable.Get<UnityAction>("fun1");
        ua();
        OneParam p1 = LuaMgr.GetInstance().luaGlobalTable.Get<OneParam>("fun2");
        p1(1);
        multiFunc mf = LuaMgr.GetInstance().luaGlobalTable.Get<multiFunc>("fun5");
        int a, b, c, d, e, f, g;
         int jj = mf(1, out a, out b, out c, out d, out e, out f, out g);
        Debug.Log(jj+""+a + "" + b + "" + c + "" + d + "" + e + "" + f + "" + g);
        muuuu aaaawd = LuaMgr.GetInstance().luaGlobalTable.Get<muuuu>("fun4");
        aaaawd(1,2,3,4,5,21312,45654,786876,876657,23423432,1231231);

    }

    // Update is called once per frame
    void Update()
    {
        
    }
}


评论