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

UI doesn't appear until mouse is moved.

Discussion in 'Scripting' started by ultraviol3nt, Oct 23, 2014.

  1. ultraviol3nt

    ultraviol3nt

    Joined:
    Jan 17, 2010
    Posts:
    155
    I have a piece of code that essentially toggles the HUD on an off. It works beautifully in the editor, exactly the way I'd like. Unfortunately, running as a standalone, the HUD does not appear until after the mouse is moved. Moreover, I'm getting an error (!dest.m_MultiFrameGUIState.m_NamedKeyControlList) that I haven't seen a solution for, and I have to wonder if it's related. It's difficult to tell where the problem is coming from, so I'll post both portions of the UI code.

    uiChat.cs:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class uiChat : MonoBehaviour {
    6.  
    7.     public List<string> globalChannelMessages;
    8.     public List<string> localChannelMessages;
    9.     public string messageToSend;
    10.     public string playerName;
    11.     public Vector2 scrollPosition;
    12.     public GUISkin gSkin;
    13.  
    14.     private GameObject playerCamera;
    15.     private GameObject[] connectedPlayerObjects;
    16.     private float wantedChatX;
    17.     private float currentChatX = -Screen.width / 2;
    18.     private float chatXLerp;
    19.  
    20.  
    21.     string s1 = string.Empty, s2 = string.Empty, s3 = string.Empty;
    22.  
    23.     void Update()
    24.     {
    25.         //find a player camera if one isn't already selected
    26.         if (!playerCamera)
    27.         {
    28.             playerCamera = GameObject.FindGameObjectWithTag("MainCamera");
    29.             playerName = playerCamera.GetComponentInParent<PlayerNet>().playerName;
    30.         }
    31.  
    32.  
    33.         if (Input.GetButtonDown("TOGGLE_HUD"))
    34.         {
    35.             chatXLerp = 0;
    36.         }
    37.  
    38.         if (chatXLerp < 1)
    39.         {
    40.             chatXLerp += .05f;
    41.         }
    42.  
    43.         currentChatX = Mathf.Lerp(currentChatX, wantedChatX, chatXLerp);
    44.  
    45.         /*Send message if the return key is pressed
    46.         if (Input.GetButtonDown("SEND_MESSAGE") && messageToSend != "")
    47.         {
    48.             networkView.RPC("SendChatMessage", RPCMode.AllBuffered, "global", playerName + ": " + messageToSend);
    49.             messageToSend = "";
    50.         }*/
    51.     }
    52.  
    53.     void OnGUI()
    54.     {
    55.         GUI.skin = gSkin;
    56.         gSkin.textField.fixedWidth = Screen.width / 2 - 60;
    57.         gSkin.label.fixedWidth = Screen.width / 2 - 20;
    58.         GUI.skin.horizontalScrollbar = GUIStyle.none;
    59.         //Input.eatKeyPressOnTextFieldFocus = false;
    60.  
    61.         //Chat Box
    62.         if (playerCamera)
    63.         {
    64.          
    65.  
    66.             //Is the player using the HUD?
    67.             if (playerCamera.GetComponent<uiCore>().showHUD)
    68.             {
    69.                 wantedChatX = 5;
    70.             }
    71.             else
    72.             {
    73.                 wantedChatX = -Screen.width / 2;
    74.                 GUIUtility.keyboardControl = 0;
    75.             }
    76.  
    77.                 //Chat messages
    78.                 GUILayout.BeginArea(new Rect(currentChatX, 5, Screen.width / 2, (Screen.height / 4) * 3), gSkin.box);
    79.                 GUILayout.BeginVertical();
    80.                 scrollPosition = GUILayout.BeginScrollView(scrollPosition);
    81.                 foreach (string s in globalChannelMessages)
    82.                 {
    83.                     GUILayout.Label(s);
    84.                 }
    85.                 GUILayout.EndScrollView();
    86.                 GUILayout.EndVertical();
    87.                 GUILayout.EndArea();
    88.  
    89.  
    90.                 //Chat Text Field
    91.                 GUILayout.BeginArea(new Rect(currentChatX, Screen.height / 4 * 3 + 10, Screen.width / 2, 40), gSkin.box);
    92.                 GUILayout.BeginHorizontal();
    93.                 GUI.SetNextControlName("chatTextField");
    94.                 messageToSend = GUILayout.TextField(messageToSend, 140);
    95.                 if (GUILayout.Button("Send") || Input.GetButtonDown("SEND_MESSAGE"))
    96.                 {
    97.                     if (messageToSend != "")
    98.                     {
    99.                         networkView.RPC("SendChatMessage", RPCMode.AllBuffered, "global", playerName + ": " + messageToSend);
    100.                         messageToSend = "";
    101.                     }
    102.                 }
    103.                 GUILayout.BeginHorizontal();
    104.                 GUILayout.EndArea();
    105.         }
    106.      
    107.  
    108.  
    109.  
    110.  
    111.  
    112.         //Prevent tab from selecting chat box
    113.         if (Event.current.keyCode == KeyCode.Tab || Event.current.character == '\t')
    114.             Event.current.Use();
    115.  
    116.         s1 = GUI.TextField(new Rect(-1000000, 0, 0, 0), s1);
    117.         s2 = GUI.TextField(new Rect(-1000000, 0, 0, 0), s2);
    118.         s3 = GUI.TextField(new Rect(-1000000, 0, 0, 0), s3);
    119.     }
    120.  
    121.  
    122.  
    123.  
    124.     [RPC]
    125.     void SendChatMessage(string mChannel, string message)
    126.     {
    127.         if (mChannel == "global")
    128.         {
    129.             //Add new message to message list
    130.             globalChannelMessages.Add(message);
    131.  
    132.             //scroll chat box to bottom
    133.             scrollPosition.y += 200;
    134.         }
    135.     }
    136. }
    And the uiCore.cs:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class uiCore : MonoBehaviour {
    5.  
    6.     public GUISkin gSkin;
    7.     public bool promptInteract = false;
    8.     public bool showHUD = false;
    9.     public float hudHeight = 0;
    10.     private float hudLerpFactor;
    11.     private float hudWantedHeight = 0;
    12.     private float notX = -Screen.width / 4;
    13.     private float wantedNotX;
    14.     public float notXLerpFactor;
    15.     public GameObject rootGO;
    16.     private string timeString;
    17.  
    18.     void OnGUI()
    19.     {
    20.         GUI.skin = gSkin;
    21.         gSkin.button.fixedWidth = Screen.width / 4 + 1;
    22.         gSkin.button.fixedHeight = 40;
    23.         //gSkin.customStyles[0].fixedWidth = Screen.width / 4;
    24.  
    25.         GUI.BeginGroup(new Rect(notX, Screen.height - hudHeight - 45, Screen.width / 4, 40));
    26.         GUI.Box(new Rect(0, 0, Screen.width / 4, 40), "");
    27.         GUI.Label(new Rect(10, 5, Screen.width / 4, 40), "Press E to interact");
    28.         GUI.EndGroup();
    29.  
    30.         GUILayout.BeginArea(new Rect(0, Screen.height - hudHeight, Screen.width, 40));
    31.         GUILayout.BeginHorizontal();
    32.         GUILayout.Button("HOME");
    33.         GUILayout.Button("SOCIAL");
    34.         GUILayout.Button("MAP");
    35.         GUILayout.Button("SETTINGS");
    36.         GUILayout.EndHorizontal();
    37.         GUILayout.EndArea();
    38.  
    39.  
    40.         //Clock
    41.         string hour = System.DateTime.Now.Hour.ToString();
    42.         string minute = System.DateTime.Now.Minute.ToString();
    43.         if(System.DateTime.Now.Minute < 10){
    44.             timeString = hour + ":0" + minute;
    45.         }
    46.         else{
    47.             timeString = hour + ":" + minute;
    48.         }
    49.         GUI.Label(new Rect(Screen.width - 140, Screen.height - hudHeight - 45, 100, 40), timeString, gSkin.customStyles[0]);
    50.  
    51.         if (showHUD)
    52.         {
    53.             Screen.lockCursor = false;
    54.             Screen.showCursor = true;
    55.             GetComponentInParent<CharacterMotor>().enabled = false;
    56.             GetComponentInParent<FPSInputController>().enabled = false;
    57.             rootGO.GetComponent<MouseLook>().enabled = false;
    58.             GetComponent<MouseLook>().enabled = false;
    59.         }
    60.         else
    61.         {
    62.             Screen.lockCursor = true;
    63.             Screen.showCursor = false;
    64.             GetComponentInParent<CharacterMotor>().enabled = true;
    65.             GetComponentInParent<FPSInputController>().enabled = true;
    66.             rootGO.GetComponent<MouseLook>().enabled = true;
    67.             GetComponent<MouseLook>().enabled = true;
    68.         }
    69.     }
    70.  
    71.     void Update()
    72.     {
    73.         if (Input.GetButtonDown("TOGGLE_HUD"))
    74.         {
    75.             if (showHUD == true)
    76.             {
    77.                 showHUD = false;
    78.                 hudWantedHeight = 0;
    79.             }
    80.             else
    81.             {
    82.                 showHUD = true;
    83.                 hudWantedHeight = 40;
    84.             }
    85.             hudLerpFactor = 0;
    86.         }
    87.     }
    88.  
    89.     void FixedUpdate()
    90.     {
    91.         //Toggle interaction prompt
    92.         if (notX != wantedNotX)
    93.         {
    94.             notX = Mathf.Lerp(notX, wantedNotX, notXLerpFactor);
    95.         }
    96.  
    97.         if (notXLerpFactor < 1)
    98.         {
    99.             notXLerpFactor += .05f;
    100.         }
    101.  
    102.         if (promptInteract)
    103.         {
    104.             wantedNotX = 5;
    105.         }
    106.         else
    107.         {
    108.             wantedNotX = -Screen.width / 4;
    109.         }
    110.  
    111.         //Toggle hud
    112.         if (hudHeight != hudWantedHeight)
    113.         {
    114.             hudHeight = Mathf.Lerp(hudHeight, hudWantedHeight, hudLerpFactor);
    115.         }
    116.  
    117.         if (hudLerpFactor < 1)
    118.         {
    119.             hudLerpFactor += .05f;
    120.         }
    121.     }
    122. }
    123.  
    To reiterate, the UI behaves properly in the editor. When the player starts, the UI is hidden until he/she presses the Tab key. The UI is then shown until the player presses tab again. But in standalone, the UI is hidden even after pressing tab, and doesn't appear until the player moves the mouse.

    Possible related error is !dest.m_MultiFrameGUIState.m_NamedKeyControlList

    Also, the return key doesn't do anything. Maybe related?
     
    Last edited: Oct 23, 2014