Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
2
Question by taiku · Jul 19, 2015 at 03:28 AM · uicanvastouchignore

Detect canvas object under mouse because only some canvases should block mouse.

Here is what I am trying to do: I have say 6 canvases, where 1 is an overlay HUD, but the remaining canvases are informational health bar type canvases. I want to prevent mouse actions when mouse (or touch) is over the HUD but not over the other canvases.

So this question doesn't cover it because it references EventSystem.current.IsPointerOverGameObject which only tells me it's over some canvas object. But I need to know if its over a specific object.

What I'd like to do is get a reference to the canvas object under the mouse, like UEventSystem.current.GameObjectUnderPointer and then use compare tag.

I can't seem to do it with a raycast, presumabely because I can't get the canvas camera, but even then I sometimes have world canvases I will want to prevent touches on.

I am not sure how to proceed and am looking for advice. Thanks!

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 getyour411 · Jul 19, 2015 at 05:19 AM 0
Share

I can't tell whether this will help or not, but just in case...

http://docs.unity3d.com/$$anonymous$$anual/class-CanvasGroup.html

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by taiku · Jul 19, 2015 at 05:08 PM

So I figured this out myself, with one catch. There is a method in the EventSystem called RaycastAll which does exactly what I was looking for. It casts a raycast into the event system and returns canvas objects in the raycast (code below).

This works to get all the canvas elements under mouse, but when I enable my canvas background image (the one that has the ignore tag) it intercepts events from all the other canvas elements. I can't add the canvas group and disable "blocks raycasts" or it kills my raycast code below. So to solve this I simply created 2 canvases and moved the second one behind the first one and am putting all of my background images in that one. But this ruins my hierarchy and a simple priority or respecting the items z-position would solve this (in Unity's internal event system). So this is annoying. (Edit: I realized you can put the image higher in the hierarchy and then children receive events first, but this is still an annoying way to do it, hierarchy should be for organization IMO and a simply priority would be easy to add).

Event System Raycast Code:

 public List<RaycastResult> RaycastMouse(){
         
         PointerEventData pointerData = new PointerEventData (EventSystem.current)
         {
             pointerId = -1,
         };
         
         pointerData.position = Input.mousePosition;
 
         List<RaycastResult> results = new List<RaycastResult>();
         EventSystem.current.RaycastAll(pointerData, results);
         
         Debug.Log( results.Count);
         
         return results;
     }
Comment
Add comment · Show 3 · 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 PixxlMan · May 21, 2019 at 08:28 PM 0
Share

Thank you! This is awesome!

avatar image Yuvix25 · Apr 20, 2020 at 09:49 AM 0
Share

Finally, its the only solution I found, THANK YOU

avatar image B33bo_Astro · Nov 01, 2020 at 03:49 PM 0
Share

THANK YOU SO $$anonymous$$UCH

avatar image
1

Answer by mconradie · Jul 19, 2015 at 11:08 AM

Try using a UI.Graphic.Raycast

UI.Graphic.Raycast

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

26 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

Related Questions

I can't use more that two buttons at the time. How do I change that? 0 Answers

Touch to Ray on Canvas 1 Answer

How to get PointerEventDatas[]? 0 Answers

My Canvas only accept 2 touchs at same time 0 Answers

Check UI panel touched after OnEnable()? 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