Laying out Toogles horizontally, extending GUI Layout Toggle

Hi,

I’m having some issues understanding what’s going on behind the surface, I’ve created a public Bool array for which I would like to override the normal way it’s presented, looking in the GUILayout section is a bit daunting or slightly skimpy on the examples, I wonder if anyone here would care to share their expertise.

At the moment i can’t even test, as ‘extends’ is flagged in red, so I can’t even get the example to work!

using UnityEngine;
using UnityEditor;
using System.Collections;

  class EditorGUILayoutToggle extends EditorWindow {

    var showHorizontal : boolean = true;
    var showBtn : boolean = true;

    @MenuItem("Examples/Editor GUILayout Toggle Usage")

    static function Inint(){
        var window = GetWindow(EditorGUILayoutToggle);
        window.Show();
    }

    function OnGUI(){
        showBtn = EditorGUILayout.Toggle("Show Button", showBtn);
        if (showBtn)
        if(GUILayout.Button"Close"))
            this.Close();
    }

    // for each button in array layout horizontally
    // for (int i = 0; i < numBtns; i++){
    //        Will GUILayoutOption.ExpandHeight(false) work?}
}

I’m wondering if setting GUILayoutOption.ExpandHeight to false would work, but I haven’t been able get my head around this, whether to iterate for each button or whether there’s no need as the extending the editor class would do for each item in an array already.

If any one’s got any ideas I’m all ears!

This is actually a fairly involved task.

Still drawing vertically for some reason even though there’s Begin Horizontal and end Horizontal DAMN it!

using UnityEngine;
using UnityEditor;
using System.Collections;

public static class EditorList {

    public static void Show (SerializedProperty list, EditorListOption options = EditorListOption.Default)
    {
        bool
        showListLabel = (options & EditorListOption.ListLabel) != 0,
        showListSize = (options & EditorListOption.ListSize) != 0;
       
        if (showListLabel){

        EditorGUILayout.PropertyField (list);
        EditorGUI.indentLevel += 1;
        }

        if (!showListLabel || list.isExpanded) {
            if (showListSize) {
                EditorGUILayout.PropertyField (list.FindPropertyRelative ("Array.size"));
            }
            ShowElements(list, options);
        }
            if (showListLabel) {
                EditorGUI.indentLevel -= 1;
            }
    }

    private static void ShowElements(SerializedProperty list, EditorListOption options)
    {
            bool showElementLabels = (options & EditorListOption.ElementLabels) != 0,
                 showButtons = (options & EditorListOption.Buttons) != 0;

            for (int i = 0; i < list.arraySize; i++) {

            if (showButtons) {
                EditorGUILayout.BeginHorizontal ();
            }
            if (showElementLabels) {
                EditorGUILayout.PropertyField (list.GetArrayElementAtIndex (i));
            } else
                {
                EditorGUILayout.PropertyField (list.GetArrayElementAtIndex (i), GUIContent.none);
                }
            if (showButtons)
                {
                ShowButtons ();
                EditorGUILayout.EndHorizontal ();
                }
            }
        }

    private static GUILayoutOption miniButtonSize = GUILayout.Width(20f);

    private static GUIContent buttonLabel = new GUIContent("+");

    private static void ShowButtons()
    {
       
        GUILayout.Button(buttonLabel, EditorStyles.miniButton, miniButtonSize);
    }
}

and

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(EditorToggle))]

public class EditorToggleInspector : Editor {

    public override void OnInspectorGUI()
    {
        serializedObject.Update ();
        //EditorGUILayout.PropertyField (serializedObject.FindProperty ("bools"), EditorListOption.Buttons);
        EditorList.Show (serializedObject.FindProperty("bools"), EditorListOption.Buttons);
        EditorList.Show(
            serializedObject.FindProperty("objects"),
            EditorListOption.ListLabel | EditorListOption.Buttons);
        serializedObject.ApplyModifiedProperties ();
    }
}

and

using UnityEngine;
using UnityEditor;
using System;
using System.Collections;

[Flags]
public enum EditorListOption {

    None = 0,
    ListSize = 1,
    ListLabel = 2,
    ElementLabels = 4,
    Buttons = 8,
    Default = ListSize | ListLabel | ElementLabels,
    NoElementLabels = ListSize | ListLabel,
    All = Default | Buttons
}

My apologies but I’m so close, my super ghetto solution:

using UnityEditor;
using UnityEngine;
using System;

[Flags]
public enum EditorListOption {
    None = 0,
    ListSize = 1,
    ListLabel = 2,
    ElementLabels = 4,
    Buttons = 16,
    Toggles = 16,
    Default = ListSize | ListLabel | ElementLabels,
    NoElementLabels = ListSize | ListLabel,
    All = Default | Buttons
}

public static class EditorList {

    private static GUIContent

        buttonContent = new GUIContent("+");

    private static GUILayoutOption miniButtonWidth = GUILayout.Width(20f);

    public static void Show (SerializedProperty list, EditorListOption options = EditorListOption.Default) {
        if (!list.isArray) {
            EditorGUILayout.HelpBox(list.name + " is neither an array nor a list!", MessageType.Error);
            return;
        }

        bool
            showListLabel = (options & EditorListOption.ListLabel) != 0,
            showListSize = (options & EditorListOption.ListSize) != 0;

        if (showListLabel) {
            EditorGUILayout.PropertyField(list);
            EditorGUI.indentLevel += 1;
        }
        if (!showListLabel || list.isExpanded) {
            SerializedProperty size = list.FindPropertyRelative("Array.size");
            if (showListSize) {
                EditorGUILayout.PropertyField(size);
            }
            if (size.hasMultipleDifferentValues) {
                EditorGUILayout.HelpBox("Not showing lists with different sizes.", MessageType.Info);
            }
            else {
                ShowElements(list, options);
            }
        }
        if (showListLabel) {
            EditorGUI.indentLevel -= 1;
        }
    }

    private static void ShowElements (SerializedProperty list, EditorListOption options) {
        bool
            showElementLabels = (options & EditorListOption.ElementLabels) != 0,
            showButtons = (options & EditorListOption.Buttons) != 0;

        for (int i = 0; i < list.arraySize; i++) {
            if (showButtons) {
                EditorGUILayout.BeginHorizontal();
            }
            if (showButtons) {
                ShowButtons(list, i);
                EditorGUILayout.EndHorizontal();
            }
        }
        if (showButtons && list.arraySize == 0 && GUILayout.Toggle(false,buttonContent, EditorStyles.miniButton)) {
            list.arraySize += 1;
        }
    }

    private static void ShowButtons (SerializedProperty list, int index) {


        if (GUILayout.Toggle(true, buttonContent, EditorStyles.miniButtonLeft, miniButtonWidth)) {
            //list.MoveArrayElement(index, index + 1);
        }
        if (GUILayout.Toggle(true, buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true, buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true, buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true, buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true,buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true,buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true,buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true,buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true,buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true,buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true,buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true,buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true,buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true,buttonContent, EditorStyles.miniButtonMid, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        if (GUILayout.Toggle(true,buttonContent, EditorStyles.miniButtonRight, miniButtonWidth)) {
            //list.InsertArrayElementAtIndex(index);
        }
        }
}
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(ListTester)), CanEditMultipleObjects]
public class ListTesterInspector : Editor {

    public override void OnInspectorGUI () {
        serializedObject.Update();
        EditorList.Show(serializedObject.FindProperty("bools"), EditorListOption.Toggles | EditorListOption.ListLabel);
        EditorList.Show(serializedObject.FindProperty("notAList"));
        serializedObject.ApplyModifiedProperties();
    }
}

And a little better, just can’t work out how to access the damn buttons now, it looks like I’ve switched them off altogether now.

using UnityEditor;
using UnityEngine;
using System;

[Flags]
public enum EditorListOption {
    None = 0,
    ListSize = 1,
    ListLabel = 2,
    ElementLabels = 4,
    Buttons = 16,
    Toggles = 16,
    Default = ListSize | ListLabel | ElementLabels,
    NoElementLabels = ListSize | ListLabel,
    All = Default | Buttons
}

public static class EditorList {

    private static GUIContent

        buttonContent = new GUIContent("+");

    private static GUILayoutOption miniButtonWidth = GUILayout.Width(20f);

    public static void Show (SerializedProperty list, EditorListOption options = EditorListOption.Default) {
        if (!list.isArray) {
            EditorGUILayout.HelpBox(list.name + " is neither an array nor a list!", MessageType.Error);
            return;
        }

        bool
            showListLabel = (options & EditorListOption.ListLabel) != 0,
            showListSize = (options & EditorListOption.ListSize) != 0;

        if (showListLabel) {
            EditorGUILayout.PropertyField(list);
            EditorGUI.indentLevel += 1;
        }
        if (!showListLabel || list.isExpanded) {
            SerializedProperty size = list.FindPropertyRelative("Array.size");
            if (showListSize) {
                EditorGUILayout.PropertyField(size);
            }
            if (size.hasMultipleDifferentValues) {
                EditorGUILayout.HelpBox("Not showing lists with different sizes.", MessageType.Info);
            }
            else {
                ShowElements(list, options);
            }
        }
        if (showListLabel) {
            EditorGUI.indentLevel -= 1;
        }
    }

    private static void ShowElements (SerializedProperty list, EditorListOption options) {
        bool
            showElementLabels = (options & EditorListOption.ElementLabels) != 0,
            showButtons = (options & EditorListOption.Buttons) != 0;

        for (int i = 0; i < list.arraySize; i++) {
            if (showButtons) {
                EditorGUILayout.BeginHorizontal();
            }
            if (showButtons) {
                ShowButtons(list, i, false, numberOfSlots:16);
                EditorGUILayout.EndHorizontal();
            }
        }
        if (showButtons && list.arraySize == 0 && GUILayout.Toggle(false, buttonContent, EditorStyles.miniButton)) {
            list.arraySize += 1;
        }
    }

    private static void ShowButtons (SerializedProperty list, int index, bool nOff, int numberOfSlots) {

        for (int i = 0; i < numberOfSlots; i++) {

            if (GUILayout.Toggle (nOff, buttonContent, miniButtonWidth))
                {
                //What the???
                }

            }
        }
}