Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by IgnoranceIsBliss · Mar 02, 2016 at 02:22 AM · androidunity 5guibuttons

UI Button OnClick Sensitivity for High DPI Devices

I've got a Unity project running on a Samsung Galaxy S5, and users are having issues pressing buttons.

They need to repeatedly click on any button using the OnClick handler. I believe this is because of very small movements in their finger when they touch the device reading as scrolling rather than a simple touch. Adding an OnPointerDown event trigger of course avoids the issue...

Unfortunately, some of my buttons are on items that take up large chunks of a ScrollRect. This means that I can't easily switch to using EventTriggers to simply capture OnPointerDown - that would unnecessarily trigger the button when the person is actually scroll-dragging.

Is there any way I can change the 'scroll' sensitivity within the Button generally? I'd like to adjust it to suit Screen.dpi, since most devices with a higher resolution screen have an accompanying increase in touch resolution.

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

5 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by FireOApache · Mar 06, 2016 at 11:35 AM

Looking at Unity's UI implementation (on tag 5.3) I've found that (1) is called by (2) which uses pixelDragThreshold attribute defined by the current EventSystem instance in the scene...

A possible (and maybe the simplest) solution would be to use a script which will change that threshould based on the screen PPI (Screen.dpi) of each device.

 1. private static bool ShouldStartDrag(...) @ PointerInputModule.cs : 228
 2. protected virtual void ProcessDrag(...) @ PointerInputModule.cs : 248

The following script does the drag threshold adaptation, put this in your event system game object:

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class DragCorrector : MonoBehaviour
 {
     public int baseTH = 6;
     public int basePPI = 210;
     public int dragTH = 0;
 
     void Start()
     {
         dragTH = baseTH * (int)Screen.dpi / basePPI;
 
         EventSystem es = GetComponent<EventSystem>();
 
         if (es) es.pixelDragThreshold = dragTH;
     }
 }

Here's a very simple script to check the device's PPI... you can run this on different phones to see if Unity is calculating it right:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class PPIViewer : MonoBehaviour
 {
     public Text label;
 
     void Start()
     {
         if (label != null)
         {
             label.text = "PPI: " + Screen.dpi.ToString();
         }
     }
 }
Comment
Add comment · Show 2 · 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 hema_dubal · Aug 29, 2017 at 06:05 AM 0
Share

Perfect! Thanks !!

avatar image namadeveloper1 · Aug 27, 2018 at 08:19 AM 0
Share

So many very super useful

avatar image
1

Answer by Roixo · Jan 12, 2018 at 06:33 PM

This code is more efficient for me: link text

 public class DragThresholdUtil : MonoBehaviour {
   void Start () {
     int defaultValue = EventSystem.current.pixelDragThreshold;        
     EventSystem.current.pixelDragThreshold = 
             Mathf.Max(
                  defaultValue , 
                  (int) (defaultValue * Screen.dpi / 160f));
   }
 }


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
avatar image
0

Answer by SimonDarksideJ · Jun 03, 2016 at 12:14 PM

Nice little fix. Would you be OK with me adding this to the UI extensions project? Should prove valuable.

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 FireOApache · Jun 03, 2016 at 12:23 PM 0
Share

Sure, please go ahead! Could you credit me on that somehow?

avatar image SimonDarksideJ FireOApache · Jun 03, 2016 at 02:41 PM 0
Share

Definitely, everyone gets credited for everything on the project :D https://bitbucket.org/ddreaper/unity-ui-extensions

And thanks!

avatar image SimonDarksideJ FireOApache · Jun 03, 2016 at 04:38 PM 1
Share

Done :D https://bitbucket.org/ddreaper/unity-ui-extensions/src/0ee363bb42a07588161310f6b354c3989b888ad1/Scripts/Utilities/DragCorrector.cs?at=develop_5.3

Just in time for the upco$$anonymous$$g release (which won't happen if I don't stop adding things :D)

avatar image FireOApache SimonDarksideJ · Jun 03, 2016 at 05:12 PM 0
Share

Awesome, thank you mate! ;-)

avatar image
0

Answer by BeshevGames · Feb 26, 2017 at 08:56 AM

Try to increase the Drag Treshold in the Event System to about 16 :) .

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
avatar image
0

Answer by josefvesely · Mar 04, 2018 at 11:32 AM

I came up to following blog post when I was solving this issue:

Unity UI Drag Threshold

It introduces script that update Drag Threshold according to DPI.

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

94 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 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 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 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

How can I stick buttons on my cube and the same time using script? 1 Answer

Dynamically adding (prefab) Buttons to a ScrollView 0 Answers

GUI button doesn't appear on Android 0 Answers

When I load Unity5 application on android phono. It is not working. ERROR : Unfortunatly(gameName) has stopped 1 Answer

How to hold AR object after detection?? 1 Answer


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