Search Unity

Running code when the player clicks anywhere BUT on a button

Discussion in 'UGUI & TextMesh Pro' started by Robs, Sep 3, 2014.

  1. Robs

    Robs

    Joined:
    Jan 12, 2014
    Posts:
    5
    Hello all,

    On my main menu I want the player to be able to start the game by pressing anywhere on the screen except for on buttons like "options" and "info". I was using EventSystemManager.currentSystem.IsPointerOverEventSystemObject () to try to ignore clicks on buttons, but I then ran into the problem that the clicks didn't go through text elements either. How would I do this?

    Additionally, I am attaching an empty GameObject to the Canvas with a script that handles this logic while the main menu is active. Is there a way to do all of this without having to attach an empty GameObject to the UI?
     
  2. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    Well, in essence what you're proposing kind of is just a giant, invisible button though. Is there any particular reason you don't like going this route?
     
  3. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    This seems like it may be best accomplished with regular old scripting.

    Have a boolean set to "gameOn = false" or something, and then wait for a touch. If the touch is on a button, ignore it (disable the button interactivity until the game starts), but if the touch isn't on a button, do some start game function.
     
  4. pixpusher2

    pixpusher2

    Joined:
    Oct 30, 2013
    Posts:
    121
    Try attaching IgnoreRaycast.cs script to the Text elements to prevent it from catching the clicks. The script is from the UI Example Project.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class IgnoreRaycast : MonoBehaviour, ICanvasRaycastFilter
    6. {
    7.     public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
    8.     {
    9.         return false;
    10.     }
    11. }
     
    Ghosthowl and infinitypbr like this.