• 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 seidensj · Mar 11, 2016 at 03:34 PM · uitoggleonclickoverride

Best way to override OnClick with OnPointerDown for Toggle?

I want to execute a toggle's OnValueChanged method on a OnPointerDown event instead of an OnClick event. Is there an elegant way to do this?

Right now I'm just using an EventTrigger component with a OnPointerDown event on the Toggle GameObject to execute the the method I want, but this gets gross quickly. For one OnClick is still called on the Toggle Component. Yes, I could turn interactable false on the Toggle component, but then this presents the wrong visual state, yada yada yada.

I really just want all the functionality of the toggle, I just want it to be triggered OnPointerDown INSTEAD of OnClick.

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
1

Answer by Lo0NuhtiK · Mar 11, 2016 at 04:29 PM

Make a class that inherits from Toggle and override its stuff...

 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class MyToggle : Toggle 
 {
      public override void OnPointerClick(PointerEventData eventData)
      {
          //do nothing... we don't want it do do anything OnClick
      }
 
      public override void OnPointerDown(PointerEventData eventData)
      {
           //do the stuff it usually does
           base.OnPointerDown(eventData);
           //do the stuff we want it to do
           base.OnSubmit(eventData);
      }     
 }

Then take the unity toggle component off, put this on, reassign the target graphic etc.

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 8e8618be-a4d5-486a-b998-e6d4a4eb8c8b_DELETE · Feb 16, 2020 at 10:39 AM 0
Share

What do you mean with: "Then take the unity toggle component off, put this on, reassign the target graphic etc.", please explain, I'm new to unity

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity 5.0.2f1 UI Button OnClick Function 6 Answers

How to make it so only a certain section of a button will work on click 1 Answer

UI button stops working after reload scene 1 Answer

Change Scroll bar on click 0 Answers

Unity Toggle Disabled Color Not Applying to Off State 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