Skip to content

Instantly share code, notes, and snippets.

@AkhmadMax
Created May 30, 2013 11:15
Show Gist options
  • Save AkhmadMax/5677186 to your computer and use it in GitHub Desktop.
Save AkhmadMax/5677186 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/
// 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