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. Dismiss Notice

Understanding GUILayout.Toggle parameters

Discussion in 'Immediate Mode GUI (IMGUI)' started by Johker, Oct 15, 2010.

  1. Johker

    Johker

    Joined:
    Jun 24, 2010
    Posts:
    24
    Hi Guys,

    I have the following piece of code and I am unsure why it is not working as expected. I want to assign a custom texture for the different GUILayout.Toggle states, which works perfectly as long as I don't assign any additional GUILayoutOption parameters. I thought assigning additional GUILayoutOption parameters will only override settings defined by the GUIStyle, and not the texture?

    The following code correctly uses the custom textures :
    Code (csharp):
    1.  
    2. public class MainMenuScreen : MonoBehaviour
    3. {
    4.     public Texture2D checkOn;
    5.     public Texture2D checkOff;
    6.     private bool mySetting = false;
    7.  
    8.     private bool SettingsToggle(bool isChecked, string name)
    9.     {
    10.         GUILayout.BeginVertical();
    11.     GUILayout.Space(10);
    12.     isChecked = GUILayout.Toggle (isChecked, isChecked ? checkOn : checkOff);
    13.     GUILayout.EndVertical();
    14.     GUILayout.Space(10);
    15.     GUILayout.Label(name);
    16.     }
    17.  
    18.     private void DrawSettings()
    19.     {
    20.         mySetting = SettingsToggle(mySetting, "Test Setting");
    21.     }
    22. }
    23.  
    When I add the following parameters to this line of code, the default GUISkin gets used and I lose my custom textures. Why?

    Code (csharp):
    1.  
    2.     isChecked = GUILayout.Toggle (isChecked, isChecked ? checkOn : checkOff, GUILayout.Width(15f), GUILayout.Height(100f));
    3.  
    Can anyone shed some light on this?
     
  2. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    Since you're neither setting GUI.skin nor passing a GUIStyle to GUILayout.Toggle(), you're going to get the default skin in either case. The texture you pass to GUILayout.Toggle() is content; it doesn't affect how the toggle is styled. I'm not sure what you're trying to achieve, but if you want a different rendering of on/off state for the checkbox part of a toggle, you need to do that through a custom GUIStyle rather than passing textures into GUILayout.Toggle().
     
    louisdimmockscopely likes this.
  3. zXSmashXz

    zXSmashXz

    Joined:
    Nov 2, 2014
    Posts:
    15
    I have a somewhat similar question, I am using this piece of code in a tutorial that I am using.

    Code (csharp):
    1.  
    2. void OnGUI(){
    3.         isMageClass = GUILayout.Toggle (isMageClass, "Mage Class");
    4.         isWarriorClass = GUILayout.Toggle (isWarriorClass, "Warrior Class");
    5.         if(GUILayout.Button ("Create")){
    6.             if(isMageClass){
    7.                 newPlayer.PlayerClass = new BaseMageClass();
    8.             }else if(isWarriorClass){
    9.                 newPlayer.PlayerClass= new BaseWarriorClass();
    10.             }
    11.  
    Can someone show me an example of where to add the location of this GUILayout.Toggle();?
     
  4. zXSmashXz

    zXSmashXz

    Joined:
    Nov 2, 2014
    Posts:
    15
    Wow, just realized how old this post is :(

    Okay I think I just figured it out,
    Code (csharp):
    1.  
    2. isMageClass = GUI.Toggle (new Rect(10,10,100,30), isMageClass, "Mage Class");
    3.  
     
    Last edited: Nov 6, 2014