Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity 4 - GUI.FocusControl() Broken?!?

Discussion in 'Developer Preview Archive' started by BrUnO-XaVIeR, Aug 28, 2012.

  1. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    Hmm... Looks like it is broken in this b7 build; Can someone confirm this?
    I went back on every scene done to check if it is all ok and I noticed there are weird bugs in this remade OnGUI() interface.
    The main thing I see is the focus of controls seems bound to the mouse now, FocusControl() does nothing... Is it just me?

    It totally ruined my menu controlled by joysticks/keyboards.

    Note: Tested on Mac only. Not working.
     
    Last edited: Aug 28, 2012
  2. BBRome

    BBRome

    Joined:
    Jul 3, 2012
    Posts:
    373
    +1 but only full screen
     
  3. aaronflippo

    aaronflippo

    Joined:
    Jun 7, 2012
    Posts:
    45
    I'm seeing this in Unity 3.5.6f4 as well. As soon as fullscreen is activated, GUI.FocusControl() seems to have no effect.
     
  4. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    714
    I think I also encountered this issue.

    I didn't raise any bugs since I've put the blame on my lack of programming skill and started doing something else, hoping that I would find the problem at a later date.
     
  5. UnLogick

    UnLogick

    Joined:
    Jun 11, 2011
    Posts:
    1,745
    Just tried it on an old 3.5.2 and the latest internal 4.0 build and GUI.FocusControl is working.

    If this is not the case for you please submit a bug with a repro project.
     
  6. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    I am using Unity 4b7.
    SetFocus is not working here whatsoever; Not on editor, nor built.
    Bug is happening everywhere: editor, flash player, webplayer, standalone, fullscreen, windowed, etc.
    I guess this bug was introduced by the re-write of Unity GUI. Controls are focused by mouse only.

    Bug Report:
    Case 493113

    Sample code, this is working just fine in Unity 3.5 for me, but I can't make it work on Unity 4b7:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class MainMenu : MonoBehaviour {
    5.     enum STATES{Main,Start,Load,Options}; STATES STATE = STATES.Main;
    6.     //
    7.     public GUIStyle BeginStyle;
    8.     public GUIStyle LoadStyle;
    9.     public GUIStyle OptionsStyle;
    10.     public GUIStyle QuitStyle;
    11.     //
    12.     private int FocusID = 0;
    13.     private float FocusTimer;
    14.     private bool FocusChanged;
    15.     //
    16.     //
    17.     void Start() {
    18.         Debug.Log("Using WASD keys as Vertical | Horizontal Axis.");
    19.     }
    20.     //
    21.     void OnGUI() {
    22.         FocusChanged=true;
    23.         //
    24.         if (STATE == STATES.Main) {
    25.             GUI.SetNextControlName("0");
    26.             if (GUI.Button(new Rect(50,Screen.height-(BeginStyle.fixedHeight*4),BeginStyle.fixedWidth,BeginStyle.fixedHeight),"",BeginStyle)) {
    27.                 STATE = STATES.Start;
    28.             }
    29.             //
    30.             GUI.SetNextControlName("1");
    31.             if (GUI.Button(new Rect(265,Screen.height-(LoadStyle.fixedHeight*2.85f),LoadStyle.fixedWidth,LoadStyle.fixedHeight),"",LoadStyle)) {
    32.                 //STATE = STATES.Load;
    33.             }
    34.             //
    35.             GUI.SetNextControlName("2");
    36.             if (GUI.Button(new Rect(525,Screen.height-(OptionsStyle.fixedHeight*2.1f),OptionsStyle.fixedWidth,OptionsStyle.fixedHeight),"",OptionsStyle)) {
    37.                 //STATE = STATES.Options;
    38.             }
    39.             //
    40.             GUI.SetNextControlName("3");
    41.             if (GUI.Button(new Rect(Screen.width-(QuitStyle.fixedWidth+10),Screen.height-(QuitStyle.fixedHeight+5),QuitStyle.fixedWidth,QuitStyle.fixedHeight),"",QuitStyle)) {
    42.                 Application.Quit();
    43.             }
    44.             //
    45.             FocusID = ManageFocus(FocusID,3);
    46.             if (Input.GetButtonDown("Jump")) {
    47.                 if (FocusID < 0) {return;}
    48.                 if (FocusID == 0) {Debug.Log("Another Menu Window.");}//STATE = STATES.Start;}
    49.                 if (FocusID == 1) {Debug.Log("Another Menu Window.");}//STATE = STATES.Load;}
    50.                 if (FocusID == 2) {Debug.Log("Another Menu Window.");}//STATE = STATES.Options;}
    51.                 if (FocusID == 3) {Application.Quit();}
    52.             }
    53.         } else {FocusID = -1;}
    54.         //
    55.             //...
    56.         //
    57.         if (STATE == STATES.Start) {
    58.             Application.LoadLevel("Menu");
    59.         }
    60.     }
    61.     //
    62.     // This is working on Unity 3.5, but 'GUI.FocusControl()' in Unity 4 is doing nothing; Or is 'GUI.SetNextControlName()' that is not working??
    63.     int ManageFocus(int ID, int Lenght) {
    64.         GUI.FocusControl(ID.ToString());
    65.         if (FocusChanged  Time.timeSinceLevelLoad > FocusTimer+0.35f) {FocusChanged=false; FocusTimer=Time.timeSinceLevelLoad;}
    66.         if ((Input.GetAxis("Vertical") < 0 || Input.GetAxis("Horizontal") > 0)  !FocusChanged) {FocusChanged=true; if (ID < Lenght) {ID++;}     Debug.Log("Focus:("+ID+") | Why don't this work in Unity 4?? The button used to be focused on Unity 3...");} else
    67.         if ((Input.GetAxis("Vertical") > 0 || Input.GetAxis("Horizontal") < 0)  !FocusChanged) {FocusChanged=true; if (ID > 0) {ID--;}      Debug.Log("Focus:("+ID+") | Why don't this work in Unity 4?? The button used to be focused on Unity 3...");}
    68.         if (ID < 0) {ID = -1;} if (ID > Lenght) {ID = Lenght;} return ID;
    69.     }
    70.     //
    71.     // Used by other scripts:
    72.     public void SetState(string ID) {
    73.         FocusID = -1;
    74.         if (ID == "Main") {STATE = STATES.Main;}
    75.         if (ID == "Start") {STATE = STATES.Start;}
    76.         if (ID == "Load") {STATE = STATES.Load;}
    77.         if (ID == "Options") {STATE = STATES.Options;}
    78.     }
    79. }
    80.  
     
    Last edited: Oct 11, 2012
  7. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    714
    I sent a bug (Case 493488). I hope it gets fixed someday since this is the only way to control the GUI with a gamepad or keyboard.

    I can't really post all my code here since there are multiple dependencies and that would be a mess. However, here is the FocusControl part that doesn't work. I receive all the logs and everything follows a simple logic that doesn't fail on runtime.

    Code (csharp):
    1. for(var i = 0; i < dOptLength ; i++) {
    2.        
    3.             var iS = i.ToString();
    4.             GUI.SetNextControlName ("DialogueChoice" + iS);
    5.             Debug.Log("DialogueChoice" + iS); // I RECEIVE THIS LOG.
    6.             if (GUI.Button(Rect (dOptRectLocX, dOptRectLocYN, dOptRectScaleX, dOptRectScaleY), dOpt[i], dialogueChoicesStyle)) {
    7.                 NextDialogue(i);
    8.             }
    9.             dOptRectLocYN = dOptRectLocYN + dOptRectScaleY/1.4;
    10.         }
    11.        
    12.          if(Input.GetKeyDown("u")) {
    13.          
    14.          GUI.FocusControl ("DialogueChoice1");
    15.          Debug.Log("Done"); // I RECEIVE THIS LOG.
    16.          
    17.         }
     
    Last edited: Oct 10, 2012
  8. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    I've got an email from Unity stating that the issue has been fixed and the fix will be available in a future beta; Let's wait :)
     
  9. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    Fix not available on b11... Pity :(
    Btw, I luv new icons. Tons of nice icons on b11.