Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

My GUI is looks S*** when I Build and Run my game but looks fine in the editor

Discussion in 'Immediate Mode GUI (IMGUI)' started by 420BlazeIt, Apr 16, 2015.

  1. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Heres a video of my problem:


    As you can see everything is fine in the editor but in the built version theres a ton of stuff wrong!

    Stuff wrong in the built version:
    • Every GUILayout.Label looks weird in the built version.
    • GUI.Windows are still visable behind the Main Menu's buttons.
    • Everything looks weird.
    Please help guys!!! Thanks for reading!

    Edit: Changed link from vid.me to youtube.com because vid.me made the video have terrible quality.
     
    Last edited: Apr 16, 2015
  2. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Has nobody ever had this before?
     
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You have to show the code if you want help. The old GUI can be very tricky and sensitive when it comes to code execution paths.
     
    420BlazeIt likes this.
  4. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Sorry heres the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Text.RegularExpressions;
    4.  
    5. public class NetworkManager : MonoBehaviour {
    6.  
    7.     public GUISkin MainMenuGUISkin;
    8.  
    9.     private bool showStartServer;
    10.     private bool showJoinServer;
    11.  
    12.     private string startServerPort;
    13.     private string startServerMaxPlayers;
    14.  
    15.     private string joinServerIP;
    16.     private string joinServerPort;
    17.  
    18.     void OnGUI() {
    19.  
    20.         GUILayout.Label("FPS Prototype - Adam Stuck");
    21.  
    22.         //Only show the main menu if you are not connected to a server or if you are not hosting a server
    23.         if(!Network.isClient && !Network.isServer) {
    24.  
    25.  
    26.             if(!showStartServer && !showJoinServer) {
    27.  
    28.                 GUI.skin = MainMenuGUISkin;
    29.  
    30.                 GUI.Window(0, new Rect(Screen.width/2-(Screen.width/4)/2, 0, Screen.width/4, Screen.height), MainMenuWindow, "");
    31.  
    32.                 GUI.skin = null;
    33.  
    34.             }
    35.  
    36.             if(showStartServer) {
    37.                
    38.                 GUI.Window(1, new Rect(Screen.width/2-200/2, Screen.height/2-275/2, 200, 275), StartServerWindow, "");
    39.                
    40.             }
    41.  
    42.             if(showJoinServer) {
    43.                
    44.                 GUI.Window(2, new Rect(Screen.width/2-200/2, Screen.height/2-275/2, 200, 275), JoinServerWindow, "");
    45.                
    46.             }
    47.  
    48.         }
    49.  
    50.     }
    51.  
    52.     //Main Menu Window
    53.     void MainMenuWindow (int windowID) {
    54.  
    55.         //Ensures that the buttons are in the middle of the screen
    56.         int middleOfScreen = (Screen.height/9) + (Screen.height/30) + (Screen.height/9) + (Screen.height/30) + (Screen.height/9);
    57.  
    58.         GUILayout.Space(Screen.height/2 - middleOfScreen/2);
    59.  
    60.         //Hide main menu and show Start Server window.
    61.         if(GUILayout.Button("Start Server", GUILayout.Height(Screen.height/9))) {
    62.  
    63.             startServerPort = "26500";
    64.             startServerMaxPlayers = "10";
    65.  
    66.             showStartServer = true;
    67.  
    68.         }
    69.  
    70.         GUILayout.Space(Screen.height/30);
    71.  
    72.         //Hide main menu and show Join Server window.
    73.         if(GUILayout.Button("Join Server", GUILayout.Height(Screen.height/9))) {
    74.  
    75.             joinServerIP = "127.0.0.1";
    76.             joinServerPort = "26500";
    77.  
    78.             showJoinServer = true;
    79.  
    80.         }
    81.  
    82.         GUILayout.Space(Screen.height/30);
    83.  
    84.         //Exit game.
    85.         if(GUILayout.Button("Exit Game", GUILayout.Height(Screen.height/9))) {
    86.  
    87.             Application.Quit();
    88.  
    89.         }
    90.        
    91.     }
    92.  
    93.     //Start Server Window
    94.     void StartServerWindow (int windowID) {
    95.  
    96.         GUILayout.Label("Port");
    97.  
    98.         startServerPort = GUILayout.TextField(startServerPort, 5);
    99.  
    100.         startServerPort = Regex.Replace(startServerPort, "[^0-9]", "");
    101.  
    102.         GUILayout.Space(20);
    103.  
    104.         GUILayout.Label("Max Players");
    105.        
    106.         startServerMaxPlayers = GUILayout.TextField(startServerMaxPlayers, 2);
    107.  
    108.         startServerMaxPlayers = Regex.Replace(startServerMaxPlayers, "[^0-9]", "");
    109.  
    110.         GUILayout.Space(20);
    111.  
    112.         //Start the server
    113.         if(GUILayout.Button("Start Server", GUILayout.Height(Screen.height/20))) {
    114.            
    115.             Network.InitializeServer(int.Parse(startServerMaxPlayers), int.Parse(startServerPort), false);
    116.            
    117.         }
    118.        
    119.         GUILayout.Space(10);
    120.  
    121.         //Go back to main menu.
    122.         if(GUILayout.Button("Go Back", GUILayout.Height(Screen.height/20))) {
    123.            
    124.             showStartServer = false;
    125.            
    126.         }
    127.  
    128.     }
    129.  
    130.     //Join Server Window
    131.     void JoinServerWindow (int windowID) {
    132.  
    133.         GUILayout.Label("IP");
    134.  
    135.         joinServerIP = GUILayout.TextField(joinServerIP, 15);
    136.  
    137.         joinServerIP = Regex.Replace(joinServerIP, "[^0-9.]", "");
    138.  
    139.         GUILayout.Space(20);
    140.        
    141.         GUILayout.Label("Port");
    142.        
    143.         joinServerPort = GUILayout.TextField(joinServerPort, 5);
    144.        
    145.         joinServerPort = Regex.Replace(joinServerPort, "[^0-9]", "");
    146.  
    147.         GUILayout.Space(20);
    148.  
    149.         //Connect to server
    150.         if(GUILayout.Button("Connect to Server", GUILayout.Height(Screen.height/20))) {
    151.            
    152.             Network.Connect(joinServerIP, int.Parse(joinServerPort));
    153.  
    154.         }
    155.  
    156.         GUILayout.Space(10);
    157.  
    158.         //Go back to main menu.
    159.         if(GUILayout.Button("Go Back", GUILayout.Height(Screen.height/20))) {
    160.            
    161.             showJoinServer = false;
    162.            
    163.         }
    164.        
    165.     }
    166.  
    167. }
     
  5. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    Old onGUI is fun, I must have 15000 lines of code and 100 Booleans dedicated to exposing and hiding various onGUI windows as appropriate. Your Boolean conditions on when to hide & show onGUI elements isn't complete.
     
    Dantus likes this.
  6. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Can you be a bit more specific as to what line I am missing what on please?
     
  7. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Guys seriously?! Nobody knows a solution??? Come on. This is really frustrating and "Your Boolean conditions on when to hide & show onGUI elements isn't complete" didn't help me at all. Somebody just tell me what the problem is properly so I can get it fixed. Waiting 2 days just to get some stupid Unity bug fixed with your vague answers is not at all helpful. I didnt post on the forum to find out "Your Boolean conditions on when to hide & show onGUI elements isn't complete" I posted here because I need an answer!!!!
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Goat is completely right. There is most likely something wrong with your booleans. A workaround for that issue might be to use the following code:
    Code (csharp):
    1. if(!showStartServer && !showJoinServer) {
    2.   GUI.skin = MainMenuGUISkin;
    3.   GUI.Window(0, new Rect(Screen.width/2-(Screen.width/4)/2, 0, Screen.width/4, Screen.height), MainMenuWindow, "");
    4.   GUI.skin = null;
    5. } else if(showStartServer) {
    6.   GUI.Window(1, new Rect(Screen.width/2-200/2, Screen.height/2-275/2, 200, 275), StartServerWindow, "");
    7. } else if(showJoinServer) {
    8.   GUI.Window(2, new Rect(Screen.width/2-200/2, Screen.height/2-275/2, 200, 275), JoinServerWindow, "");
    9. }
    It uses else if to make sure only one of the windows is being drawn at once.
     
  9. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    Thanks for the reply but unfortunately this didn't fix anything :(

    I had a look at the new GUI system but it seems like I would need to be able to draw to make a good-looking gui with that.
     
  10. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Did you make sure that the script is only used once in the project?
     
  11. 420BlazeIt

    420BlazeIt

    Joined:
    Aug 14, 2014
    Posts:
    102
    If you mean there is only one gameobject that has this script then yes.
     
  12. Xaurrien

    Xaurrien

    Joined:
    Jul 2, 2012
    Posts:
    20
    Is there an active camera rendering something behind your GUI?
    It looks like the surface behind your GUI is never cleared. That could explain why the windows are still visible.

    I suggest to add a camera rendering a solid color or to use GUI.DrawTexture() to draw some background before the rest of the GUI.