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 Nexus2911 · Sep 12, 2014 at 11:17 PM · unity 4.6

Unity 4.6 add OnPointerDown Listener to Button

I can add an onClick listener to one of my buttons using:

 JumpButton.onClick.AddListener(() => {Jump(); });

I want do the same with another button only I want the event to be a an OnPointer or OnMouse down event. I tried the following:

 RotateButton.OnPointerDown.AddListener(() => {RotateVehicle(); });

But this yields error:

 Expression denotes a 'method group', where a 'variable', 'value or 'type was expected


Thank you in advance.

Comment
Add comment · Show 1
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 rutter · Sep 12, 2014 at 11:24 PM 0
Share

Assu$$anonymous$$g that RotateButton has type UI.Selectable, OnPointerDown is a function. I haven't messed around with the new GUI, yet, but that would apparently explain the error.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by orb · Sep 12, 2014 at 11:28 PM

There's no AddListener() method on OnPointerDown(), so you'll have to implement it the old-fashioned way, by implementing the right interfaces:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class BT : MonoBehaviour, IPointerDownHandler
 {
     public void OnPointerDown(PointerEventData data)
     {
     }
 }
 

etc.

Comment
Add comment · Show 14 · 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 Nexus2911 · Sep 13, 2014 at 01:04 AM 0
Share

It seems really odd that there is not a listener for pointer down.

And I apologize, I should clarify.

The function that rotates the vehicle is on a script attached to an instantiated prefab, therefore, I was trying to use the workflow to one of my other questions as shown here: http://answers.unity3d.com/questions/788493/unity-46-access-button-by-script.html

How would I implement your solution within that workflow?

avatar image orb · Sep 13, 2014 at 10:06 AM 0
Share

I'd use a helper script that implements the interfaces, and calls a delegate, so you add the listener to the helper component ins$$anonymous$$d of directly on the buttons. I'd also go look for a request for this functionality in Feedback and vote it up :)

avatar image Nexus2911 · Sep 13, 2014 at 03:55 PM 0
Share

Orb, thank you for your response.

As you've might have guessed, I'm not a seasoned programmer ;).

Can you provide a basic test case of your solution? I don't expect you to completely write the code for me but I could use a little more guidance.

avatar image BMayne · Sep 13, 2014 at 04:33 PM 0
Share

If you wonder why there is no "listener" for input it's because Unity uses Reflection to find all classes that have an Interface of the type. If you don't know what reflection is there is about million things about it on $$anonymous$$SDN. It pretty much comes down to code that can look at class and get a list of details about that class. It then has the power to call the functions it wants. (What I just said is super incredibly dumbed down version of what it truly does)

avatar image Nexus2911 · Sep 13, 2014 at 04:50 PM 0
Share

I'm looking into this https://github.com/AyARL/UnityGUIExamples

I can't believe there isn't a simpler way to go about this. Anyway, I used TriggerSetup.cs from the above link and attached it to my Rotate button.

I can't show the entire script. I know its not very convenient but you'll probably need to view the entire TriggerSetup.cs script from the above link.

As you'll see below, In TriggerSetup.cs, I referenced the instantiated prefab's NVehicleController script. I placed my controller.RotateVehicle() call within the OnPointerDown() function.

Now the Rotate button does indeed trigger the RotateVehicle() function within NVehicleController using onPointerDown, however, while it shows the debug message within that function, my vehicle isn't rotating so I'm thinking that the RotateFunction() is getting canceled right away because of the error:

 Type IPointerEnterHandler expected EventTrigger received.  i:1. Object reference not set to an instance of an object 

Partial TriggerSetup.cs:

      void Start()
         {
     
             // =====  $$anonymous$$y Addition ====
             GameObject vehicle = GameObject.FindGameObjectWithTag("Vehicle"); // This is an instantiated prefab
             controller = vehicle.GetComponent<NVehicleController>();
             // =====  END $$anonymous$$y Addition ====
     
     
             eventTrigger = gameObject.GetComponent<EventTrigger>();
     
             AddEventTrigger(OnPointerClick, EventTriggerType.PointerClick);
             AddEventTrigger(OnPointerEnter, EventTriggerType.PointerEnter, toggle);
             AddEventTrigger(OnPointerExit, EventTriggerType.PointerExit);
             AddEventTrigger(OnPointerDown, EventTriggerType.PointerDown);
             AddEventTrigger(OnPointerUp, EventTriggerType.PointerUp);
             
         
         }

  // Use listener with no parameters
     private void AddEventTrigger(UnityAction action, EventTriggerType triggerType)
     {
         // Create a nee TriggerEvent and add a listener
         EventTrigger.TriggerEvent trigger = new EventTrigger.TriggerEvent();
         trigger.AddListener((eventData) => action()); // ignore event data
 
         // Create and initialise EventTrigger.Entry using the created TriggerEvent
         EventTrigger.Entry entry = new EventTrigger.Entry() { callback = trigger, eventID = triggerType };
 
         // Add the EventTrigger.Entry to delegates list on the EventTrigger
         eventTrigger.delegates.Add(entry);
     }
 


  private void OnPointerDown()
     {
         textField.text = "OnPointerDown";
 
         // $$anonymous$$y Addition
         controller.RotateVehicle();
     }
  
Show more comments

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

6 People are following this question.

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

Related Questions

info.plist edit example for iOS device orientation? 1 Answer

Using acceleratorometer, manipulate the data to trick phone into thinking gravity is pointing in a different direction than down. 1 Answer

U4.6 - How to use individual sprites from an atlas in script? 1 Answer

Canvas Button OnClick() function arguments 1 Answer

How to integrate banner and interstitial ads in windows phone 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