10.C#把Lua表映射到接口

10.CSharp调用Lua-表映射到接口


10.1 知识点

准备工作

初始化Lua管理器,创建Lua脚本并在C#脚本调用

LuaManager.Instance.Init();
LuaManager.Instance.DoLuaFile("Lesson10_CSharp调用Lua_表映射到接口");

给Lua脚本添加普通表

print('我是Lua脚本 Lesson10_CSharp调用Lua_表映射到接口')

testClass = {

    testInt = 6,
    testBool = true,
    testFloat = 6.66,
    testString = "666",
    testFun = function()
        print("666666")
    end,

    testIntClass = 
    {
        testIntClassInt = 999
    },

    testMoreInLuaInt = 6,
}

表映射到接口

声明对应数据结构的接口

//接口中是不允许有成员变量的 
//需要使用和Lua表中的同名属性来接收映射
//使用[CSharpCallLua]特性要重新清空代码并生成代码
[CSharpCallLua]
public interface ITestClass
{
    //表映射到类的公共成员属性要和Lua脚本中被映射的表一致
    int testInt
    {
        get;
        set;
    }

    bool testBool
    {
        get;
        set;
    }

    float testFloat
    {
        get;
        set;
    }

    string testString
    {
        get;
        set;
    }

    UnityAction testFun
    {
        get;
        set;
    }

    //支持接口的嵌套
    ITestIntClass testIntClass
    {
        get;
        set;
    }

    //如果类的成员变量被映射的表少 其他成员能正常赋值
    //如果类的成员变量被映射的表多 多出来的成员会被忽略 保持默认值
    int testNoInLuaInt
    {
        get;
        set;
    }
}

[CSharpCallLua]
public interface ITestIntClass
{
    int testIntClassInt
    {
        get;
        set;
    }
}

进行映射

ITestClass testClass1 = LuaManager.Instance.Global.Get<ITestClass>("testClass");

//公共成员
Debug.Log(testClass1.testInt);//6
Debug.Log(testClass1.testBool);//True
Debug.Log(testClass1.testFloat);//6.66
Debug.Log(testClass1.testString);//666
testClass1.testFun();//666666

//类的嵌套
Debug.Log(testClass1.testIntClass.testIntClassInt);//999

//Lua表中不存在的成员 不会被赋值 打印默认值
Debug.Log(testClass1.testNoInLuaInt);//0

//使用接口改变映射到的C#类会改变Lua原表中的值
testClass1.testInt = 10000;
ITestClass testClass2 = LuaManager.Instance.Global.Get<ITestClass>("testClass");
Debug.Log(testClass2.testInt);//10000

10.2 知识点代码

Lesson10_CSharp调用Lua_表映射到接口.lua

print('我是Lua脚本 Lesson10_CSharp调用Lua_表映射到接口')

testClass = {

    testInt = 6,
    testBool = true,
    testFloat = 6.66,
    testString = "666",
    testFun = function()
        print("666666")
    end,

    testIntClass = 
    {
        testIntClassInt = 999
    },

    testMoreInLuaInt = 6,
}

Lesson10_CSharp调用Lua_表映射到接口

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

public class Lesson10_CSharp调用Lua_表映射到接口 : MonoBehaviour
{
    void Start()
    {
         LuaManager.Instance.Init();
         LuaManager.Instance.DoLuaFile("Lesson10_CSharp调用Lua_表映射到接口");

        #region 知识点一 表映射到接口

        ITestClass testClass1 =  LuaManager.Instance.Global.Get<ITestClass>("testClass");

        //公共成员
        Debug.Log(testClass1.testInt);//6
        Debug.Log(testClass1.testBool);//True
        Debug.Log(testClass1.testFloat);//6.66
        Debug.Log(testClass1.testString);//666
        testClass1.testFun();//666666

        //类的嵌套
        Debug.Log(testClass1.testIntClass.testIntClassInt);//999

        //Lua表中不存在的成员 不会被赋值 打印默认值
        Debug.Log(testClass1.testNoInLuaInt);//0

        //使用接口改变映射到的C#类会改变Lua原表中的值
        testClass1.testInt = 10000;
        ITestClass testClass2 =  LuaManager.Instance.Global.Get<ITestClass>("testClass");
        Debug.Log(testClass2.testInt);//10000

        #endregion
    }
}

#region 知识点一 表映射到接口

//接口中是不允许有成员变量的 
//需要使用和Lua表中的同名属性来接收映射
//使用[CSharpCallLua]特性要重新清空代码并生成代码
[CSharpCallLua]
public interface ITestClass
{
    //表映射到类的公共成员属性要和Lua脚本中被映射的表一致
    int testInt
    {
        get;
        set;
    }

    bool testBool
    {
        get;
        set;
    }

    float testFloat
    {
        get;
        set;
    }

    string testString
    {
        get;
        set;
    }

    UnityAction testFun
    {
        get;
        set;
    }

    //支持接口的嵌套
    ITestIntClass testIntClass
    {
        get;
        set;
    }

    //如果类的成员变量被映射的表少 其他成员能正常赋值
    //如果类的成员变量被映射的表多 多出来的成员会被忽略 保持默认值
    int testNoInLuaInt
    {
        get;
        set;
    }
}

[CSharpCallLua]
public interface ITestIntClass
{
    int testIntClassInt
    {
        get;
        set;
    }
}

#endregion


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏