Click anywhere cancels action

I have developed a ui where a user clicks on a 3d object, and gets a popup uGUI selection to make a choice. The choice is critical to determine what type of action the user wants to do. I have this working, and it works well IF the user makes a choice. My issue is that the user COULD choose to click somewhere else, not on any of the available choices. Since the popup is just a small panel displayed over the 3d Object, I have no way to stop them from clicking anywhere else on the screen, nor am i sure I would want to.

In the end, if a user clicks elsewhere, I need to undo the primary action. How can I write something that basically says “If a user clicks anywhere else on the screen but on this panel then…”?

Put a big invisible Image in the back of your UI hierarchy. Catch clicks on that (e.g. by adding an EventTrigger), and then you can invoke your code to do the cancel.

There are certainly other ways to do it (such as adding a custom ray-caster to your scene), but this is my go-to solution to this common problem.

2 Likes

This is easiest.

Other ways include:

  • A custom raycaster
  • Checking for input in Update and canceling if the pointer is not over a GameObject
  • Rewriting EventSystem to deliver cancel events

But as a general rule, using a invisible image is the simplest way to do it.

2 Likes