Event

Occurrence of a change of state in an object, which is published throughout the system. An event is an object type component. The list of possible events is defined with the relevant object type in the Business Object Builder. This list can be extended according to customer requirements using the delegation concept. The actual creation of the events added must be catered for. Each event carries information from its creation context in its event container. This information is available to the receiver of the event and can be used for event-driven control and communication mechanisms. Events can start, terminate or continue tasks and workflows. Examples of events: Invoice entered and purchase order released.

----------------------
分隔线上面是 SAP 标准文档中提供的说明
分隔线下面是我的翻译
----------------------
对象中状态改变的发生,通过系统发布。事件是对象类型组件。可能事件的清单在业务对象制作器中为相关对象类型定义。这个清单可以根据客户需求使用委托概念来扩展。添加的事件的实际创建必须满足需要。每个事件获得它的事件容器中的创建者上下文的信息。这个信息可以被事件接收者获得,并且能被事件驱动控件和通讯机制使用。事件可以开始、中止或继续任务和工作流。事件的例子:发票输入和采购订单释放。  
### 如何在 Unity 中使用 UnityEvent #### 定义 UnityEvent 成员变量 为了能够在脚本中利用 `UnityEvent`,需先声明一个该类型的公共成员变量。这允许通过 Inspector 界面轻松配置响应事件的行为[^3]。 ```csharp using UnityEngine; using UnityEngine.Events; public class MyClass : MonoBehaviour { // 声明一个无参的 UnityEvent 类型的公开字段 public UnityEvent myEvent; } ``` #### 初始化 UnityEvent 实例 当创建带有泛型参数的 `UnityEvent` 或确保其不为空时,可以在 `Start()` 方法或其他初始化逻辑处实例化它[^2]。 ```csharp void Start() { // 如果 onEventTriggered 未被赋值,则新建一个接受 int 参数的 UnityEvent 对象 if (onEventTriggered == null) onEventTriggered = new UnityEvent<int>(); } ``` #### 添加监听器并触发事件 可以通过调用 `AddListener()` 方法来注册回调函数至特定的 `UnityEvent` 上;而要执行这些已绑定的方法只需简单地调用 `Invoke()` 函数即可传递必要的参数给所有的监听者[^1]。 ```csharp // 注册一个处理程序到 UnityEvent myEvent.AddListener(SomeMethod); // 调用 Invoke 来触发所有附加于 this.myEvent 的方法 if(myEvent != null){ myEvent.Invoke(); } private void SomeMethod(){ Debug.Log("This method was called by the UnityEvent."); } ``` 对于带有一个或多个参数的情况,同样适用上述模式: ```csharp // 使用带单个整数参数的 UnityEvent public UnityEvent<int> onIntEvent; // 向此 UnityEvent 添加具有相应签名的方法 onIntEvent.AddListener(OnIntReceived); // 当条件满足时,向所有监听者广播指定数值 if(onIntEvent != null){ onIntEvent.Invoke(someIntegerValue); } private void OnIntReceived(int value){ Debug.Log($"Received integer: {value}"); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值