Search Unity

[SOLVED] OnGUI Panes Drawing/Not Staying Drawn

Discussion in 'Immediate Mode GUI (IMGUI)' started by Alex_Snyder, Apr 22, 2016.

  1. Alex_Snyder

    Alex_Snyder

    Joined:
    Apr 22, 2016
    Posts:
    3
    Alrighty, I've got a pretty newbie problem but I'm really struggling to solve it myself so I'm hoping for some support here.

    I'm trying to draw a pane on the left half of the screen to display the character's RPG-style stats (like Diablo 2's character window). It would open and close when the player hit an input button (default 'c'), and automatically close when they started moving.
    Using the code below (my current code) it won't go away. And if I put the input controls in OnGUI() it won't appear at all. So it's either always there or never there (and i'm getting frustrated).

    Also I don't wanna use GUI.Window cuz I don't want it to move at all. I've scoured the forums and the Unity Guide for answers but haven't found anything that has worked for me.

    Any and all help would be appreciated. Thank you in advance :).

    Code (CSharp):
    1.  
    2. public class PaneDrawer : MonoBehaviour
    3. {
    4.     public Texture2D bgTex;
    5.     [HideInInspector]private Character character;
    6.     [HideInInspector]public bool charMenu = false;
    7.  
    8.     private ClickMovement move;
    9.  
    10.     private Rect leftPane = new Rect (0, 0, (Screen.width - (0.6f * Screen.width)), (Screen.height - (0.125f * Screen.height)));
    11.     private Rect rightPane = new Rect (Screen.width, 0, (Screen.width - (0.4f * Screen.width)), (Screen.height - (0.125f * Screen.height)));
    12.  
    13.     void Start ()
    14.     {
    15.         character = GetComponent<Character> ();      
    16.         charMenu = false;
    17.         move = GetComponent<ClickMovement> ();
    18.     }
    19.  
    20.     void Update ()
    21.     {
    22.         if (move.moving == false) {
    23.             if (Input.GetButton ("charMenu") && charMenu == false) {
    24.                 OnGUI ();
    25.                 charMenu = true;
    26.                 Debug.Log ("Open Char Menu");
    27.                 return;
    28.             } else if (charMenu == true && Input.GetButton ("charMenu")) {
    29.                 charMenu = false;
    30.                 Debug.Log ("Close Char Menu");
    31.                 return;
    32.             }
    33.         }
    34.     }
    35.  
    36.     void OnGUI ()
    37.     {      
    38.         GUI.Box (leftPane, bgTex);
    39.     }
    40. }
    41.  
     
  2. Alex_Snyder

    Alex_Snyder

    Joined:
    Apr 22, 2016
    Posts:
    3
    Edit: I just blew my own mind! I realized I could make the panes (complete with text fields and everything) in the inspector, out of game objects. And use the script to simply toggle it's enabled field.