xLua中Lua呼叫C#

小紫蘇xw發表於2021-10-12

xLua中Lua呼叫C#

1.前提

這裡使用的是XLua框架,需要提前配置xlua,設定載入器路徑;

可以參考之前的Blog:《xlua入門基礎》

//呼叫端,所有的lua程式碼都寫在LuaCallCSharp.lua檔案中
public class LuaCallCSharp1 : MonoBehaviour
{
    void Start()
    {
        XluaEnv.I.DoString("LuaCallCSharp");
    }

    private void OnDestroy()
    {
        XluaEnv.I.Free();
    }
}

2.呼叫C#類

靜態類

public static class TestStatic
{
    public static void ShowName(string name, int id)
    {
        Debug.Log($"name:{name},id:{id}");
    }
}

--靜態類
CS.TestStatic.ShowName("littlePerilla",1012);

image-20211011155557124

動態類

public class NPC
{
    public string name;
    public int attack;

    public NPC(string name,int attack)
    {
        this.name = name;
        this.attack = attack;
    }

    public int Hp { get; set; }

    public void Attack()
    {
        Debug.Log($"attack:{attack},Hp:{Hp}");
    }
}

--類物件
local hero = CS.NPC("Angel",100)
hero.Hp = 110
hero:Attack()

image-20211011155610643

呼叫Unity官方Api

--建立物體
local go = CS.UnityEngine.GameObject("LuaObj ")
--新增元件
go:AddComponent(typeof(CS.UnityEngine.BoxCollider))

Lua不支援泛型,所有用到泛型的地方需要把每種可能的過載都寫一遍;

image-20211011155657877

呼叫父類和子類

public class Father
{
    public string name = "father";

    public virtual void Say()
    {
        Debug.Log($"{name}:我在被呼叫");
    }
}

public class Child :Father
{
    public string name = "child";

    public override void Say()
    {
        Debug.Log($"{name}:我在被呼叫");
    }
}

local father = CS.Father()
father:Say()

local child = CS.Child()
child:Say()

image-20211011155338118

類擴充方法

擴充類必須為靜態類,類必須加特性[LuaCallCSharp];《C#類擴充方法》;

會受到xlua版本和unity版本影響,導致呼叫失敗,xlua官方推薦版本是2017(太過時了);

public class TestExtension
{
    public string Test1()
    {
        return "test";
    }
}

[LuaCallCSharp]
public static class MyExtension
{
    public static void Test2(this TestExtension obj)
    {
        Debug.Log("ExtensionFunc:"+ obj.Test1());
    }
}

local testEx = CS.TestExtension()

print(testEx:Test1())
testEx:Test2()

3.呼叫C#結構體

結構體和類相似,都有構造方法;

public struct TestStruct
{
    public int id;
    public string name;

    public void Output()
    {
        Debug.Log(id);
        Debug.Log(name);
    }
}

--結構體
local teststrut = CS.TestStruct()

teststrut.id = 12
teststrut.name = "littlePerilla"

teststrut:Output()

image-20211011155503446

4.呼叫C#列舉

列舉使用的userdate自定義資料型別;

public enum State
{
    idle = 0,
    walk,
    run,
    attack
}

--列舉使用的userdate自定義資料型別
local state = CS.State.idle

print(state)

--轉換獲得列舉
print(CS.State.__CastFrom(1))
print(CS.State.__CastFrom("run"))

image-20211011155441582

5.呼叫C#中委託

靜態委託賦值呼叫必須釋放;

動態委託不必要,但是最好也釋放;

呼叫委託前先做為空判定;

Lua中沒有+=或-=方法,只能通過a = a+b來實現多播;

public delegate void DelegateLua();

public class TestDelegate
{
    public static DelegateLua deStatic;

    public DelegateLua deDynamic;

    public static void Func()
    {
        Debug.Log("靜態委託");
    }

    public void Func2()
    {
        Debug.Log("動態委託");
    }
}

--靜態委託賦值呼叫必須釋放
CS.TestDelegate.deStatic = CS.TestDelegate.Func
CS.TestDelegate.deStatic()
CS.TestDelegate.deStatic = nil

local func = function ()
	-- body
	print("lua函式替換委託")
end

--lua函式賦值委託
CS.TestDelegate.deStatic = func
--多播委託,確定deStatic不為空,lua沒有+=和-=
if(CS.TestDelegate.deStatic ~= nil)then
	CS.TestDelegate.deStatic = CS.TestDelegate.deStatic + func
else 
	CS.TestDelegate.deStatic = func
end

--呼叫前判定不為空
if(CS.TestDelegate.deStatic ~= nil)then
	CS.TestDelegate.deStatic()
end

CS.TestDelegate.deStatic = nil
--動態委託
local test = CS.TestDelegate()

local func1 = function()
	print("動態委託呼叫")
end

test.deDynamic = func1
test.deDynamic()
test.deDynamic = nil

image-20211011155034038

6.呼叫C#事件

事件的呼叫不能直接複製,必須使用(“+”,function);

事件回撥完成也需要釋放;

public delegate void EventLua();

public class TestEvent
{
    public event EventLua luaEvent1;
    
    public static event EventLua luaEvent2;

    public static void Func()
    {
        Debug.Log("靜態事件");
    }

    public static void CallEvent2()
    {
        if (luaEvent2 != null)
            luaEvent2();
    }

    public void CallEvent1()
    {
        if (luaEvent1 != null)
            luaEvent1();
    }
}

--靜態事件
CS.TestEvent.luaEvent2("+",CS.TestEvent.Func)
CS.TestEvent.CallEvent2()
CS.TestEvent.luaEvent2("-",CS.TestEvent.Func)


--動態事件
local test = CS.TestEvent()

local func = function ()
	print("動態事件")
end

test:luaEvent1("+",func)
test:CallEvent1()
test:luaEvent1("-",func)

image-20211011154943195

相關文章