Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Heads up Display

Discussion in 'Getting Started' started by boolfone, Jun 25, 2016.

  1. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289
    Is there a way to detect a click on a UI Image?

    I want to make a button on my maze game (http://coolfone.mobi/fpmaze9/) so that when the user is holding down the button, the character moves forward.

    Thanks.
     
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    How is your UI implemented? I mean, the answer is "yes", but the specifics of how depend on your UI system.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yes. Just add an EventTrigger component.

    Or implement the EventSystem interfaces.

    This video is not explicitly about detecting clicks, but it does go through the process.

     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    Indeed, I've found the best way to detect clicks anywhere on the screen is to put a full-screen Image (with no texture and color alpha set to zero, so it's invisible) in the UI behind everything else, and then attach an EventTrigger. Then you hook that up to whatever game object should handle clicks in the world.

    The reason I find this the best is that you can then properly handle clicks on other UI elements (e.g. buttons) without those falling through to the world. That can be a real thorny problem otherwise.
     
  5. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    You implement interface and you need:

    Code (CSharp):
    1. using UnityEngine.UI;
    2. using UnityEngine.EventSystems;
    Code (CSharp):
    1. IPointerDownHandler // In your class
    2.  
    3. // then
    4.  
    5. public virtual void OnPointerDown(PointerEventData data)
    6. {
    7.     // Your click action here
    8. }
     
  6. boolfone

    boolfone

    Joined:
    Oct 2, 2014
    Posts:
    289

    Okay. Event trigger appears to work.

    event-trigger.png

    I posted a new version of the maze with event triggers at http://coolfone.mobi/fpmaze11/.

    Thanks.