Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Elenesski · Aug 03, 2015 at 02:48 PM · eventsdelegate

Can I use a delegate/event in Unity3D?

In Unity I see this type of code a lot for wiring up code for events:

         Button myButton;
         myButton.onClick.AddListener(delegate( string aText ) {
             // Some Action
         });

Where we add a listener to an event. Coming from the C# world to Unity, I am used to writing code with delegates and events like this:

         public delegate void ValueChanged(string aText);

         public class ButtonRoot : MonoBehaviour {
             public event ValueChanged onValueChanged;
         }

         ButtonRoot DeckButton = new ButtonRoot;
         DeckButton.onValueChanged += delegate( string aText ) {
             // Some Action
         };

Is there some reason why I should not use the Event/AddListener vs. the C# event/delegate mechanism?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by stepan-stulov · Aug 03, 2015 at 04:24 PM

There are two main from-the-box mechanisms of subscribing to something in Unity:

  1. Traditional C# event delegates. These are non-persistent within the Unity's serialisation system. At least not from the box. These delegates are fast. I would personally use these delegates only in programmer-to-programmer cases.

  2. Unity Events. These are persistent within the Unity's serialisation system and exposed in the editor/inspector. These are not really serialised delegates, but serialised observers. Also note that these unity events support both persistent callbacks (specified only through the inspector) and non-persistent callbacks (specified through AddListener/RemoveListener). There might be a way to add persistent callbacks from code two, but that's not the AddListener method, something else, maybe even UnityEditor-only. People very often associate these events with Unity's new UI system while the truth is — they aren't. They are completely UI agnostic and are on their own. Unity is to blame for very poor naming here (EventTrigger, EventSystem, etc.). These events are much slower. I would only use these delegates to expose event subscription to non-programming folks to enable visual logics/flow authoring. Like linking some sounds on when some monster kicked some other monster.

It just coincides so, that Unity uses Unity Events in their own new UI system. So it's not like you have choice, you have to play by their rules within the UI code. Apart from the UI, as I've already stated, I'd recommend to keep traditional delegates for code-only things, and persistent/serialised delegates for enabling visual logics/flow authoring. In fact I find it cool to have two different mechanisms of event subscription, one for programmers, and one for designers.

Apart from these two mechanisms there are tons of third party solutions of different grade of quality in the Asset Store and on the Internets.

But to your question: the reason you should favour one to another depends on what you're trying to implement.

Comment
Add comment · Show 4 · 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
avatar image Elenesski · Aug 03, 2015 at 11:42 PM 0
Share

That's what I was looking for. Thanks.

avatar image fafase · Aug 04, 2015 at 08:53 AM 0
Share

The second case is only persistent if you use the editor for assigning the event. Doing the case shown in the OP does not show the event in the Inspector.

avatar image stepan-stulov · Aug 04, 2015 at 11:23 AM 0
Share

Here is some API for adding persistent listeners to events through code: http://docs.unity3d.com/ScriptReference/Events.UnityEventTools.AddPersistentListener.html

In this case actually delegates are being added though, unless I misunderstood the UnityAction. Also this class doesn't seem to be editor-only.

I haven't tried this, so can't say much.

Update: Nope, this class is indeed in the UnityEditor namespace.

avatar image Elenesski · Aug 04, 2015 at 03:10 PM 0
Share

I wire up my events on Awake(). I prefer delegate/event as it's much more flexible for inter-object communication. I know what it's doing and I have no surprises for how events are handled.

avatar image
0

Answer by Mykhaylo-Hnatyuk · Aug 03, 2015 at 03:03 PM

sure you can https://unity3d.com/ru/learn/tutorials/modules/intermediate/scripting/events

Comment
Add comment · Show 1 · 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
avatar image Elenesski · Aug 03, 2015 at 03:08 PM 0
Share

I know I can ... I have code that does this right now. The question is; is there some reason why I should use delegate/event over the more traditional AddListener mechanism? Such as encountering issue on different platforms, performance considerations, etc.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Send/Broadcast message vs Delegates and Events 3 Answers

Help with multi menu closing using bool 1 Answer

Can't include System.Threading.Tasks 3 Answers

Chartboost delegate not working on 2nd scene load 0 Answers

Events/delegates not working on Android (working in Unity player though) 2 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