Events Use Example

Here is small example of button that dispatchers click with click time as data.

To create our button we should create simple mono behavior script. But instead inheriting our button class from MonoBehaviour we will inherit it from EventDispatcher to give our button ability dispatch the events. Here is full code of out Button.cs script

using UnityEngine;
using System.Collections;

public class Button : EventDispatcher {

	private float w = 150;
	private float h = 50;
	private Rect buttonRect;
	void Awake() {
	    buttonRect = new Rect((Screen.width - w) / 2, (Screen.height - h) / 2, w, h);
	void OnGUI() {
		if(GUI.Button(buttonRect, "click me")) {
			dispatch(BaseEvent.CLICK,  Time.time);


When we click GUI button it will dispatch click event  as we see if use dispatch function with data and sending Time.time variable as event data.

Next we create Class with will create this button and will listen for it event. Lets create our example listener class and attach it to any GameObject on scene. Here is full code of ExampleListner script.

using UnityEngine;
using System.Collections;

public class ExampleListner : MonoBehaviour {
	void Start () {
		//creating our button
		Button btn = gameObject.AddComponent<Button>();
		//listening for the event
		btn.addEventListener(BaseEvent.CLICK, onButtonClick);
	private void onButtonClick(CEvent e) {
		//getting click time from event
		float clickTimde = (float);
		//printing result
		Debug.Log("Button click time is: " + clickTimde.ToString());


This is basic example of building event based solution. You will find a lot more under Plugin Example folder.