I’ve made a custom GUILayout option for an array of bools so that they lie horizontal, I now have no function of the toggle button, just turning it on and off. I suspect I’ve been a fool somewhere and locked it out of use, but I was struggling with this till late last night pulling out my hair!
The important part I think I’m messing up is:
public static void ShowButtons (SerializedProperty list, int index, int numberOfSlots) {
bool[] nOff = new bool[numberOfSlots];
for (int i = 0; i < numberOfSlots; i++) {
if (GUILayout.Toggle(nOff[i], buttonContent, miniButtonWidth))
{
if (nOff [i] != true) {
nOff[i] = true;
Debug.Log (nOff[i]);
}
else
{
nOff[i] = false;
}
Debug.Log(index); // The Row
Debug.Log(i); // The Collumn
}
}
}
But I can’t actually turn on and off the bools in the inspector, when I debug I see each button knows it’s own row and column, it’s just forgotten how to turn itself on and off. What am I missing, I’m going slightly crazy tracking this down!
Here’s the whole thing:
using UnityEditor;
using UnityEngine;
using System;
[Flags]
public enum EditorListOption {
None = 0,
ListSize = 1,
ListLabel = 2,
ElementLabels = 4,
NumSlots = 16,
Buttons = 16,
Toggles = 16,
Default = ListSize | ListLabel | ElementLabels,
NoElementLabels = ListSize | ListLabel,
All = Default | Buttons | NumSlots
}
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;
//numSlots =
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, numberOfSlots:16);
EditorGUILayout.EndHorizontal();
}
}
if (showButtons && list.arraySize == 0 && GUILayout.Toggle(true, buttonContent, EditorStyles.miniButton)) {
list.arraySize += 1;
}
}
public static void ShowButtons (SerializedProperty list, int index, int numberOfSlots) {
bool[] nOff = new bool[numberOfSlots];
for (int i = 0; i < numberOfSlots; i++) {
if (GUILayout.Toggle(nOff[i], buttonContent, miniButtonWidth))
{
if (nOff [i] != true) {
nOff[i] = true;
Debug.Log (nOff[i]);
}
else
{
nOff[i] = false;
}
Debug.Log(index); // The Row
Debug.Log(i); // The Collumn
}
}
}
}
and
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(ScriptName)), CanEditMultipleObjects]
//[CustomEditor(typeof(SamplerLoops))]
public class ListTesterInspector : Editor {
public override void OnInspectorGUI () {
serializedObject.Update();
EditorList.Show(serializedObject.FindProperty("bools"), EditorListOption.Toggles | EditorListOption.ListSize);
//EditorList.Show(serializedObject.FindProperty("loops1"), EditorListOption.Toggles | EditorListOption.ListSize);
serializedObject.ApplyModifiedProperties();
}
}
If anyone could help I would be insanely grateful.