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 /
  • Help Room /
avatar image
1
Question by alphaspec · Apr 02, 2017 at 09:58 AM · user interfacedetectmouseover

Ignore clicks on UI in script/detect when the mouse is over UI

So I have a game where your camera is centered on a planet. You can click and drag to orbit the camera around the planet using the code

     if (Input.GetMouseButton(0)){
             velocityX += xSpeed * Input.GetAxis ("Mouse X") * distance * 0.005f;
             velocityY += ySpeed * Input.GetAxis ("Mouse Y") * distance * 0.002f;
         }

I then have panels, buttons, and scrollviews, etc, around the edges of the screen that the user can interact with, show, or hide. The problem is if you click and drag a slider in the UI or something, the camera orbits because it is just following the clicks. How do I detect if a click was over ANY UI element from my camera script and ignore it? While the docs describe "IsPointerOverGameObject" as a way to detect if you are over a UI element in practice it returns true for any game object in the scene and is thus useless for this as at the very least you are clicking on the background stars object. OnPointerEnter/OnPointerEnter could be used to set a "isOverUI" variable to true on my camera script. However I would have to add a OnPointerEnter script to every UI element in my game, make sure they don't trigger if the UI element is hidden, and have them store references to my camera script which seems like overkill. Is there no "PointerOverUI" function I can just call in my camera script?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Cuttlas-U · Apr 06, 2017 at 08:00 PM

Hi Guys As u said "IsPointerOverGameObject" is the best way to solve this , I think u didn't use it well so u thought it can't solve your problem,

I had a top-down shooter game and the same problem, I did manage to solve that problem with : "IsPointerOverGameObject"

but its so complex to use and unity-manual didn't mention it well , if u use it wrong it will give u wrong answers and your code won't work,

there are 3 conditions :

if u are in Editor and using mouse for your input you should use this :

  if(EventSystem.current.IsPointerOverGameObject ( 0 )
     {
      doit = false;
     }
 

if u build the program for android it should like this , if not it wont work:

    if(EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
     {
      doit = false;
     }


if u are in Editor and using remote :

   if(EventSystem.current.IsPointerOverGameObject(-1))
     {
      doit = false;
     }
 

for android u should put these if statement only in "touch began" as said in unity manual"

   if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
     }
 

 

these number may have been changed in different positions since i have different version of unity but i should give u an idea about how hard this works on different Platforms; any way be sure that this will work, just need a little change .,.,

Cuttlas

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 Wappen · Aug 25, 2018 at 03:22 PM 0
Share

Nice. Helped me out, but if you wanr to test if mouse is over UI element you have to test

if (EventSystem.current.IsPointerOverGameObject ()) { print("Pointer over UI"); }

avatar image
-2

Answer by weaverbeaver · Apr 06, 2017 at 05:58 PM

I have the exact same problem and an answer would be nice.

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 lavarith · Feb 21, 2020 at 11:18 AM 1
Share

Statements like this should be comments, not answers.

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

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

Related Questions

Help with Detection of Mouse Over Irregular UI Elements 0 Answers

Raycast detect in object (problem) 0 Answers

How to determine class of object at mouse position 1 Answer

How to make or find advanced UI assets 1 Answer

UI Disappears after Ctrl+alt+del (and various other instances) [5.2.1f1] 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