GUI click through?

#NOTE for 2015…


Hi, in my game there is a panel in the corner that has buttons in it, but when I click the button it registers in the world, creating an object, can I stop this?

You're gonna want to take a look at the HitTest method.

You use it like this:

// C#
GUILayer l = Camera.main.GetComponent(typeof(GUILayer)) as GUILayer;
GUIElement ele = l.HitTest(Input.mousePosition);
if(ele != null)
{
     // The mouse is over a GUI element here. 
}
else
{
     // The mouse isn't over any GUI element here.
}

i made it through GUIUtility.hotControl

I added this to another thread and noticed this one… here is what I wrote there:

I just tried this trick and it seems to work great…

The Short Explanation:

Add a child object to your camera (box or plane). Add a collider to the object. Assign a material that is transparent, and a shader that supports transparent. Scale it far beyond what your camera would ever show. Shift it so it is just in front of the camera. As a child, it will now move with the camera. Deactivate it (uncheck in inspector). Now, when you show GUI stuff, call SetActive(true) on the object. When you are done with GUI stuff, call SetActive(false) on the object. Works like a charm on our system. We have two concurant cameras for objects and menu items. We do it with both cameras.

Detailed Steps (just coded from memory, but should work… C#):

  1. Create a new material in unity. Give it the colors of ARGB 1,1,1,1 in the color picker screen. (0…255 color types).

That makes it pretty much transparent. I leave a little just to make sure it gets hit… ignorance regarding hit model, but WPF requires this also, hence passthrough clicks, etc.

For each Camera:

  1. Create Box (you could use planes, but adjust other areas as needed), assign the material you created to it, and set the shader to Transparent/Diffuse

  2. Add the box to the camera as a child. Deactivate the box (uncheck it next to the name of the object in the inspector).

  3. Set it’s position x,y,z to 0.

  4. Set it’s scale x,y,z to 10,7,0.1 respectively. (you may need to make it bigger in the x/y if you have huge angle views on your camera. Play around with it to get the size that works. If you zoom in and out, make sure it’s big enough for the maximum zoom you’ll have.

  5. Set it’s rotation x,y,z to 0.

  6. Add a box collider.

  7. If you have multiple of these, you may want to name them we use: ViewCollisionBlocker, MenuCollisionBlocker, etc.

  8. Now, using the tree view, double click on the box to zoom to it. Then shift it out in front of the camera a little bit (0.1??? adjust as needed for your app, etc.).

  9. On your script where you do the OnGUI() code… you may want to add

public GameObject GUIBlockers;

to the script. Then, in the inspector on the script, add your blocker boxes to that array on the script adjusting the array size as needed.

  1. On your script, you may also want to add…

private bool blockersActive = false;

  1. In your OnGUI() section, you may want to add…

void OnGuid()

{

bool showBlockers = false;

// have logic here to determine if you are going to show gui stuff or not

// if you decide to show the gui…then set…

showBlockers = true;

// do your gui stuff

if(GUIBlockers != null)

{

if(showBlockers)

{

if(!blockersActive)

{

blockersActive = true;

foreach(GameObject blocker in GUIBlockers)

{

blocker.SetActive(true);

}

}

}

else if(blockersActive)

{

blockersActive = false;

foreach(GameObject blocker in GUIBlockers)

{

blocker.SetActive(false);

}

}

}

}