Switch between HorizontalLayoutGroup and VerticalLayoutGroup using Custom Inspector Editor [UI] [Custom Editor]
Hi!,
I've been working on this custom inspector as part of a larger project. The core idea is to be able to switch from a horizontal layout to a vertical one an change width and heigth of the panel with said layouts.
The panel is used to display a bunch of small buttons which trigger other events, the number of buttons can change dynamically.
This far I've dealt with adding and replacing the components using a condition inside the OnInspectorGUI function. You can't destroy and add a Layout component in the same draw call, an exception is thrown saying a missing component is still trying to be accessed, so I check for the current Event to be EventType.Repaint to remove the component and then on the next one I add the correct Layout.
My problem is the enum field I'm using to store the current orientation resets it's value to the original one once the Layout component has been changed.
I've figured out I could set a timer or something like that and don't change the orientation for a while. But I find it rather hacky.
I tried using EditorGUIUtility.ExitGUI() withouth success either.
Here's the target:
using Brainztorm.Quest.Detail.Events;
using System.Collections.Generic;
using UnityEngine;
namespace Brainztorm.Quests.HUD
{
public class QuestHUD : MonoBehaviour
{
[SerializeField]
private GameObject questPrefab;
[SerializeField]
private QuestButton[] icons;
[SerializeField]
private int spaceIcons;
private Dictionary<string, QuestButton> views;
private Queue<QuestButton> availableViews;
private void Awake()
{
views = new Dictionary<string, QuestButton>();
availableViews = new Queue<QuestButton>();
foreach (QuestButton button in icons)
{
availableViews.Enqueue(button);
}
}
private void SetButtonCount(int count)
{
if (availableViews.Count < count)
AddButtons(count - availableViews.Count);
else if (availableViews.Count > count)
RemoveButtons(availableViews.Count - count);
}
private void AddButtons(int count)
{
for (int i = 0; i < count; i++)
{
GameObject instance = Instantiate(questPrefab);
instance.transform.SetParent(transform);
instance.transform.localScale = Vector3.one;
availableViews.Enqueue(instance.GetComponent<QuestButton>());
}
}
private void RemoveButtons(int count)
{
for (int i = 0; i < count; i++)
{
QuestButton button = availableViews.Dequeue();
Destroy(button.gameObject);
}
}
public void Display(QuestEvents[] quests)
{
Clean();
SetButtonCount(quests.Length);
foreach (QuestEvents quest in quests)
{
Display(quest.Data.Code, quest);
}
}
public void Clean()
{
foreach (string code in views.Keys)
{
Release(code);
}
views.Clear();
}
private void Release(string code)
{
QuestButton view;
if (views.TryGetValue(code, out view))
{
view.Clean();
availableViews.Enqueue(view);
}
}
public void Display(string code, QuestEvents quest)
{
if (!views.ContainsKey(code))
AllocateView(code);
views[code].Display(quest);
}
public void UpdateView(string code)
{
QuestButton view;
if (views.TryGetValue(code, out view))
view.ReDraw();
}
private void AllocateView(string code)
{
QuestButton view = availableViews.Dequeue();
views.Add(code, view);
}
}
}
And here's the custom inspector:
using Brainztorm.Quests.HUD;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
[CustomEditor(typeof(QuestHUD))]
public class QuestHUDEditor : Editor
{
private DisplayMode orientation;
private int spacing;
private SerializedProperty questPrefabProp;
private SerializedProperty iconsProp;
private HorizontalOrVerticalLayoutGroup layout;
[ExecuteInEditMode]
private void OnEnable()
{
questPrefabProp = serializedObject.FindProperty("questPrefab");
iconsProp = serializedObject.FindProperty("icons");
layout = (target as QuestHUD).GetComponent<HorizontalOrVerticalLayoutGroup>();
if (!layout)
SetLayout();
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(questPrefabProp);
EditorGUILayout.PropertyField(iconsProp);
UpdateSpacing();
serializedObject.ApplyModifiedProperties();
orientation = (DisplayMode)EditorGUILayout.EnumPopup("Orientation", orientation);
if (Event.current.type == EventType.Repaint)
{
DestroyIfIncorrectLayout();
if (layout == null)
SetLayout();
SetLayoutVariables();
}
serializedObject.ApplyModifiedProperties();
}
private void UpdateSpacing()
{
int spacing = EditorGUILayout.IntSlider("Spacing", this.spacing, 0, 30);
if (layout != null && spacing != this.spacing)
{
SetLayoutVariables();
this.spacing = spacing;
}
}
private void DestroyIfIncorrectLayout()
{
if (layout != null && OrientationDoesntMatchLayout())
{
DestroyImmediate(layout);
Repaint();
EditorGUIUtility.ExitGUI();
}
}
private bool OrientationDoesntMatchLayout()
{
return (orientation == DisplayMode.Vertical && layout is HorizontalLayoutGroup)
|| (orientation == DisplayMode.Horizontal && layout is VerticalLayoutGroup);
}
private void SetLayout()
{
GameObject gameObject = (target as QuestHUD).gameObject;
if (orientation == DisplayMode.Horizontal)
layout = gameObject.AddComponent<HorizontalLayoutGroup>();
else
layout = gameObject.AddComponent<VerticalLayoutGroup>();
Repaint();
}
private void SetLayoutVariables()
{
layout.spacing = spacing;
layout.childForceExpandHeight = false;
layout.childForceExpandWidth = false;
}
public enum DisplayMode
{
Vertical, Horizontal
}
}
Thanks in advance! :)