Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public enum EventsID
- {
- Reset,
- ChooseItem,
- PerformAction
- }
- public class EventManager
- {
- // Generic callback
- #region PublicDelegate
- public delegate void Callback();
- public delegate void Callback<T>(T arg1);
- #endregion
- //public delegate void Callback<T, U>(T arg1, U arg2);
- private static Dictionary<EventsID, Delegate> eventsDictionary = new Dictionary<EventsID, Delegate>();
- //take event id in input
- public static void TriggerEvent(EventsID InEventID)
- {
- //if there is event with id key
- if (eventsDictionary.ContainsKey(InEventID))
- {
- //invoke as CallBack without input
- (eventsDictionary[InEventID] as Callback).Invoke();
- }
- }
- //take event id and method input as input
- public static void TriggerEvent<T>(EventsID InEventID, T InVariable)
- {
- //if there is event with id key
- if (eventsDictionary.ContainsKey(InEventID))
- {
- //invoke as CallBack with input
- (eventsDictionary[InEventID] as Callback<T>).Invoke(InVariable);
- }
- }
- //call this method to add event without input
- public static void StartListening(EventsID InEventID, Callback listener)
- {
- //check if ther is dictionary with correct key
- if (!eventsDictionary.ContainsKey(InEventID))
- {
- //if there isn't add event
- eventsDictionary.Add(InEventID, listener);
- }
- else
- {
- //take delegate, cast it ass callbakc and add callback (use this to check if signature as equal)
- eventsDictionary[InEventID] = (Callback)eventsDictionary[InEventID] + listener;
- }
- }
- //call this method to add event without input
- public static void StartListening<T>(EventsID InEventID, Callback<T> listener)
- {
- //check if ther is dictionary with correct key
- if (!eventsDictionary.ContainsKey(InEventID))
- {
- //if there isn't add event
- eventsDictionary.Add(InEventID, listener);
- }
- else
- {
- //take delegate, cast it ass callbakc and add callback (use this to check if signature as equal)
- eventsDictionary[InEventID] = (Callback<T>)eventsDictionary[InEventID] + listener;
- }
- }
- //call this method to remove event without input
- public static void StopListening(EventsID InEventID, Callback listener)
- {
- //if dictionary containt key
- if (eventsDictionary.ContainsKey(InEventID))
- {
- //take delegate, cast it ass callbakc and remove callback (use this to check if signature as equal)
- eventsDictionary[InEventID] = (Callback)eventsDictionary[InEventID] - listener;
- //ask alessia
- eventsDictionary.Remove(InEventID);
- }
- }
- //call this method to remove event with one input input
- public static void StopListening<T>(EventsID InEventID, Callback<T> listener)
- {
- //if dictionary containt key
- if (eventsDictionary.ContainsKey(InEventID))
- {
- //take delegate, cast it ass callbakc and remove callback (use this to check if signature as equal)
- eventsDictionary[InEventID] = (Callback<T>)eventsDictionary[InEventID] - listener;
- //ask alessia
- eventsDictionary.Remove(InEventID);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement