Search Unity

How to make partially selected EditorGUI.Toggle? [Solved]

Discussion in 'Immediate Mode GUI (IMGUI)' started by Skyblade, May 23, 2015.

  1. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    77
    I have nested group of toggles created with EditorGUILayout.ToggleLeft().
    I want to change state of the parent toggle to partial check if child toggles have mixed states, like here:

    Toggle.png

    But EditorGUILayout.ToggleLeft value is a boolean value, so how can I do that?
     
  2. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    Hey there,

    They don't use a bool value. First off they create an enum

    Code (CSharp):
    1.         public enum Amount
    2.         {
    3.             NotSet = -1,
    4.             None,
    5.             All,
    6.             Mixed
    7.         }
    Then they switched between editor styles based on the int value.

    Code (CSharp):
    1. {
    2.     bool flag = pitem.item.enabled > 0;
    3.     GUIStyle style = EditorStyles.toggle;
    4.     bool flag2 = pitem.isFolder && pitem.item.enabled == 2;
    5.  
    6.     if (flag2)
    7.     {
    8.         style = EditorStyles.toggleMixed;
    9.     }
    10.  
    11.     bool flag3 = GUI.Toggle(toggleRect, flag, GUIContent.none, style);
    12.  
    13.     if (flag3 != flag)
    14.     {
    15.             pitem.item.enabled = ((!flag3) ? 0 : 1);
    16.     }
    17. }
    From Unitys PackageImportTreeView.PackageImportTreeViewGUI class

    Cheers,
     
    codestage and Skyblade like this.
  3. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    77
    Wow, thanks for this info!

    However, I can not see EditorStyles.toggleMixed when writing my code :(
    It does exist in PackageImportTreeView.PackageImportTreeViewGUI decompiled class, but IntelliSense does not show it:

    ToggleMixed.png

    I'm using Unity 5.0.2f1.
    What did I miss?
     
  4. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    You are correct I did not look that it was internal. It's no big deal however since you can create styles based on a string value.
    Code (CSharp):
    1.     isActive = GUILayout.Toggle(isActive, "Toggle", "ToggleMixed");
    2.  
    3.     //Not well known fact. A GUIStyle can be created from a string.
    4.     new GUIStyle("ToggleMixed") == EditorStyles.toggleMixed == "ToggleMixed"
     

    Attached Files:

    codestage and Skyblade like this.
  5. Skyblade

    Skyblade

    Joined:
    Nov 19, 2013
    Posts:
    77
    Man, you are awesome!
    Could not learn such fine things without you. Thanks a lot!

    However, this could be done without hacks using EditorGUI.showMixedValue ;)
     
    Last edited: May 26, 2015
    codestage, Jean-Fabre and BMayne like this.
  6. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    Call me a code hipster :p