c#调用lua之把lua中的表映射到c#中的类

test.lua

--class
--这样定义的表相当于是个自定义的类,有成员变量和成员函数
testClass={
	testInt=1,
	testBool=true,
	testFloat=1.3,
	testString="13",
	testFun=function ()
		-- body
		print("nihaoyahahahahha")
	end,
	testInClass={
		testInInt=13
	}

}

c#代码

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

public class CallLuaClass
{
    //在这个类中去声明lua中的table里的成员变量,且名字一定要一致!!!
    //权限要是公共的!!!!,不然如果私有和保护的没法赋值
    //这个自定义类中的成员可以更多或者更少,反正都会被忽略
    public int testInt;
    public bool testBool;
    public float testFloat;
    public string testString;

    //函数无参无返回值可以用unityaction来接
    public UnityAction testFun;
    //lua表中的表的名字与这里的要定义一致
    public CallLuaInClass testInClass;
}

//lua表中还有一个表(类),表中表里的成员名字也要与这里对应一致
public class CallLuaInClass
{
    public int testInInt;
}

public class CallClass : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        LuaMgr.GetInstance().Init();
        LuaMgr.GetInstance().DoLuaFile("Main");

        //注意用这种方式还是浅拷贝,改变这里的内容,不会改变lua表里的内容
        CallLuaClass clc = LuaMgr.GetInstance().Global.Get<CallLuaClass>("testClass");
        Debug.Log(clc.testInt);
        Debug.Log(clc.testBool);
        Debug.Log(clc.testFloat);
        Debug.Log(clc.testString);
        clc.testFun();
        Debug.Log(clc.testInClass.testInInt+"lua表中的表");

       
        
    }

    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值