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
0
Question by rejwan1 · Jan 27, 2015 at 03:27 PM · uieventsystem

How to add callback for button press to a private function (New UI)?

I am aware that in the new UI you can only add callbacks for public functions for buttons, however I was wondering if it's possible to flag my function in some attribute that will allow me to hook it up in the editor (without hooking it manually in the code)?

Thanks!

Comment
Add comment · Show 3
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 CodeElemental · Jan 27, 2015 at 03:48 PM 1
Share

It is not possible to call a private function outside it's scope (in this case class) . It might be possible if you use reflection (I've never done this, the private methods are private for a reason) but I wouldn't recommend it. Also, $$anonymous$$ake a public "wrapper" method that calls the private function inside if you want a compromise and a quick solution.

avatar image rejwan1 · Jan 27, 2015 at 04:30 PM 0
Share

@CodinRonin - that is incorrect, Unity does this a lot with reflection (Send$$anonymous$$essage for example) and I've seen it happen in a lot of places (ie. old animation system) - I am wondering if it's possible to use private functions in the new UI Event System

avatar image fafase · Jan 27, 2015 at 06:25 PM 0
Share

If you want to use a private method outside of its class then you have a design problem as it should be public.

I doubt you saw some private members being used outside of their class in Unity since, again, that would be bad design and I doubt Unity hires bad design programmers.

What you may have seen is internal variables which makes them available within the UnityEngine namespace but not available from your scripts.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by iamvishnusankar · Jan 27, 2015 at 07:02 PM

Here's the solution for you.

  1. Create a custom EventManager class to handle your button click events.

    //Custom Event manager class

       public class MyEventManager : MonoBehaviour 
         
             {
                 public delegate void ClickedAction();
         
                 public static event ClickedAction onCliked;
         
             
                 public void _ButtonClick()
                 {
                     onCliked ();
                 }
             }
     
    
    
  2. Subscribe to the clicked event from any other script and you can execute private function. Make sure you've properly subscribed and unsubscribed from custom event using OnEnbale() and OnDisable() to avoid multiple instances.

    //Test script 1

       public class Test1 : MonoBehaviour 
      {
         
         //Subscribe to event onEnable
             void OnEnable()
             {
                 MyEventManager.onCliked += MyTest1;
             }
         
             void MyTest1 ()
             {
                 Debug.Log ("Test 1 called");
             }
         
         //Unsubscribe to event onDisable
             void OnDiable()
             {
                 MyEventManager.onCliked -= MyTest1;
             }
         }
     
    
    
    

       //Test script 2
     
         public class Test2 : MonoBehaviour 
      {
         
         //Subscribe to event onEnable
             void OnEnable()
             {
                 MyEventManager.onCliked += MyTest2;
             }
         
             void MyTest2 ()
             {
                 Debug.Log ("Test 2 called");
             }
         
         //Unsubscribe to event onDisable
             void OnDiable()
             {
                 MyEventManager.onCliked -= MyTest2;
             }
         }
    
    
    
    
    

Hope this help! Cheers :)

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

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

21 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

Related Questions

How to listen for UI Resize Events 3 Answers

Unity UI: How to stop select event from propagating? 1 Answer

Catch pointer events by multiple gameObjects 6 Answers

New Unity UI not working with windows standalone builds 0 Answers

Is it wise to add scripts to Unity's Event System? 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