Advertisement
Guest User

Events

a guest
Nov 20th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public enum EventsID
  7. {
  8.     Reset,
  9.     ChooseItem,
  10.     PerformAction
  11. }
  12.  
  13. public class EventManager
  14. {
  15.     // Generic callback
  16.     #region PublicDelegate
  17.     public delegate void Callback();
  18.     public delegate void Callback<T>(T arg1);
  19.     #endregion
  20.     //public delegate void Callback<T, U>(T arg1, U arg2);
  21.  
  22.     private static Dictionary<EventsID, Delegate> eventsDictionary = new Dictionary<EventsID, Delegate>();
  23.  
  24.     //take event id in input
  25.     public static void TriggerEvent(EventsID InEventID)
  26.     {
  27.         //if there is event with id key
  28.         if (eventsDictionary.ContainsKey(InEventID))
  29.         {
  30.             //invoke as CallBack without input
  31.             (eventsDictionary[InEventID] as Callback).Invoke();
  32.         }
  33.     }
  34.  
  35.     //take event id and method input as input
  36.     public static void TriggerEvent<T>(EventsID InEventID, T InVariable)
  37.     {
  38.         //if there is event with id key
  39.         if (eventsDictionary.ContainsKey(InEventID))
  40.         {
  41.             //invoke as CallBack with input
  42.             (eventsDictionary[InEventID] as Callback<T>).Invoke(InVariable);
  43.         }
  44.     }
  45.  
  46.     //call this method to add event without input
  47.     public static void StartListening(EventsID InEventID, Callback listener)
  48.     {
  49.         //check if ther is dictionary with correct key
  50.         if (!eventsDictionary.ContainsKey(InEventID))
  51.         {
  52.             //if there isn't add event
  53.             eventsDictionary.Add(InEventID, listener);
  54.         }
  55.  
  56.         else
  57.         {
  58.             //take delegate, cast it ass callbakc and add callback (use this to check if signature as equal)
  59.             eventsDictionary[InEventID] = (Callback)eventsDictionary[InEventID] + listener;
  60.         }
  61.  
  62.     }
  63.     //call this method to add event without input
  64.     public static void StartListening<T>(EventsID InEventID, Callback<T> listener)
  65.     {
  66.         //check if ther is dictionary with correct key
  67.         if (!eventsDictionary.ContainsKey(InEventID))
  68.         {
  69.             //if there isn't add event
  70.             eventsDictionary.Add(InEventID, listener);
  71.         }
  72.  
  73.         else
  74.         {
  75.             //take delegate, cast it ass callbakc and add callback (use this to check if signature as equal)
  76.             eventsDictionary[InEventID] = (Callback<T>)eventsDictionary[InEventID] + listener;
  77.         }
  78.     }
  79.  
  80.     //call this method to remove event without input
  81.     public static void StopListening(EventsID InEventID, Callback listener)
  82.     {
  83.         //if dictionary containt key
  84.         if (eventsDictionary.ContainsKey(InEventID))
  85.         {
  86.             //take delegate, cast it ass callbakc and remove callback (use this to check if signature as equal)
  87.             eventsDictionary[InEventID] = (Callback)eventsDictionary[InEventID] - listener;
  88.  
  89.             //ask alessia
  90.             eventsDictionary.Remove(InEventID);
  91.         }
  92.     }
  93.  
  94.     //call this method to remove event with one input input
  95.     public static void StopListening<T>(EventsID InEventID, Callback<T> listener)
  96.     {
  97.         //if dictionary containt key
  98.         if (eventsDictionary.ContainsKey(InEventID))
  99.         {
  100.             //take delegate, cast it ass callbakc and remove callback (use this to check if signature as equal)
  101.             eventsDictionary[InEventID] = (Callback<T>)eventsDictionary[InEventID] - listener;
  102.             //ask alessia
  103.             eventsDictionary.Remove(InEventID);
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement