Author Topic: Prevent Click Through  (Read 4811 times)

jaised

  • Guest
Prevent Click Through
« on: August 14, 2012, 03:17:18 PM »
Hello Community!

I have 2 cameras set up. 1 is used for UI and the other is used for viewing the scene. When viewing the scene, the player uses a drag motion to look around. However, when I have the UI up, I want to block the input if it's through the UI to not affect the player controller camera. I have tried the:

  1. if(UICamera.lastHit.transform.gameObject.layer != 0) // default layer
  2.                         return;


approach in the player camera's drag function : http://www.tasharen.com/?topic=prevent-click-through

I haven't got it to work correctly. My GUI is on a GUI layer and the scene elements are on the default layer. After debugging statements, I am getting objects with the default layer even though I am explicitly over GUI layered elements. Any help would be greatly appreciated. Thank you.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,152
  • Toronto, Canada
    • View Profile
Re: Prevent Click Through
« Reply #1 on: August 14, 2012, 03:21:11 PM »
You can either turn off/remove UICamera on your game camera, or set it to not handle the mouse events.

leathers

  • Guest
Re: Prevent Click Through
« Reply #2 on: October 09, 2013, 02:19:43 PM »
Hello. Yes this seems like very basic stuff we would need to know to work with NGUI and there should be some info out there on how to do this easily. I am sorry I don't have an answer for you but I definitely am struggling with the exact same issue.

BIS

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Prevent Click Through
« Reply #3 on: November 03, 2014, 03:47:54 AM »
Hello everyone !
Recently encountered with the similar problem, two cameras (main camera and camera for UI) and UI is transparent for all mouse events.
Here is my solution:

Basic Ideas
1. Use static field with name hoveredObject from class UICamera.
When this field is not null it means that cursor is located over some NGUI object (NGUI object should have a collider of course), so check it and you can block all input processing in you scripts.
This approach works well for stand alone or web applications, but unfortunately when I  used it on mobile device with touch screen first touch never can be blocked. Another disadvantage of this method, you should add a special script for every object on you main scene to block the touches for this object over NGUI elements.
 
2. To solve issue for touch screens can be used other method: it is possible to write a special script attached to the NGUI object  that adds collider right front of the Main Camera scaled and positioned in space  by such way that this collider always covers NGUI object (actually rendered by the second NGUI camera).

Here is the demo with implementation of this ideas:
https://sites.google.com/site/bis153site/home/files/NGUIInputBlockingDemo_2014.11.03.zip

1. Scripts
Scripts/GUI/NGUIScriptBlocker.cs - implementetion of the first idea, scripts checks state of the field UICamera.hoveredObject and when it is not null disables all other scripts attached to the object.

Scripts/GUI/NGUIRayCastingBlocker.cs  - implementation of the second idea, script should be added to NGUI object, names for main and NGUI cameras can be set up in this script as parameters but when its empty scripts tries to find cameras automatically.

2. Demo description and instructions.
There are 2 scenes:
Scenes/Scene1 - base 3D scene
Scenes/Scene_GUI2 - 2D GUI

Open and run Scene1,  Scene_GUI2 will be downloaded and added by the MainProgram script.
There are color bars in the center, if you click on them then material becomes transparent. You can press left button of the mouse and orbit around the scene.
Scrip NGUIScriptBlocker.cs is attached to the Main Camera, in result when you try to press mouse button over the NGUI panel script CameraOrbitAndPinch.cs will be disabled and you can not orbit camera.
Script  NGUIRayCastingBlocker.cs is attached to the objects Sphere (orange dragable sphere in the left bottom cornert) and PanelBG (background for the right NGUI panel) on scene  Scene_GUI2.
Script adds  colliers visible for the Main Camera in Scene1 and this colliders always located in the same position (from the main camera point o view) as origibnal NGUI objects, so if you click on bar located under the right panel then this click will be blocked. Inspect objects with name "NGUIRayCastingBlocker for ..." to check this idea.

Demo works with free NGUI 2.7, if you are going to use this scripts it with NGUI 3.x then uncomment the first line in the scrip NGUIRayCastingBlocker.cs:
//#define NGUI_v3
« Last Edit: November 03, 2014, 04:00:13 AM by BIS »

lunadaj

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Prevent Click Through
« Reply #4 on: March 16, 2018, 03:07:43 AM »
Ello everyone,

I had the same issue. Im using 2Dtoolkit camera for game scene and UIRoot camera for UI. Tired BIS's solution but didnt work for some reasons.
But there's this UICamera.hoveredObject in his code and led me to this solution.

UICamera.interactingWithUI checks if user is interacting with UI, so I put this in LateUpdate like so.

void LateUpdate()
   {
      if(!UICamera.interactingWithUI){
         HandleTouch();
         HandleMouse();
         resetCamera();
      }
      
   }

Hope it helps. :D


Bartec

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: Prevent Click Through
« Reply #5 on: May 22, 2018, 04:46:50 AM »
Hi,

how to Block Raycast, when over UI element.


regards
« Last Edit: May 26, 2018, 09:28:37 AM by Bartec »

cabanel3

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
  • cab
    • View Profile
Re: Prevent Click Through
« Reply #6 on: June 05, 2018, 04:54:23 AM »
.