Search Unity

How cool is that? ;-)

Discussion in 'Immediate Mode GUI (IMGUI)' started by jashan, Jan 8, 2008.

  1. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    I may be the last to get it, but I guess UnityGUI is "kind of" cool ;-)

    Where else could you do something like:

    Code (csharp):
    1.  
    2. if (GUILayout.Toggle(selectedGameType == GameType.Multiplayer, "[M]ultiplayer")
    3.     || Input.GetKeyDown("m")) {
    4.   selectedGameType = GameType.Multiplayer;
    5.   isValid = false;
    6. }
    7.  
    This just rocks!!!

    :)

    Jashan
     
  2. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    PS ... one must be careful, though... when doing this to dynamically change the GUI (i.e. you hit a key and some new areas pop up), it seems like the GUI system is having a problem with finding some references. I would assume this has to do with the multiple OnGUI calls during one frame (one for layout, one for "event-handling"... or maybe more).

    My fix was to simply move those very specific cases back to the Update()-method (not very nice, but at least it works ;-) ).

    ... error is something like "Getting control 0's position in a group with only 0 controls when doing keyDown"...

    Jashan
     
  3. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
    When doing that kind of "non" gui checks it's usually a good idea only to do them on the last call to the OnGUI i.e.
    Code (csharp):
    1.  
    2. if (GUILayout.Toggle(selectedGameType == GameType.Multiplayer, "[M]ultiplayer")
    3.     || (Input.GetKeyDown("m")  Event.current = EventType.repaint)) {
    4.   selectedGameType = GameType.Multiplayer;
    5.   isValid = false;
    6. }
     
  4. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    That looks like a cool idea, and interestingly, when I do add

    Code (csharp):
    1.  Event.current.type == EventType.keyDown
    the original problem is fixed. When I check EventType.repaint, however, the key stroke is not recognized. I can fix that by changing Input.GetKeyDown to Input.GetKey - however, then, even when using EventType.repaint, I'm getting the original problem.

    Now, what really confuses me: I have these buttons spread over a few functions because the GUI has various states, depending on the selections. Interestingly, while the problem occured only in one particular instance before, which can be fixed by adding a check to "EventType.keyDown", that same code messes everything up where it worked before by only checking "Input.GetKeyDown(...)".

    Is there any "proper" way of doing this? I think keyboard shortcuts for buttons are a rather nice thing to add, but at the moment the way I'm doing this feels kind of hackish ;-)

    Jashan
     
  5. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Try using something like this:

    if (Event.current.Equals (Event.KeyboardEvent ("[esc]"))) {
    Debug.Log ("ESC was pressed");
    }

    Now, this method has some bugs in the 2.0.1 - but that's the "proper" way to do these kind of things
     
  6. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    This method is not as cool, but it works (plus it still works if CAPS lock is on) :)
    Code (csharp):
    1. Testbln = GUILayout.Toggle(Testbln, "Im a toggle");
    2. if (Event.current.character.ToString().ToLower() == "m"  Event.current.type == EventType.keyUp) Testbln = !Testbln;
    Cheers
    Shaun
     
  7. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Is capitals not working one of the bugs? i.e.
    Code (csharp):
    1. if(Event.current.Equals(Event.KeyboardEvent("M")) || Event.current.Equals(Event.KeyboardEvent("m"))) Testbln=!Testbln;
    Using the above code, I never get the event if CAPS lock is on.
     
  8. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Hm... that...

    Code (csharp):
    1.  
    2. if (GUI.Button("[C]onfigure")
    3.     || Event.current.Equals(Event.KeyboardEvent("c"))) {
    4.     isConfigurationScreen = true;
    5. }
    6.  
    ... doesn't work for me, the same code works smoothly with Input.GetKeyDown and checking against Event.current.type == EventType.keyDown. It's in an OnGUI() method.

    @shaun: I would believe that this is "by design". Capital letters are referred to by shift (#), so it should probably be Event.KeyboardEvent("#m")... I haven't checked that, though, but the manual says it:

    Scripting > Runtime Classes > Event > Event.KeyboardEvent

    Jashan
     
  9. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Thanks Jashan... I was still in .ToString() mode, when it's a Keyboard event. doh!
     
  10. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    Just how buggy is it? I can't get it to work anywhere. Is the bug in any way related with powerbook g4?

    Regards,
    Afonso