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

Hide GUIButton But Still Have It Active?

Discussion in 'Scripting' started by GhulamJewel, Dec 1, 2015.

  1. GhulamJewel

    GhulamJewel

    Joined:
    May 23, 2014
    Posts:
    351
    Hello I am trying to hide GUIButton/Label with a separate script but still have it active so the buttons still work but the texture and the buttons disappeared. Below is the script that has the GUIButtons which I would like to hide and unhide through a different script.


    Code (CSharp):
    1.    
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class ModifyTerrain : MonoBehaviour {
    7.  
    8.  private GUISkin skin;
    9.  
    10.  
    11. public void OnGUI(){
    12.  
    13.         // Set the skin to use
    14.         GUI.skin = skin;
    15.  
    16.         GUI.skin.label.alignment = TextAnchor.MiddleCenter;
    17.         GUI.color = Color.red;
    18. //        GUI.Label (new Rect (Screen.width / 2 - 100, Screen.height - 120, 200, 30), blocks.blockInfo[currentBlock].Name);
    19.         GUI.color = Color.white;
    20.  
    21.         GUI.BeginGroup (invRect, "", "Box");
    22.         for(int i = 0; i < 10; i++) {
    23.             if(GUI.Button (new Rect(i * 47, 0, 47, 45), blocks.blockInfo[i+1].guiTex)){
    24.                 currentBlock = i+1;
    25.             }
    26.         }
    27.         GUI.EndGroup ();
    28.  
    29. //        if(invRect.Contains (GUIUtility.ScreenToGUIPoint (Event.current.mousePosition))){
    30. //            containsMouse = true;
    31. //        }else{
    32. //            containsMouse = false;
    33. //        }
    34.         GUI.Label (new Rect (Screen.width / 2 - 40, Screen.height / 2 - 40, 80, 80), crossImg);
    35.     }
    36. }

    This script I have created works great but this disables the script so can not use the buttons anymore. Please advise thank you. Please be aware this is the old GUI system.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HideCBInventory : MonoBehaviour {
    5.  
    6.     public GameObject Inventory;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void OnMouseDown () {
    15.    
    16.         Inventory.GetComponent<ModifyTerrain> ().enabled = ! Inventory.GetComponent<ModifyTerrain> ().enabled;
    17.     }
    18. }
    19.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    To do what you want, you can't change .enabled, since (as you found) the button will of course not work while it is not enabled.

    Instead you have to change the content — have the button and label both draw empty strings.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Worst case scenario with legacy you can always check Event.current and manually calculate if it's inside your invisible rect.
     
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    You could change the Alpha values to 0 so it will be not visible
     
    Kiwasi likes this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Should work.

    Some of the UI stuff somewhere automatically discarded clicks on transparent pixels. I cant remember if it was the legacy stuff or something else. Worth watching for if you take this approach.
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    BTW, to do this, create a new GUIStyle and pass it to GUI.Button. In your new GUIStyle's GUIStyleStates (normal, hover, etc.) set background to null and textColor to Color.clear.

    Or, before calling GUI.Button, save GUI.color and then set it to Color.clear. After GUI.Button set it back to the saved value.