9.C#把Lua表映射到类

9.CSharp调用Lua-表映射到类


9.1 知识点

准备工作

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

LuaManager.Instance.Init();
LuaManager.Instance.DoLuaFile("Lesson09_CSharp调用Lua_表映射到类");

给Lua脚本添加普通表和字典表

print('我是Lua脚本 Lesson09_CSharp调用Lua_表映射到类')

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

    testIntClass = 
    {
        testIntClassInt = 999
    },

    testPrivateInt = 6,
    testProtectedInt  = 6,

    testMoreInLuaInt = 6,
}

testDictionaryClass = {
    testDictionaryClassDictionary = {
        ["1"] = 1,
        ["2"] = 2,
        ["3"] = 3,
        ["4"] = 4
    }
}

普通表映射类

声明对应数据结构的类

public class TestClass
{
    //表映射到类的公共成员名要和Lua脚本中被映射的表一致
    public int testInt;
    public bool testBool;
    public float testFloat;
    public string testString;
    public UnityAction testFun;

    //支持类的嵌套
    public TestIntClass testIntClass;

    //公共的成员才能被赋值 私有和保护的成员无法赋值 无法成功映射
    private int testPrivateInt;
    protected int testProtectedInt;
    public void TestPrintPrivateAndProtectedFunction()
    {
        Debug.Log("testPrivateInt:" + testPrivateInt);
        Debug.Log("testProtectedInt:" + testProtectedInt);
    }


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

}

public class TestIntClass
{
    public int testIntClassInt;
}

进行映射

TestClass testClass1 = LuaManager.Instance.Global.Get<TestClass>("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

//私有和保护的成员无法被赋值 无法成功映射
testClass1.TestPrintPrivateAndProtectedFunction();
//testPrivateInt: 0
//testProtectedInt: 0

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

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

字典表映射类

声明对应数据结构的类

public class TestDictionaryClass
{
    public Dictionary<string, int> testDictionaryClassDictionary;
}

进行映射

TestDictionaryClass testDictionaryClass = LuaManager.Instance.Global.Get<TestDictionaryClass>("testDictionaryClass");
Debug.Log(testDictionaryClass.testDictionaryClassDictionary);//System.Collections.Generic.Dictionary`2[System.String,System.Int32]
Debug.Log(testDictionaryClass.testDictionaryClassDictionary.Count);//4
//遍历字典时不会按顺序 可能是乱序
foreach (string item in testDictionaryClass.testDictionaryClassDictionary.Keys)
{
    Debug.Log(item + "_" + testDictionaryClass.testDictionaryClassDictionary[item]);
}

9.2 知识点代码

Lesson09_CSharp调用Lua_表映射到类.lua

print('我是Lua脚本 Lesson09_CSharp调用Lua_表映射到类')

testClass = {

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

    testIntClass = 
    {
        testIntClassInt = 999
    },

    testPrivateInt = 6,
    testProtectedInt  = 6,

    testMoreInLuaInt = 6,
}

testDictionaryClass = {
    testDictionaryClassDictionary = {
        ["1"] = 1,
        ["2"] = 2,
        ["3"] = 3,
        ["4"] = 4
    }
}

Lesson09_CSharp调用Lua_表映射到类

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

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

        #region 知识点一 普通表映射类

        TestClass testClass1 = LuaManager.Instance.Global.Get<TestClass>("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

        //私有和保护的成员无法被赋值
        testClass1.TestPrintPrivateAndProtectedFunction();
        //testPrivateInt: 0
        //testProtectedInt: 0

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

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

        #endregion

        #region 知识点二 字典表映射类

        TestDictionaryClass testDictionaryClass = LuaManager.Instance.Global.Get<TestDictionaryClass>("testDictionaryClass");
        Debug.Log(testDictionaryClass.testDictionaryClassDictionary);//System.Collections.Generic.Dictionary`2[System.String,System.Int32]
        Debug.Log(testDictionaryClass.testDictionaryClassDictionary.Count);//4
        //遍历字典时不会按顺序 可能是乱序
        foreach (string item in testDictionaryClass.testDictionaryClassDictionary.Keys)
        {
            Debug.Log(item + "_" + testDictionaryClass.testDictionaryClassDictionary[item]);
        }

        #endregion
    }
}


#region 知识点一 普通类的映射

public class TestClass
{
    //表映射到类的公共成员名要和Lua脚本中被映射的表一致
    public int testInt;
    public bool testBool;
    public float testFloat;
    public string testString;
    public UnityAction testFun;

    //支持类的嵌套
    public TestIntClass testIntClass;

    //公共的成员才能被赋值 私有和保护的成员无法赋值
    private int testPrivateInt;
    protected int testProtectedInt;
    public void TestPrintPrivateAndProtectedFunction()
    {
        Debug.Log("testPrivateInt:" + testPrivateInt);
        Debug.Log("testProtectedInt:" + testProtectedInt);
    }


    //如果类的成员变量比被映射的表少,其余成员仍能正常赋值
    //如果类的成员变量比被映射的表多,多出来的成员会被忽略,保持默认值
    public int testNoInLuaInt;

}

public class TestIntClass
{
    public int testIntClassInt;
}

#endregion

#region 知识点二 字典表映射类

public class TestDictionaryClass
{
    public Dictionary<string, int> testDictionaryClassDictionary;
}

#endregion


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

×

喜欢就点赞,疼爱就打赏