Unity Xlua 之 C#调用Lua(三)

Unity Xlua 之 C#调用Lua(三)

一.List和Dictionary映射table

public class Lesson06_LuaListDic : MonoBehaviour
{
    private void Start()
    {
        LuaMgr.GetInstance().Init();
        LuaMgr.GetInstance().LoadFile("Main");
        var globalTable = LuaMgr.GetInstance().Global;
        var testList1 = globalTable.Get<List<int>>("TestList1");
        foreach (var item in testList1)
            Debug.Log(item);
        var testList2 = globalTable.Get<List<object>>("TestList2");
        foreach (var item in testList2)
            Debug.Log(item);
        var testDic1 = globalTable.Get<Dictionary<string, int>>("TestDic1");
        foreach (var keyPair in testDic1)
            Debug.Log($"{keyPair.Key},{keyPair.Value}");
        var testDic2 = globalTable.Get<Dictionary<object, object>>("TestDic2");
        foreach (var keyPair in testDic2)
            Debug.Log($"{keyPair.Key},{keyPair.Value}");
    }
}
TestList1 = {1,2,3,4,5,6}
TestList2 = {"123",true,1.2,333}

TestDic1 = {
    ["1"] = 1,
    ["2"] = 2,
    ["3"] = 3,
    ["4"] = 4
}
TestDic2 = {
    ["132"] = "zzs",
    [true] = false,
    [1] = 333,
    [false] = "sss"
}

二.类映射table

public class CSCallLua
{
    public int testInt;
    public string testString;
    public bool testBool;
    public UnityAction testFunc;
    public void Test()
    {
        Debug.Log("Test");
    }
    public CSCallInLua TestInCls;
}


public class CSCallInLua
{
    public int testInt;
}

public class Lesson07_LuaClass : MonoBehaviour
{
    private void Start()
    {
        LuaMgr.GetInstance().Init();
        LuaMgr.GetInstance().LoadFile("Main");

        var globalTable = LuaMgr.GetInstance().Global;
        var callLua = globalTable.Get<CSCallLua>("TestCls");
        Debug.Log(callLua.testInt);
        Debug.Log(callLua.testString);
        Debug.Log(callLua.testBool);
        Debug.Log(callLua.TestInCls.testInt);
        callLua.testFunc?.Invoke();
    }
}
TestCls = {
    testInt = 2,
    testString = "zzs",
    testFloat = 1.2,
    testBool = false,
    testFunc = function ()
        print("类中的方法")
    end,
    TestInCls = {
        testInt = 333
    }
}

三.接口映射table

只有接口是引用传递,类和默认的int,float,string等都是值传递
需要打上[CSharpCallLua]标签,并且重新生成代码

[CSharpCallLua]
public interface ICSCallInterface
{
    int testInt { get; set; }
    string testString { get; set; }
    bool testBool { get; set; }
    void testFunc();
    CSCallLuaInterface testInInterface { get; set; }
}

public class CSCallLuaInterface
{
    public int testInt;
}

public class Lesson08_LuaInterface : MonoBehaviour
{
    private void Start()
    {
        LuaMgr.GetInstance().Init();
        LuaMgr.GetInstance().LoadFile("Main");

        var globalTable = LuaMgr.GetInstance().Global;
        var csCall = globalTable.Get<ICSCallInterface>("TestInterface");
        Debug.Log(csCall.testInt);
        Debug.Log(csCall.testString);
        Debug.Log(csCall.testBool);
        Debug.Log(csCall.testInInterface.testInt);
        csCall.testFunc();

        csCall.testInInterface.testInt = 999;
        csCall.testString = "wy";
        
        Debug.Log("=======================");
        csCall = globalTable.Get<ICSCallInterface>("TestInterface");
        Debug.Log(csCall.testInInterface.testInt);
        Debug.Log(csCall.testString);
        //**可以看出只有接口是引用传递,类和默认的int,float,string等都是值传递**
    }
}
TestInterface = {
    testInt = 1,
    testString = "zzs",
    testFloat = 1.22,
    testBool = false,
    testFunc = function ()
        print("接口中的方法")
    end,
    testInInterface = {
        testInt = 666
    }
}

四.LuaTable映射table

LuaTable尽量不要使用,影响性能,获取的LuaTable如果不手动释放或者不CG将常驻内存

public class Lesson09_LuaTable : MonoBehaviour
{
    private void Start()
    {
        LuaMgr.GetInstance().Init();
        LuaMgr.GetInstance().LoadFile("Main");
        var globalTable = LuaMgr.GetInstance().Global;
        var table = globalTable.Get<LuaTable>("TestInterface");
        Debug.Log(table.Get<int>("testInt"));
        Debug.Log(table.Get<string>("testString"));
        Debug.Log(table.Get<float>("testFloat"));
        table.Get<UnityAction>("testFunc")?.Invoke();
        var table2 = table.Get<LuaTable>("testInInterface");
        Debug.Log(table2.Get<int>("testInt"));
        //LuaTable尽量不要使用,影响性能,获取的LuaTable如果不手动释放或者不CG将常驻内存
        table.Dispose();
        table2.Dispose();
    }
}
TestInterface = {
    testInt = 1,
    testString = "zzs",
    testFloat = 1.22,
    testBool = false,
    testFunc = function ()
        print("接口中的方法")
    end,
    testInInterface = {
        testInt = 666
    }
}

五.C#调用Lua练习(用lua实现递归查找子物体)

function Find(rootTrans,name,type)
    if rootTrans.name == name then
        return rootTrans:GetComponent(type)
    end
    local target = nil
    for i = 0, rootTrans.childCount - 1 do
        target = Find(rootTrans:GetChild(i),name,type)
        if target~=nil then
            break
        end
    end
    return target
end
public class Lesson10_LuaFindChild : MonoBehaviour
{
    private void Start()
    {
        LuaMgr.GetInstance().Init();
        LuaMgr.GetInstance().LoadFile("FindChild");

        var globalTable = LuaMgr.GetInstance().Global;
        var findFunc = globalTable.Get<Func<Transform,string,Type,Transform>>("Find");
        var t = findFunc(transform, "Test", typeof(Transform));
        Debug.Log(t.name);
    }
}

你可能感兴趣的:(Xlua,lua,unity,c#)