18.Lua调用C#的委托和事件

18.Lua调用CSharp-委托和事件


18.1 知识点

准备工作

启动Lua脚本

LuaManager.Instance.Init();
LuaManager.Instance.DoLuaFile("Lesson18_Lua调用CSharp_委托和事件");

定义有事件和委托的测试类

public class Lesson18_Test
{
    //声明委托和事件 
    public UnityAction myDelegate;
    public event UnityAction myEvent;
    
    public void DoEvent()
    {
        if (myEvent != null)
            myEvent();
    }
    
    public void ClearEvent()
    {
        myEvent = null;
    }
}

在Lua脚本实例化测试类和几个测试用的函数

local test = CS.Lesson18_Test()

--声明几个Lua函数用于测试
local luaFunction1 = function()
    print("我是Lua函数1")
end

local luaFunction2 = function()
    print("我是Lua函数2")
end

委托

  • 委托是用来装函数的 在Lua使用C#中的委托 是用来装lua函数的
--委托加减函数
--Lua中没有复合运算符 不能+= -=
--委托中加函数 如果委托还是nil 不能直接+ 要先=赋值函数 之后加函数再使用+

--空的委托直接+函数报错
-- test.myDelegate = test.myDelegate + luaFunction1 --报错 attempt to perform arithmetic on a nil value (field 'myDelegate')

--空的委托要先=函数
test.myDelegate = luaFunction1
--执行委托
test.myDelegate() --我是Lua函数1

--非空的委托直接+函数
test.myDelegate = test.myDelegate + luaFunction2
--执行委托
test.myDelegate() --我是Lua函数1 我是Lua函数2

--往委托中加函数时可以使用匿名函数 但是不建议这样写 因为委托减函数的时候无法指定匿名函数
test.myDelegate = test.myDelegate + function()
    print("我是Lua匿名函数")
end
--执行委托
test.myDelegate() --我是Lua函数1 我是Lua函数2 我是Lua匿名函数

--可以减掉装载的函数 不能减掉匿名函数
test.myDelegate = test.myDelegate - luaFunction1
test.myDelegate = test.myDelegate - luaFunction2
--执行委托
test.myDelegate() --我是Lua匿名函数


--委托清空
test.myDelegate = nil
-- test.myDelegate()--执行空委托报错 attempt to call a nil value (field 'myDelegate')
--委托清空过后 空的委托要先=函数
test.myDelegate = luaFunction1
--执行委托
test.myDelegate() --我是Lua函数1

事件

事件加减函数

--事件加减函数 
--事件不能像委托直接=或+-函数
--需要使用语法
--对象:事件名("运算符", 函数变量)

--给事件加两个函数
--直接=设置事件的函数会报错
-- test.myEvent = luaFunction1-- 报错 cannot set myEvent, no such field
test:myEvent("+", luaFunction1)
--直接+设置事件也会报错
-- test.myEvent = test.myEvent + luaFunction2--报错  attempt to perform arithmetic on a function value (field 'myEvent')
test:myEvent("+", luaFunction2)
--调用C#封装的事件方法执行事件
test:DoEvent()--我是Lua函数1 我是Lua函数2

--匿名函数
test:myEvent("+", function()
    print("我是Lua匿名函数")
end)
--调用C#封装的事件方法执行事件
test:DoEvent()--我是Lua函数1 我是Lua函数2 我是Lua匿名函数

--可以减掉装载的函数 不能减掉匿名函数
test:myEvent("-", luaFunction1)
test:myEvent("-", luaFunction2)
--调用C#封装的事件方法执行事件
test:DoEvent()--我是Lua匿名函数

--事件清空 不能直接置空 要在C#封装置空事件的函数再在Lua调用
-- test.myEvent = nil--报错 cannot set myEvent, no such field
test:ClearEvent()
--调用c#封装的事件方法执行事件
test:DoEvent()--没有报错

18.2 知识点代码

Lesson18_Lua调用CSharp_委托和事件.lua

print('我是Lua脚本 Lesson18_Lua调用CSharp_委托和事件')

local test = CS.Lesson18_Test()

--声明几个Lua函数用于测试
local luaFunction1 = function()
    print("我是Lua函数1")
end
local luaFunction2 = function()
    print("我是Lua函数2")
end


print("**********知识点一 委托************")

--委托是用来装函数的 在Lua使用C#中的委托 是用来装lua函数的


--委托加减函数
--Lua中没有复合运算符 不能+= -=
--委托中加函数 如果委托还是nil 不能直接+ 要先=赋值函数 之后加函数再使用+

--空的委托直接+函数报错
-- test.myDelegate = test.myDelegate + luaFunction1 --报错 attempt to perform arithmetic on a nil value (field 'myDelegate')

--空的委托要先=函数
test.myDelegate = luaFunction1
--执行委托
test.myDelegate() --我是Lua函数1

--非空的委托直接+函数
test.myDelegate = test.myDelegate + luaFunction2
--执行委托
test.myDelegate() --我是Lua函数1 我是Lua函数2

--往委托中加函数时可以使用匿名函数 但是不建议这样写 因为委托减函数的时候无法指定匿名函数
test.myDelegate = test.myDelegate + function()
    print("我是Lua匿名函数")
end
--执行委托
test.myDelegate() --我是Lua函数1 我是Lua函数2 我是Lua匿名函数

--可以减掉装载的函数 不能减掉匿名函数
test.myDelegate = test.myDelegate - luaFunction1
test.myDelegate = test.myDelegate - luaFunction2
--执行委托
test.myDelegate() --我是Lua匿名函数


--委托清空
test.myDelegate = nil
-- test.myDelegate()--执行空委托报错 attempt to call a nil value (field 'myDelegate')
--委托清空过后 空的委托要先=函数
test.myDelegate = luaFunction1
--执行委托
test.myDelegate() --我是Lua函数1



print("**********知识点二 事件************")

--事件加减函数 
--事件不能像委托直接=或+-函数
--需要使用语法
--对象:事件名("运算符", 函数变量)

--给事件加两个函数
--直接=设置事件的函数会报错
-- test.myEvent = luaFunction1-- 报错 cannot set myEvent, no such field
test:myEvent("+", luaFunction1)
--直接+设置事件也会报错
-- test.myEvent = test.myEvent + luaFunction2--报错  attempt to perform arithmetic on a function value (field 'myEvent')
test:myEvent("+", luaFunction2)
--调用C#封装的事件方法执行事件
test:DoEvent()--我是Lua函数1 我是Lua函数2

--匿名函数
test:myEvent("+", function()
    print("我是Lua匿名函数")
end)
--调用C#封装的事件方法执行事件
test:DoEvent()--我是Lua函数1 我是Lua函数2 我是Lua匿名函数

--可以减掉装载的函数 不能减掉匿名函数
test:myEvent("-", luaFunction1)
test:myEvent("-", luaFunction2)
--调用C#封装的事件方法执行事件
test:DoEvent()--我是Lua匿名函数

--事件清空 不能直接置空 要在C#封装置空事件的函数再在Lua调用
-- test.myEvent = nil--报错 cannot set myEvent, no such field
test:ClearEvent()
--调用c#封装的事件方法执行事件
test:DoEvent()--没有报错

Lesson18_Lua调用CSharp_委托和事件

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

public class Lesson18_Lua调用CSharp_委托和事件 : MonoBehaviour
{
    void Start()
    {
        LuaManager.Instance.Init();
        LuaManager.Instance.DoLuaFile("Lesson18_Lua调用CSharp_委托和事件");
    }
}

public class Lesson18_Test
{
    //声明委托和事件 
    public UnityAction myDelegate;

    public event UnityAction myEvent;

    public void DoEvent()
    {
        if (myEvent != null)
            myEvent();
    }

    public void ClearEvent()
    {
        myEvent = null;
    }
}


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

×

喜欢就点赞,疼爱就打赏