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
2
Question by patrik-org · Nov 09, 2014 at 11:31 PM · uiraycasteventsystemunity4.6

Unity 4.6 UI - Ignore raycast

Is there way to tell the UI event system to ignore a UI gameObject when doing raycasts? I have transparent panels that I want the clicks to go through.

 Canvas
  |- Panel1
      |- Button1
  |- Panel2
      |- Button2
 
 +-------------------+
 |       +-----------------------+
 |       |           |           |
 |       | [Button1] |           |
 +-------|-----------+ [Button2] |
         +-----------------------+

Panel2 covers Panel1 but I want the clicks to go through so button1 may be pressed.

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
13
Best Answer

Answer by troien · Nov 10, 2014 at 10:51 AM

Yes, add a Canvas Group component and uncheck the 'Blocks Raycasts' checkbox. (You'll probably want to unckeck the 'Interactable' checkbox aswell)

ps. You can use this Component aswell to do your fading using the 'Alpha' slider.

Comment
Add comment · Show 6 · 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 patrik-org · Nov 10, 2014 at 01:51 PM 0
Share

It almost worked. I've updated my question to illustrate the problem. If I uncheck "Blocks Raycasts" on Panel2 I can no longer click on Button2 as it seems to affect all the children as well. I've tried adding a CanvasGroup to Button2 as well and tried different settings but nothing works.

avatar image patrik-org · Nov 10, 2014 at 01:56 PM 0
Share

I might have found a working solution. If I disable the Image-component from Panel2 the clicks seems to go through.

avatar image troien · Nov 10, 2014 at 03:29 PM 1
Share

Hmmm... This looks like a bug of the 'Ignore Parent Groups' chekbox. Because it doesn't ignore the parent group (Only the value of Alpha) if you check it...

I did find a kind of hacky way to work around this, which is implementing your own ICanvasRaycastFilter.

This piece of code can be added to your panels (ins$$anonymous$$d of the Canvas Group)

 using UnityEngine;
 
 public class CustomRaycastFilter : $$anonymous$$onoBehaviour, ICanvasRaycastFilter
 {
     public Raycast$$anonymous$$ode mode;
 
     public enum Raycast$$anonymous$$ode
     {
         ReceiveNone,        // You can't interact with us or any of our children
         ReceiveAll,         // You can interact with us and all of our children
         OnlyChildsReceive   // You can't interact with us, but you can with any of our children
     }
 
     public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
     {
         switch (mode)
         {
             case Raycast$$anonymous$$ode.ReceiveNone:
                 return false;
             case Raycast$$anonymous$$ode.ReceiveAll:
 
                 RectTransform rt = transform as RectTransform;
                 Vector2 localPos = rt.worldToLocal$$anonymous$$atrix.$$anonymous$$ultiplyPoint(sp);
                 return rt.rect.Contains(localPos);
             case Raycast$$anonymous$$ode.OnlyChildsReceive:
                 for (int i = 0; i < transform.childCount; i++)
                 {
                     RectTransform childRect = transform.GetChild(i) as RectTransform;
                     Vector2 childPos = childRect.worldToLocal$$anonymous$$atrix.$$anonymous$$ultiplyPoint(sp);
                     if (childRect.rect.Contains(childPos))
                     {
                         return true;
                     }
                 }
                 return false;
             default:
                 throw new System.NotImplementedException("$$anonymous$$ode not implemented");
         }
     }
 }


Though a better solution might be to simply change your hierarchy to something like this:

 Canvas
   |- Container1
     |- Panel1
     |- Button1
   |- Container2
     |- Panel2
     |- Button2

$$anonymous$$eeping the Canvas Group component on the panel. The Containers are empty GameObjects with no Components (Other than RectTransform). Their size is equal to the old panels, and the new panels size simply fill the containers size.

avatar image patrik-org · Nov 10, 2014 at 07:31 PM 0
Share

Awesome thank you.

avatar image benni05 · Jan 28, 2015 at 12:40 PM 0
Share

Yes, it was a bug, should be fixed in 4.6.2

http://issuetracker.unity3d.com/issues/ignore-parent-groups-doesnt-ignore-parent-canvas-group-component

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

28 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

Related Questions

Unity UI - Select with Raycast (Screenspace - Camera) 0 Answers

Holding down UI Button and shooting raycast from touch position doesn't work simultaneously:(( 0 Answers

understand how "Raycast Target" works on UI elements 2 Answers

How do you perform a Graphic Raycast? 3 Answers

Help, Use event system IPointerClickHandler 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