Author Topic: Click pass through sprite  (Read 13468 times)

bloomk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 38
    • View Profile
Click pass through sprite
« on: May 08, 2013, 05:52:51 PM »
I have a sprite that is a window background.. the items behind the window are still clickable. How do i stop the clicks from passing thru the sprite? I tried adding a Box Collider but it didn't work.. thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Click pass through sprite
« Reply #1 on: May 08, 2013, 06:09:57 PM »
Use NGUI events instead of Input. if you use Input functions, then it's up to you to check for UICamera.hoveredObject. Input events can't be canceled.

bloomk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 38
    • View Profile
Re: Click pass through sprite
« Reply #2 on: May 11, 2013, 02:02:37 PM »
Im new to NGUI.. i don't know what your talking about... the entire project i am having issues with is ngui so far.. so. can you explain what the solution is? :)

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Click pass through sprite
« Reply #3 on: May 12, 2013, 09:31:02 AM »
Look how the default NGUI elements catch clicks. It's done with the OnClick, OnPress etc methods. These are sent from UICamera to all colliders in the correct unity layer.

If you're using Input.(...) then you're going around the NGUI event system and instead using "raw" touches, which has some different rules going on for it.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Click pass through sprite
« Reply #4 on: May 12, 2013, 01:02:33 PM »
if (Input.GetMouseButtonDown(0)) <-- Unity way of checking for mouse button being pressed.

void OnPress (bool isPressed) {} <-- NGUI way of checking if a collider was pressed on with either a mouse or a touch.

As Nicki mentioned, look at how NGUI events work.

bloomk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 38
    • View Profile
Re: Click pass through sprite
« Reply #5 on: May 13, 2013, 10:55:09 AM »
i'm not trying to get the event.. i am trying to block it so that items behind my panel or sprite can not be clicked (stop the click from passing through the sprite)..

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Click pass through sprite
« Reply #6 on: May 13, 2013, 10:58:34 AM »
We've already answered your question.

bloomk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 38
    • View Profile
Re: Click pass through sprite
« Reply #7 on: May 15, 2013, 10:16:20 AM »
Actually, you didn't i figured it out.. it was an issue with the Z local position.. For some reason the panel was visually in front of the button but the panel was not getting the click event. I moved the Z position further forward and it fixed the issue..

leathers

  • Guest
Re: Click pass through sprite
« Reply #8 on: October 11, 2013, 02:13:51 PM »
Hello. I am having the same problem but can't seem to fix it by moving things in Z space..

I have NGUI buttons set up and the have a generic NGUI canned playAnimation.cs script on them. I think them and they work..

But if the button happens to be on top of another interactive element, in the 3D scene, which is drawn by a different camera, then that item is activated too..

It seems like there should be a check box on the button script, that says, IGNORE ANYTHING THAT RENDERS BEHIND THIS BUTTON.. I hear what you are saying about input -vs- events.. but have no idea how to put that to use to achieve what I describe above..

This has GOT to be a common question.. There has got to be a standard answer.. Any help would be much appreciated..

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Click pass through sprite
« Reply #9 on: October 11, 2013, 06:01:50 PM »
So, since the system just changed, I'm using some tricks to get it to play along.

If you choose UI as the event type on the UICamera, then the z position has no effect - sadly, this has the side effect that only widgets or colliders that have widgets in its sub hierarchy are sorted according to the depth, which is what the new UI type events are suppsoed to do.

If you want to do something like a big collider blocking the screen behind a popup, for instance, then you can't just have a collider on a game object anymore then. NOw you have to put a sprite on it, to make it be sortable so that you can hit that above the colliders behind it.

If you want the old way, where the touch hits the first collider it hits, then you can change it to World event type. That's how it used to be.

So, the workaround is like so:

If you want an invisible collider to stop touches to everything below something at a given depth, you have to add a collider in a relevant place and add a sprite to it to be able to set the depth and have it sorted. You can set the sprite to alpha=0 and it won't cost you draw calls - but it has to be there, or there has to be some widget under it in the hierarchy.

I don't much like that workaround, but that's what you gotta do if you want to use the UI events right now.