Unity_Lua C#调用Lua

引用连接:

https://blog.youkuaiyun.com/qq_42393139/article/details/122629316

前提:

这篇文章中所用到的变量调用均为值拷贝,并不能改变Lua脚本中的值,除了接口拷贝(引用拷贝,会改变Lua中的值)

1. 变量调用

格式:

LuaEnv luaEnv = new LuaEnv();
类型 C#变量名 = luaEnv.Global.Get<类型>("lua变量名");

示例:
Lua:

testInt = 10
testStr = "ABC"
testBool = true
testFloat = 1.2

C#:

LuaEnv luaEnv = new LuaEnv();

int Lua_Int = luaEnv.Global.Get<int>("testInt");
Debug.Log("Lua中的Int变量:" + Lua_Int);

string Lua_Str = luaEnv.Global.Get<string>("testStr");
Debug.Log("Lua中的Int变量:" + Lua_Str);

bool Lua_Bool = luaEnv.Global.Get<bool>("testBool");
Debug.Log("Lua中的Int变量:" + Lua_Bool);

float Lua_Float = luaEnv.Global.Get<float>("testFloat");
Debug.Log("Lua中的Int变量:" + Lua_Float);

float Lua_Double = luaEnv.Global.Get<double>("testFloat");
Debug.Log("Lua中的Int变量:" + Lua_Float);

结果:

2. 方法调用
首先声明委托,用来存储Lua中的方法

lua脚本内容:

--无参数无返回值的函数
testFun1 = function()
    print("无参无返回函数")
end

--有参数无返回值的函数
testFun2_1 = function(a)
    print("有参无返回函数,参数:"..a)
end

--多参数无返回值的函数
testFun2_2 = function(a,b)
    print("有参无返回函数,参数1:"..a.."参数2:"..b)
end

--有参数有返回值的函数
testFun2_3 = function(a)
    return a
end

--多返回值的函数
testFun3 = function(a)
	return 100,200,false,"字符",a
end

C#中的委托:
注意,除了无参无返回值的委托,剩下的委托都要加上[CSharpCallLua]

//无参无返回值
//XLua已经处理了无参无返回的委托,不需要加[CSharpCallLua]
public delegate void CustomCall();
//有参无返回,一个参数
[CSharpCallLua]
public delegate void CustomCall_v1(int a);
//有参无返回,两个参数
[CSharpCallLua]
public delegate void CustomCall_v2(int a,int b);
//有参有返回,一个返回
[CSharpCallLua]
public delegate int CustomCall_v_r1(int a);
//因为第一个返回值是number,这里采用int
//一共有5个返回值,第一个作为函数默认的返回值,后面有几个返回值就加几个out,并对应类型
[CSharpCallLua]
public delegate int CustomCall_v_mr(int a,out int b,out bool c,out string d,out int e);

调用方法类似变量

		//无参返回
        CustomCall call = XLuaMgr.GetInstance().Global.Get<CustomCall>("testFun1");
        call();
        //有参无返回,一个参数
        CustomCall_v1 call_v1 = XLuaMgr.GetInstance().Global.Get<CustomCall_v1>("testFun2_1");
        call_v1(123);
        //有参无返回,两个参数
        CustomCall_v2 call_v2 = XLuaMgr.GetInstance().Global.Get<CustomCall_v2>("testFun2_2");
        call_v2(111,222);
        //有参数有返回值 一个返回值
        CustomCall_v_r1 call_v_r1 = XLuaMgr.GetInstance().Global.Get<CustomCall_v_r1>("testFun2_3");
        Debug.Log("有参有返回值的函数:"+ call_v_r1(456));
        //多返回值函数用out来接收
        CustomCall_v_mr call_v_mr = XLuaMgr.GetInstance().Global.Get<CustomCall_v_mr>("testFun3");
        int a,b;
        bool c;
        string d;
        int e;
        Debug.Log(call_v_mr(1,out b,out c,out d,out e));

运行前记得执行XLua------>Generate Code,不然直接运行会报错,因为我们将委托打上了[CSharpCallLua],所以需要XLua生成相应代码

运行后打印:
在这里插入图片描述
变长函数委托:

lua脚本内容:

--变长参数函数1
testfun4_1 = function(...)
    arg={...}
    for k,v in pairs(arg) do
        print(k,v)
    end
end
--变长参数函数2
testfun4_2 = function(a,...)
    print("变长参数的固定参数:"..a)
    arg={...}
    for k,v in pairs(arg) do
        print(k,v)
    end
end

C#中的委托:

//变长参数的类型根据实际情况设定,通用情况用object,但会增加开销
[CSharpCallLua]
public delegate void CustomCall_mv(params object[] args);
//变长参数+固定参数
[CSharpCallLua]
public delegate void CustomCall_mv_2(int a,params object[] args);

调用Lua中的变长参数:

 //变长参数
CustomCall_mv call_mv = XLuaMgr.GetInstance().Global.Get<CustomCall_mv>("testfun4_1");
call_mv(1,2,3,4,5,"AA",true);
//变长参数+固定参数
CustomCall_mv_2 call_mv_2 = XLuaMgr.GetInstance().Global.Get<CustomCall_mv_2>("testfun4_2");
call_mv_2(100,1,2,3,4,5,"AA",true);

3. C#调用Lua中的table

lua代码:

--List
testList={11,22,33,44,55,66}
testList2={"ABCD",123,true,11,1.23}

testDic={
	["1"]=11,
	["2"]=22,
	["3"]=33,
	["4"]=44
}

C#代码:

List<int> list = XLuaMgr.GetInstance().Global.Get<List<int>>("testList");

List<object> list2 = XLuaMgr.GetInstance().Global.Get<List<object>>("testList2");

Dictionary<string,int> dic = XLuaMgr.GetInstance().Global.Get<Dictionary<string,int>>("testDic");

4. C#调用"类"
Lua代码

testClass={
	testInt=2,
	testBool=true,
	testFloat=1.2,
	testString="123",
	testFun=function()
		print("lua中的类方法执行")
	end
}

C#代码
注:名字一定要和Lua那边一样

public class CallLuaClass
{
    //在这个类中去声明成员变量
    //名字一定要和Lua那边一样
    //公共 私有和保护没办法赋值
    //这个自定义中的 变量 可以更多也可以更少
    //如果变量比 lua中的少 就会忽略它
    //如果变量比 lua中的多 不会赋值 也会忽略
    public int testInt;
    public bool testBool;
    public float testFloat;
    public float testString;
    public UnityAction testFun;

    public CallLuaInClass testInClass;

    public int i;

    public void Test()
    {
        Debug.Log(testInt);
    }

}
public class CallLuaInClass
{
    public int testInClass;
}

调用:

CallLuaClass obj = LuaMgr.GetInstance().Global.Get<CallLuaClass>("testClass");
Debug.Log(obj.testInt);
Debug.Log(obj.testBool);
Debug.Log(obj.testFloat);
Debug.Log(obj.testString);
Debug.Log(obj.i);

5. C#调用接口
示例接口

//接口中不允许有成员变量
//我们用属性来接收
//接口和类的规则一样 其中的的属性多了少了不影响结果 无法就是忽略他们
//嵌套几乎和类一样 无法是要遵循接口的规则
//如果接口中的内容变了,需要重新清理代码再生成代码
[CSharpCallLua]
public interface ICSharpCallInterface
{
    int testInt
    {
        get;
        set;
    }
    bool testBool
    {
        get;
        set;
    }
    float testFloat
    {
        get;
        set;
    }
    //string testString
    //{
    //    get;
    //    set;
    //}
    UnityAction testFun
    {
        get;
        set;
    }
    string test222
    {
        get;
        set;
    }
}

Lua中的类

testClass={
testInt=2,
testBool=true,
testFloat=1.2,
testString="123",
testFun=function()
	print("123123")
end
}

C#调用

ICSharpCallInterface obj = XLuaMgr.GetInstance().Global.Get<ICSharpCallInterface>("testClass");
Debug.Log(obj.testInt);
Debug.Log(obj.testBool);
Debug.Log(obj.testFloat);
Debug.Log(obj.test222);
obj.testFun();
//接口拷贝 是引用拷贝 改了值 lua表中的值也变了
obj.testInt = 10000;
ICSharpCallInterface obj2 = XLuaMgr.GetInstance().Global.Get<ICSharpCallInterface>("testClass");
Debug.Log(obj2.testInt);   

运行打印(注意加了新的[CSharpCallLua],在运行前要执行XLua----->Generate Code)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值