Search Unity

Discussion GUI input system

Discussion in 'Scripting' started by StarBornMoonBeam, May 10, 2023.

  1. StarBornMoonBeam

    StarBornMoonBeam

    Joined:
    Mar 26, 2023
    Posts:
    209
    Code (CSharp):
    1.  
    2.     public string KEY = "A";
    3.     private void OnGUI()
    4.     {
    5.         if (Event.current.Equals(Event.KeyboardEvent(KEY)))
    6.             Debug.Log("OK");
    7.     }
    8.  
    Since event.current.equals keyboardevent OurKey, if an OnGui section did not exist, but the game still used an event system for Canvas based UI; then keyboard events are always fired. But multiple checks need not be made, For example; I dont need to write a tonne of if statements to see if GetKeyDown.KeyCode("A") was keycode A if the Event System already knew what the current keyboard event was. Holding down key A or tapping KeyA still provides distinction between GetKey and KeyDown. The above code will not work anywhere else except OnGUI().

    So why does not the GUI input system see much usage? Is there something limited about it?

    What can you tell me about the GUI input system?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,925
    It's a leftover relic from Unity's early days when the only UI system Unity had was IMGUI, before uGUI became a thing.

    The
    Event
    class still sees use when dealing with IMGUI custom inspectors, but seldom anyone will be using IMGUI in actual gameplay code.

    We have much better ways of handling input these days, such as the Input System.
     
    StarBornMoonBeam likes this.
  3. StarBornMoonBeam

    StarBornMoonBeam

    Joined:
    Mar 26, 2023
    Posts:
    209
    agreed

    I read about some advantages to it, it is more typing friendly. I used it, and it can only detect one at a time. If the current changes the input is not logged, and it appears only one keyboard event can happen without a fair bit of work.

    However I didn't try very hard. But I did explore the event Unity - Scripting API: UnityEvent (unity3d.com) and it seems useful.