Skip to content

Instantly share code, notes, and snippets.

@keiranlovett
Forked from AkhmadMax/EventDispatcher.cs
Created February 4, 2014 11:00
Show Gist options
  • Save keiranlovett/8801653 to your computer and use it in GitHub Desktop.
Save keiranlovett/8801653 to your computer and use it in GitHub Desktop.
// C# built-in event system used in Unity3D
//
// Author: Bartek Drozdz
// Reference: http://www.everyday3d.com/blog/index.php/2010/10/04/c-events-and-unity3d/
using UnityEngine;
public class EventDispatcher : MonoBehaviour
{
public delegate void EventHandler(GameObject e);
public event EventHandler MouseOver;
void OnMouseOver ()
{
if (MouseOver != null)
MouseOver (this.gameObject);
}
}
using UnityEngine;
public class EventListener : MonoBehaviour
{
private GameObject s;
// [...]
s.GetComponent<EventDispatcher>().MouseOver += Listener;
// [...]
void Listener(GameObject g)
{
// g is being hovered, do something...
}
void OnApplicationQuit()
{
// unsubscribing
s.GetComponent<EventDispatcher>().MouseOver -= Listener;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment