• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Sep 11, 2018 at 01:07 AM by DrSharky for the following reason:

The question is answered, right answer was accepted

avatar image
Question by DrSharky · Sep 10, 2018 at 02:20 AM · eventsevent triggeringactiondelegates

EventManager with parameters

So I'm using the EventManager code from t$$anonymous$$s Unity tutorial, I figured it would help clean up my beginner project. Although I would like to be able to pass a parameter with UnityAction.

In the tutorial, every function that is added as a listener is one without any parameters.

I was hoping I could find a way to do t$$anonymous$$s without too much trouble, but I spent a w$$anonymous$$le researc$$anonymous$$ng it, and it seems like the most popular answer is to use delegates/Action instead... I tried modifying it myself, and I feel like I was close, but I'm lost at the moment.

I'd rather avoid that extra work of rewriting what I've done, so here I am.

For an example of what I'd like to do, I was hoping that I could do somet$$anonymous$$ng like t$$anonymous$$s in my code, it's invalid code, but it's basically what I'm trying to ac$$anonymous$$eve:

 void Awake()
 {
     someListener = new UnityAction (SomeFunction); //<---Can't do t$$anonymous$$s
 }
 
 void SomeFunction(GameObject someObject)  //<--- Because t$$anonymous$$s has a parameter
 {
     doSomet$$anonymous$$ng();
 }

Below you can see what I mean, where the original examples are.

The EventTest.cs:

 public class EventTest : MonoBehaviour
 {
     private UnityAction someListener;

     void Awake ()
     {
         someListener = new UnityAction (SomeFunction);  //<---T$$anonymous$$s works,
     }

     void OnEnable ()
     {
         EventManager.StartListening ("test", someListener);
     }

     void SomeFunction ()  //<---Because no parameter here!
     {
         Debug.Log ("Some Function was called!");
     }
 }

And they call the TriggerEvent in another class, so it's not directly referencing EventTest:

 public class EventTriggerTest : MonoBehaviour {

     void Update () {
         if (Input.GetKeyDown ("q"))
         {
             EventManager.TriggerEvent ("test");    //No need to reference EventTest! Hooray!
         }
 }

And here is the EventManager.cs:

 public class EventManager : MonoBehaviour {
 
     private Dictionary <string, UnityEvent> eventDictionary;
     private static EventManager eventManager;
 
     public static EventManager instance
     {
         get
         {
             if (!eventManager)
             {
                 eventManager = FindObjectOfType (typeof (EventManager)) as EventManager;
 
                 if (!eventManager)
                     Debug.LogError ("Need 1 active EventManger script on GameObject in your scene.");
                 else
                     eventManager.Init(); 
             }
             return eventManager;
         }
     }
 
     void Init ()
     {
         if (eventDictionary == null)
             eventDictionary = new Dictionary<string, UnityEvent>();
     }
 
     public static void StartListening (string eventName, UnityAction listener)
     {
         UnityEvent t$$anonymous$$sEvent = null;
         if (instance.eventDictionary.TryGetValue (eventName, out t$$anonymous$$sEvent))
             t$$anonymous$$sEvent.AddListener (listener);
         else
         {
             t$$anonymous$$sEvent = new UnityEvent ();
             t$$anonymous$$sEvent.AddListener (listener);
             instance.eventDictionary.Add (eventName, t$$anonymous$$sEvent);
         }
     }
 
     public static void StopListening (string eventName, UnityAction listener)
     {
         if (eventManager == null) return;
         UnityEvent t$$anonymous$$sEvent = null;
         if (instance.eventDictionary.TryGetValue (eventName, out t$$anonymous$$sEvent))
             t$$anonymous$$sEvent.RemoveListener (listener);
     }
 
     public static void TriggerEvent (string eventName)
     {
         UnityEvent t$$anonymous$$sEvent = null;
         if (instance.eventDictionary.TryGetValue (eventName, out t$$anonymous$$sEvent))
             t$$anonymous$$sEvent.Invoke ();
     }
 }
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by DrSharky · Sep 11, 2018 at 01:07 AM

I essentially found the answer to my question here, under the "With Parameter" section. Although it's using Action instead of UnityEvent, it's basically the same t$$anonymous$$ng: https://stackoverflow.com/questions/42034245/unity-eventmanager-with-delegate-instead-of-unityevent

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EventManager : MonoBehaviour
 {
 
     private Dictionary<string, Action<EventParam>> eventDictionary;
 
     private static EventManager eventManager;
 
     public static EventManager instance
     {
         get
         {
             if (!eventManager)
             {
                 eventManager = FindObjectOfType(typeof(EventManager)) as EventManager;
 
                 if (!eventManager)
                 {
                     Debug.LogError("There needs to be one active EventManger script on a GameObject in your scene.");
                 }
                 else
                 {
                     eventManager.Init();
                 }
             }
             return eventManager;
         }
     }
 
     void Init()
     {
         if (eventDictionary == null)
         {
             eventDictionary = new Dictionary<string, Action<EventParam>>();
         }
     }
 
     public static void StartListening(string eventName, Action<EventParam> listener)
     {
         Action<EventParam> t$$anonymous$$sEvent;
         if (instance.eventDictionary.TryGetValue(eventName, out t$$anonymous$$sEvent))
         {
             //Add more event to the existing one
             t$$anonymous$$sEvent += listener;
 
             //Update the Dictionary
             instance.eventDictionary[eventName] = t$$anonymous$$sEvent;
         }
         else
         {
             //Add event to the Dictionary for the first time
             t$$anonymous$$sEvent += listener;
             instance.eventDictionary.Add(eventName, t$$anonymous$$sEvent);
         }
     }
 
     public static void StopListening(string eventName, Action<EventParam> listener)
     {
         if (eventManager == null) return;
         Action<EventParam> t$$anonymous$$sEvent;
         if (instance.eventDictionary.TryGetValue(eventName, out t$$anonymous$$sEvent))
         {
             //Remove event from the existing one
             t$$anonymous$$sEvent -= listener;
 
             //Update the Dictionary
             instance.eventDictionary[eventName] = t$$anonymous$$sEvent;
         }
     }
 
     public static void TriggerEvent(string eventName, EventParam eventParam)
     {
         Action<EventParam> t$$anonymous$$sEvent = null;
         if (instance.eventDictionary.TryGetValue(eventName, out t$$anonymous$$sEvent))
         {
             t$$anonymous$$sEvent.Invoke(eventParam);
             // OR USE  instance.eventDictionary[eventName](eventParam);
         }
     }
 }

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

c# (Unity flavour) Action, return value, error thereof 1 Answer

How to unsubscribe from a delegate that is inside of a singleton? 2 Answers

Triggering events for overlaying UI Elements. 0 Answers

Question about subscribing to an event 1 Answer

Chartboost didCompleteRewardedVideo event not working 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges