Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ls9512 committed Apr 7, 2021
1 parent fa28d54 commit 0526dae
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
30 changes: 21 additions & 9 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* 2.4. [Object Listener](#ObjectListener)
* 2.5. [Unity MonoListener](#UnityMonoListener)
* 2.6. [Event Handler](#EventHandler)
* 2.6.1. [EventHandler](#EventHandler-1)
* 2.6.2. [EventHandler\<T\>](#EventHandlerT)
* 3. [Optional Attribute](#OptionalAttribute)
* 3.1. [ EventEnumAttribute](#EventEnumAttribute)
* 3.2. [ListenAttribute](#ListenAttribute)
Expand Down Expand Up @@ -82,7 +84,7 @@
* For each type of event, use an enumerated type such as AppEvent, GameEvent, etc. Each event type corresponds to an event dispatcher instance.
* It is possible to use only one event type per project, but it is not recommended.
* Method-type listener needs to specify the binding object, while delegate-type listener does not need to specify the object.
* For the listen method of the delegate type, the parameters use the general **object[]** form, so **eventType** needs to be specified for processing, so the mandatory convention format of the delegate is **Action<T, object[]>**.
* For the listen method of the delegate type, if there are parameters, the parameters use the general **object[]** form, so **eventType** may need to be specified for processing.
* It is recommended to group events through multiple event enumerations, and group monitoring methods through the **ListenGroupAttribute**.
* When the first parameter of the method that receives the event is named **eventType**, the event type is automatically sent, and the other parameters are shifted backward in turn. It can be used to handle situations where multiple events trigger the same method.
* **enum** / **string** type events use the **UEvent** interface, **class** / **struct** type events use the **UEvent\<T\>** interface, the interface does not do Complete type constraints, but be sure to call in accordance with the convention to avoid unexpected problems.
Expand Down Expand Up @@ -111,14 +113,24 @@ The user class implements the event mechanism by inheriting the class or initial
Same as ObjectListener's interface,uesd for MonoBehaviour GameObject.

### 2.6. <a name='EventHandler'></a>Event Handler
* **Type** : Event type
* **Group** : Listen Group
* **Target** : Event target / receiver
* **Priority** : Priority
* **Interrupt** : Interrupt event queue
* **Method** : Listen method
* **Parameters** :Listen method's parameters
* **Action<T, object[]>** : Listen delegate method
#### 2.6.1. <a name='EventHandler-1'></a>EventHandler
|Property|Description|Remarks|
|-|-|-|
|Type|Event definition object type|Event definition `enum/string/class/struct` and other objects Type, equivalent to `typeof(X)`|
|EventType|The value of the listener event|1. For `enum/string` type events, it represents the specific enum value; 2. For `class/struct` events, it also appears as `Type` in the grouping definition of the listener, but it is actually An instance of this type is represented when the event is sent, and the content of the event can be included. |
|Group|Monitor group||
|Target|Event target object||
|Priority|Priority||
|Interrupt|Whether to interrupt the event queue||
|Method|Listen method|For the non-parameter delegated listening, the delegate will be automatically converted to MehtodInfo for execution|
|Parameters|Parameters of the listen method||
|Action|Listen delegate Action||
#### 2.6.2. <a name='EventHandlerT'></a>EventHandler\<T\>
|Property|Description|Remarks|
|-|-|-|
|ActionT|Listen delegate Action\<T\>||
|ActionArgs|Listen delegate Action\<obj[]\>||
|ActionTArgs|Listen delegate Action\<T, object[]\>||

***

Expand Down
37 changes: 25 additions & 12 deletions .github/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!-- vscode-markdown-toc -->
* 1. [简介](#)
* 1.1. [环境](#-1)
* 1.2. [文件](#-1)
* 1.2. [文件夹](#-1)
* 1.3. [特性](#-1)
* 1.4. [结构](#-1)
* 1.5. [约定](#-1)
Expand All @@ -25,7 +25,9 @@
* 2.3. [事件监听器 EventListener](#EventListener)
* 2.4. [Object 事件监听器 ObjectListener](#ObjectObjectListener)
* 2.5. [Unity MonoBehaviour 事件监听器 MonoListener](#UnityMonoBehaviourMonoListener)
* 2.6. [监听事件处理器 EventHander](#EventHander)
* 2.6. [监听事件处理器 EventHandler](#EventHandler)
* 2.6.1. [EventHandler](#EventHandler-1)
* 2.6.2. [EventHandler\<T\>](#EventHandlerT)
* 3. [可选特性标签 Attribute](#Attribute)
* 3.1. [事件定义枚举 EventEnumAttribute](#EventEnumAttribute)
* 3.2. [事件监听 ListenAttribute](#ListenAttribute)
Expand Down Expand Up @@ -82,7 +84,7 @@
* 每一种类型的事件,使用一种枚举类型比如: AppEvent, GameEvent 等等,每种事件类型会对应一个 事件分发器 实例。
* 可以每个项目只使用一种事件类型,但不推荐。
* 方法型监听需要指定绑定对象,而委托型监听不需要指定对象。
* 委托类型的监听方法,参数使用通用的 **object[]** 形式,因而需要指定 **eventType** 来进行处理,所以委托强制约定格式为 **Action<T, object[]>**
* 委托类型的监听方法,如果有参数,则参数使用通用 **object[]** 形式,因而可能需要指定 **eventType** 来进行处理。
* 推荐通过多个事件枚举来对事件进行分组,通过监听分组特性来对监听方法进行分组。
* 当接受事件的方法第一个参数名为 **eventType** 时,会自动发送事件类型,其他参数依次后移。可用于处理需要区分多个事件触发同一方法的情况。
* **enum** / **string** 类型事件使用 **UEvent** 接口,**class** / **struct** 类型事件使用 **UEvent\<T\>** 接口,接口没有做完整的类型约束,但请务必按照约定调用以避免不可预期的问题。
Expand Down Expand Up @@ -110,15 +112,26 @@
### 2.5. <a name='UnityMonoBehaviourMonoListener'></a>Unity MonoBehaviour 事件监听器 MonoListener
与 ObjectListener 接口相同,提供给 MonoBehaviour 游戏对象使用。

### 2.6. <a name='EventHander'></a>监听事件处理器 EventHandler
* **Type** : 事件类型
* **Group** : 监听分组
* **Target** : 事件目标对象,可空
* **Priority** : 优先级
* **Interrupt** : 是否中断事件队列
* **Method** : 监听方法
* **Parameters** :监听方法的参数
* **Action<T, object[]>** : 监听委托
### 2.6. <a name='EventHandler'></a>监听事件处理器 EventHandler
#### 2.6.1. <a name='EventHandler-1'></a>EventHandler
|属性|描述|备注|
|-|-|-|
|Type|事件定义类型|事件定义 `enum/string/class/struct` 等对象的 Type,相当于 `typeof(X)`|
|EventType|监听事件的值|1.对于 `enum/string` 类型事件,则表示具体的枚举值;2.对于 `class/struct` 事件,在监听器的分组定义中同样表现为 `Type`,但实际发送事件时表现该类型的一个实例,可以包含事件内容。|
|Group|监听分组||
|Target|事件目标对象||
|Priority|优先级||
|Interrupt|是否中断事件队列||
|Method|监听方法|对于无参数委托形式的监听,会自动将委托转换为 MehtodInfo 执行|
|Parameters|监听方法的参数||
|Action|监听委托 Action||

#### 2.6.2. <a name='EventHandlerT'></a>EventHandler\<T\>
|属性|描述|备注|
|-|-|-|
|ActionT|监听委托 Action\<T\>||
|ActionArgs|监听委托 Action\<obj[]\>||
|ActionTArgs|监听委托 Action\<T, object[]\>||

***

Expand Down
14 changes: 7 additions & 7 deletions Unity/Runtime/Script/EventDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public partial class EventDispatcher
/// <param name="args">事件参数</param>
public void DispatchSafe<T>(T eventType, params object[] args)
{
var handlerGroup = _getOrAddHandlerGroup(EventDic, eventType);
var eventList = handlerGroup.Handlers;
for (var i = 0; i < eventList.Count; i++)
var eventHandlerGroup = _getOrAddHandlerGroup(EventDic, eventType);
var eventHandlers = eventHandlerGroup.Handlers;
for (var i = 0; i < eventHandlers.Count; i++)
{
var eventData = eventList[i];
lock (eventData)
var eventHandler = eventHandlers[i];
lock (eventHandler)
{
EventManager.ExecuteUpdate(() => { eventData.Invoke(args); });
EventManager.ExecuteUpdate(() => { eventHandler.Invoke(eventType, args); });
}

if (eventData.Interrupt) break;
if (eventHandler.Interrupt) break;
}
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "com.ls9512.uevent",
"displayName": "UEvent",
"description": "UEvent is a general message event component that can be used in Unity and .Net environment.",
"version": "1.0.1",
"unity": "2019.4.3f1",
"version": "1.0.2",
"unity": "2017",
"license": "MIT",
"repository": {
"type": "git",
Expand Down

0 comments on commit 0526dae

Please sign in to comment.