Skip to content

Event System

高凯 edited this page Feb 5, 2018 · 2 revisions

Introduction

The framework provides GlobalEvent classes to dispatch events

API

Enumeration as Key
static void AddEvent (Enum type, EventHandle handle, bool isUseOnce = false) Register an event listener
static void RemoveEvent (Enum type, EventHandle handle) Removes an event listener
static void RemoveEvent (Enum type) Removes some type of event listener
static void DispatchEvent (Enum type, params object [] args) Dispatch event

String as Key
static void AddEvent (string eventKey, EventHandle handle, bool isUseOnce = false) Register an event listener
static void RemoveEvent (string eventKey, EventHandle handle) Removes an event listener
static void RemoveEvent (string eventKey) Remove some kind of event listener
static void DispatchEvent (string eventKey, params object [] args) Dispatch event

generic method
static void AddTypeEvent <T> (EventHandle <T> handle, bool isUseOnce = false) Register an event listener
static void RemoveTypeEvent <T> (EventHandle <T> handle, bool isUseOnce = false) Removes an event listener
static void RemoveTypeEvent <T> (bool isUseOnce = false) Remove some kind of event listener
static void DispatchTypeEvent <T> (T e, params object [] args) Dispatch events

static void RemoveAllEvent () Removes all event listeners

Example

    GlobalEvent.AddEvent (MemoryEvent.FreeHeapMemory, CleanCache);
    GlobalEvent.DispatchEvent (MemoryEvent.FreeHeapMemory);
    GlobalEvent.RemoveEvent (MemoryEvent.FreeHeapMemory, CleanCache);
    GlobalEvent.AddEvent ("GameStart", ReceviceGameStart);
    GlobalEvent.DispatchEvent ("GameStart");
    GlobalEvent.RemoveEvent ("GameStart", ReceviceGameStart);
    GlobalEvent.AddTypeEvent <SyncEntityMsg> (ReceviceSyncEntity);
    GlobalEvent.DispatchTypeEvent (new SyncEntityMsg ());
    GlobalEvent.RemoveTypeEvent <SyncEntityMsg> (ReceviceSyncEntity);
Clone this wiki locally