Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by grasmann · Jun 25, 2015 at 09:00 PM · unity 5buttonactionlistener

Unityaction vs Method

I'm still pretty new to certain subjects of unity, so I don't know if this is a stupid question. XD

I tried to find information on this subject but I couldn't find anything specific. Currently I'm learning to create dynamic menus. To handle the variable processes started by buttons I'm working with unityactions. However, there's something I don't understand.

When adding listeners to an onclick event for example it doesn't seem to make a difference if I'm passing a unityaction or just simply the name of a method.

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.Events;
 using System.Collections;
 
 public class test_unityaction : MonoBehaviour {
 
     private UnityAction action;
 
     void Awake() {
         action = new UnityAction (Func);
         gameObject.GetComponent<Button> ().onClick.AddListener (action);
         gameObject.GetComponent<Button> ().onClick.AddListener (Func);
     }
 
     void Func() {
         print ("Func");
     }
 
 }

This is a very basic and easy code example. Add it to a button to test it.

The function is launched twice.

So my question is what is the difference here? Why should I pass a unityaction if I can simply pass the method name? Passing the method directly instead of a unityaction makes the code much more simple. Is there a disadvantage for passing the method directly because it seems to work as intended?

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

1 Reply

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

Answer by Hellium · Jun 25, 2015 at 09:17 PM

I have never used UnityActions. I read in the doc that it is a delegate. Then, you could possibly do something like this (didn't tested)

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.Events;
 using System.Collections;
 
 public class test_unityaction : MonoBehaviour {
 
     private UnityAction action;
 
     void Awake() {
         action = new UnityAction (Func);
         action += Func1 ;
         action += Func2 ;
         gameObject.GetComponent<Button> ().onClick.AddListener (action);
     }
 
     void Func() {
         print ("Func");
     }
 
     void Func1() {
         print ("Func1");
     }
 
     void Func2() {
         print ("Func2");
     }

     void Func3() {
         print ("Func3");
     }
 }

Obviously, using UnityAction is clearer.


You could also modify the function called dynamically with something like this :

     void Update() {
         if (Input.GetKeyDown("space"))
         {
             gameObject.GetComponent<Button> ().onClick.RemoveListener( action ) ;
             action -= Func1 ;
             action += Func3 ;
             gameObject.GetComponent<Button> ().onClick.AddListener (action);
         }
     }

Then, your button won't call Func1 anymore but call Func3 instead when you click on your button.

EDIT : I have tested without removing and adding back again the action, it doesn't seem to work.

Everything I did could have been possible without the UnityAction. Just remove the function you don't want anymore and add the one you want.


By the way, since AddListener is wainting for a delegate without any parameter and a return type of void, you could have done this :

 gameObject.GetComponent<Button> ().onClick.AddListener ( delegate()
     {
         Debug.Log("Hello World !") ;
     }
 )

Or, with anonymous delegate :

 gameObject.GetComponent<Button> ().onClick.AddListener ( () =>
     {
         Debug.Log("Hello World !") ;
     }
 )
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 grasmann · Jun 26, 2015 at 01:37 AM 0
Share

After reading your answer I've done some more testing and indeed all this doesn't seem to make any difference. XD

The only advantage of using a unityaction object seems to be that I have a seperate object I can manage seperately and pass as a parameter for example.

Also the unityaction object has several functions like begininvoke and stuff, which might be useful for asynchronous processes.

However, I discovered that removing a listener method that is also assigned through an action is removed too.

For example this will result in no listeners being assigned at all:

 void Awake() {
         action = new UnityAction (Func);
         action += Funcb;
         gameObject.GetComponent<Button> ().onClick.AddListener (action);
         gameObject.GetComponent<Button> ().onClick.AddListener (Func);
         gameObject.GetComponent<Button> ().onClick.AddListener (Funcb);
         gameObject.GetComponent<Button> ().onClick.RemoveListener (Func);
         gameObject.GetComponent<Button> ().onClick.RemoveListener (Funcb);
     }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A Problem with button listener in Start() 0 Answers

Mysterious IndexOutOfRangeException 1 Answer

Change the material of a buttons in List and keep it by draging around the screen? 2 Answers

Button position scaling weird 1 Answer

How to declare an Action with return type other than void? 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