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

Key press event triggers?

Discussion in 'UGUI & TextMesh Pro' started by junglemason, Mar 13, 2015.

  1. junglemason

    junglemason

    Joined:
    Dec 30, 2010
    Posts:
    69
    I want the escape key (or back button on an Android) to close my modal dialog window. Detecting the key press is the easy part. How do I send it to a UI element (or broadcast it to all UI elements) using the new event system?
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    you would not need to "Send" it to the UI.
    Just have a script that references the GO for the modal window and use "SetActive" on it to hide/unhide it.
     
  3. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Nooooo, Never use EventTriggers unless you really really have to. They are a last resort dynamic object.
    Better to implement your own script using the Event Interfaces that you actually need for a GO, e.g. iPointerDownHandler for click events.
     
    junglemason likes this.
  5. junglemason

    junglemason

    Joined:
    Dec 30, 2010
    Posts:
    69
    What I came up with was:
    I've extended MonoBehaviour into a custom UIElementBase which catches and handles the escape key in Update, and then all my modal window classes inherit it instead of MonoBehaviour.
     
  6. junglemason

    junglemason

    Joined:
    Dec 30, 2010
    Posts:
    69
    Thanks for this advice. I'll keep it in mind. However, I found that the only event interfaces Unity offers are for mouse/touch events and no keyboard events.
     
  7. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    The events are just are what are currently exposed by the Input handlers. The standalone input handler does handle keyboard events. (a bit)
    If you want to control the events for different inputs, then you need to write your own InputHandler :D
     
  8. junglemason

    junglemason

    Joined:
    Dec 30, 2010
    Posts:
    69
    If I end up needing much more than just the escape key for modals, I'll start looking into custom Input Modules. It looks like Unity might even make theirs open source in the future, so that's nice.
     
  9. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    junglemason likes this.