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 /
avatar image
0
Question by onuryurdupak · Sep 05, 2016 at 04:17 AM · editor-scriptingbuttonscustom editorcustom-inspectoreditorguilayout

Custom Inspector: Making Individual Vertical Containers and Dynamically Adding & Removing Child Elements

TL;DR: How can one make individual containers that display their child elements vertically inside them?

(I.e: Array of elements shown as menu items)

Hello everybody,

In the example (provided in attachment), what I am trying to achieve is to dynamically add and remove buttons to corresponding "lists". From enum popup, I make a selection and click "Add", it is supposed to add to red, yellow or green list based on selection. "Some Button"s created in the code are where they are supposed to be. But adding buttons via loop like I did in the code well, script adds new buttons after Red List label, no matter the current selection is.

I tried working with some examples I found online. Tried using EditorGUILayout.BeginVertical(), and EditorGUILayoutEndVertical(). Couldn't make much sense about using them.

Main question: How can properly group these dynamically added & removed buttons?

 using UnityEditor;
 using UnityEngine.SceneManagement;
 using UnityEditor.SceneManagement;
 using System.Collections;
 using System.Collections.Generic;
 
 [CustomEditor(typeof(SomeScript))]
 [CanEditMultipleObjects]
 public class SomeScriptCustomInspector : Editor
 {
     public SScript.LineItem LineItemType;
    
     public override void OnInspectorGUI()
     {
         SomeScript sScript = (SomeScript)target;
 
         #region StaticSection
         if (GUILayout.Button("Add New"))
         {
             TriggerEditorLibrary.AddNewLine(sScript, LineItemType);
         }
 
         LineItemType = (SScript.LineItem)EditorGUILayout.EnumPopup(LineItemType);
 
         if (GUILayout.Button("Delete All"))
         {
             sScript.ScriptLines.Clear();
         }
         #endregion
 
         #region Red
         GUILayout.Label("Red List");
         GUI.color = new Color(255, 0, 0);
 
         GUILayout.Button("Some Button");
         GUILayout.Button("Some Button");
 
         if (sScript.ScriptLines.Count != 0)
         {
             foreach (SScript element in sScript.ScriptLines)
             {
                 if (element.LineItemType == SScript.LineItem.Red)
                 {
                     GUILayout.Button(element.Name);
                 }
             }
         }
         #endregion
 
         #region Yellow
         GUILayout.Label("Yellow List");
         GUI.color = new Color(255, 255, 0);
 
         GUILayout.Button("Some Button");
         GUILayout.Button("Some Button");
 
         if (sScript.ScriptLines.Count != 0)
         {
             foreach (SScript element in sScript.ScriptLines)
             {
                 if (element.LineItemType == SScript.LineItem.Yellow)
                 {
                     GUILayout.Button(element.Name);
                 }
             }
         }
         #endregion
 
         #region Green
         GUILayout.Label("Green List");
         GUI.color = new Color(0, 255, 0);
 
         GUILayout.Button("Some Button");
         GUILayout.Button("Some Button");
 
         if (sScript.ScriptLines.Count != 0)
         {
             foreach (SScript element in sScript.ScriptLines)
             {
                 if (element.LineItemType == SScript.LineItem.Green)
                 {
                     GUILayout.Button(element.Name);
                 }
             }
         }
         #endregion
 
         if (GUI.changed)
         {
             EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
         }
     }



 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class SScript
 {
     public string Name = "";
     public enum LineItem { Red, Yellow, Green }
     public LineItem LineItemType;
 }
 
 public class SomeScript : MonoBehaviour
 {
 
     public List<SScript> ScriptLines;
 }



 using UnityEditor;
 using System.Collections;
 
 public static class TriggerEditorLibrary
 {

     public static void AddNewLine(Trigger target, ScriptLine.LineItem lineItemType)
     {
         ScriptLine newScriptLine = new ScriptLine();
         newScriptLine.Name = "New " + lineItemType.ToString();
         target.ScriptLines.Add(newScriptLine);
     }
 }

listexample.jpg (85.6 kB)
Comment
Add comment · Show 4
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
avatar image LTK · Sep 05, 2016 at 09:23 AM 0
Share

You can show log list sScript.ScriptLines?

avatar image onuryurdupak LTK · Sep 05, 2016 at 06:07 PM 0
Share

If you mean whether I can debug.log ScriptLines for checking validity of them, then yes. Also I've added SScript code to my post as well.

avatar image Glurth · Sep 05, 2016 at 06:17 PM 1
Share
 if (GUILayout.Button("Add New"))
          {
              TriggerEditorLibrary.AddNewLine(sScript, LineItemType);
          }
  
          LineItemType = (SScript.LineItem)EditorGUILayout.EnumPopup(LineItemType);
          ...
 }

I guess, it might not matter across multiple updates, but it seems odd to me that you are adding the new line, via the TriggerEditorLibraryClass, BEFORE you get the current LineItemType.

I would call that function after getting the LineItemType (store the button return value in a bool, and check that bool and if true, call the TriggerEditorLibraryClass function, AFTER the EnumPopup line)

I think we will need to see the function TriggerEditorLibrary.AddNewLine(sScript, LineItemType); Other than the above possible-issue, the posted GUI code doesn't seem to explain why new items always get added to "red".

avatar image onuryurdupak Glurth · Sep 06, 2016 at 08:15 AM 0
Share

I've attached the piece of code that is TriggerEditorLibrary to the end of my post just now.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Sep 06, 2016 at 08:25 AM

Your static AddNewLine method does not set the new object's LineItemType. You need this line as well:

 newScriptLine.LineItemType = lineItemType;
Comment
Add comment · Show 1 · Share
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
avatar image onuryurdupak · Sep 06, 2016 at 07:40 PM 0
Share

Thank you for pointing it out :) Though It was a shame that I had to waste time of people with such a trivial mistake.

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

6 People are following this question.

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

Related Questions

Edit an object in isolation quickly, as in the new Prefab Mode 0 Answers

Update custom editor based on choosen options 0 Answers

How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers

Using EditorGUILayout in my PropertyDrawer for an attribute causes ArgumentException 1 Answer

How can i create a button in a custom class with UnityEditor using custom editor? 0 Answers


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