-
-
Save keiranlovett/8801653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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