Advertisement
Muk99

DelegatesExample

Feb 13th, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.66 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EventManager : MonoBehaviour {
  5.     public delegate void PowerUpHandler (bool isPoweredUp);
  6.     public static event PowerUpHandler onPoweredUp;
  7.  
  8.     public void PowerUp () {
  9.         if (onPoweredUp != null)
  10.             onPoweredUp (true);
  11.     }
  12.  
  13.     public void PowerOff () {
  14.         if (onPoweredUp != null)
  15.             onPoweredUp (false);
  16.     }
  17. }
  18.  
  19. public class EventListener : MonoBehaviour {
  20.     void OnEnable () {
  21.         EventManager.onPoweredUp += OnPoweredChange;
  22.     }
  23.  
  24.     void OnDisable () {
  25.         EventManager.onPoweredUp -= OnPoweredChange;
  26.     }
  27.  
  28.     void OnPoweredChange (bool isPoweredUp){
  29.         print (isPoweredUp? "Powered" : "Unpowered");
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement