Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by halzate93 · Jan 05, 2016 at 09:17 PM · uilayoutcustom-inspectorenumpopuprepaint

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! :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Layout Group with Content Fitter? 0 Answers

How to make a Layout Groups child fit its content. 0 Answers

Is there anyway to make an input field stretch to fit a body of text? 0 Answers

Layout groups don't work 1 Answer

remove value from LayoutElement.preferredWidth by script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges