Search Unity

Make GUI appear when input is held

Discussion in 'Immediate Mode GUI (IMGUI)' started by Treasureman, Oct 1, 2014.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have an input called "Aim" and I want it to make a GUI texture appear when it's held (like a scoped sniper). How do I do this?

    P.S. I also want a script to make gui disappear (like the health bar)?
     
  2. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    Will be the input be through keypresses, mouseclicks, through touch input?

    • If it's a keypress, you can check the elapsed since between Input.GetKeyDown
    • If it's a mouseclick, you can check the time elapsed since Input.GetMouseButtonDown
    • If it's a touch, you can check the deltaTime since TouchPhase.Began
    The underlying idea is that you measure the time since an event. For instance, if it's been 1 second since Input.GetKeyDown("c"), then make the GUI texture appear.


    To make the GUI texture appear, there are a few things you can do. Here's an example.
    Code (CSharp):
    1. // you can set a public GUITexture in your script
    2. public GUITexture myTexture; // then drag in the GUITexture in the inspector
    3.  
    4. void Start()
    5. {
    6.     // you can either disable the GUITexture in the hierarchy so that it doesn't display by default,
    7.     // or use this line to ensure that it is disabled by default, if it were to accidentally get turned on in the hierarchy
    8.     myTexture.enabled = false;
    9. }
    10.  
    11. // then you check if the appropriate amount of time has elapsed, i.e. held the button long enough
    12. void Update()
    13. {
    14.  
    15.     if( //some condition)
    16.     {
    17.         myTexture.enabled = true;
    18.     }
    19.  
    20. }


    How do you want the gui to disppear?
    1. will it automatically disappear when the player stops holding the button or key that activated the gui? *
    2. will the player be able to let go of the button or key that activated it and then press the same button or key for a the same amount of time?
    3. will the player have to press a different button?
    If you choose number 1, then you'll need to use Input.GetKeyUp, Input.GetMouseButtonUp, or TouchPhase.Ended in conjunction with one of the respective methods listed at the top.


    If you could provide more details, then either I or someone else could help you better :)
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    Ok,
    I want it to appear when "(Input.GetButton("Aim") is activated.

    As for the disappearing, I want the texture to disappear when "(Input.GetButton("Aim") is active, same thing.

    Sorry, I just didn't understand the list you gave me. I'm really new to scripting.
     
  4. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    When I said GetKeyDown("c"), I was referring the actual letter c on a keyboard. "Aim" is not a keyboard button. However, you can add an axis in the Input Manager (Edit > Project Settings > Input), which will allow you to assign names to buttons, but this will still require an actual keyboard (or mouse, joystick, etc.) button to be mapped to.


    So if the user presses the button once, the texture will stay until they press it again to turn it off, is that right? Just trying to understand better.

    I'm sorry you didn't understand - I'll try to break it down more.