Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Flickayy · Dec 09, 2014 at 06:02 AM · c#guieditorfoldout

Create Multiple Foldouts.

Hi all, I'm trying to create a multiple group of foldout options, I've gotten close but some-things are messing up. Either they all open at once, or I get out of range exceptions. Can anyone help me out?

Here is my code. Thanks in advance!

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TrophyEditor : EditorWindow {
 
     TrophyList trophyDB;
     Vector2 scroll = Vector2.zero;
     List<bool> collapse = new List<bool>();
 
     [MenuItem("Tools/Trophy Editor")]
     static void Initialize() {
         var window = ScriptableObject.CreateInstance<TrophyEditor>();
         window.title = "Trophy Editor";
         window.ShowUtility();
     }
 
     void OnEnable() {
         trophyDB = Resources.Load<TrophyList>("Data/TrophyList");
         // If the trophy list is empty, initialize it
         if (trophyDB.trophiesList.Count == 0) {
             trophyDB.trophiesList.Add(new Trophy());
         }
     }
 
     void OnDestroy() {
         Save();
     }
 
     void Save() {
         AssetDatabase.Refresh();
         EditorUtility.SetDirty(trophyDB);
         AssetDatabase.SaveAssets();
     }
 
     void OnGUI() {
         GUILayout.BeginHorizontal(EditorStyles.toolbar);
         if (GUILayout.Button("Save", EditorStyles.toolbarButton)) {
             Save();
             EditorUtility.DisplayDialog("Save Asset", "The list of Trophies has been saved!", "Okay");
         }
         GUILayout.FlexibleSpace();
         GUILayout.EndHorizontal();
 
         EditorGUILayout.HelpBox("Welcome to the Trophy Editor.\n", MessageType.Info);
         
         EditorGUILayout.BeginHorizontal();
         EditorGUILayout.BeginVertical();
         scroll = EditorGUILayout.BeginScrollView(scroll);
 
         foreach (Trophy t in trophyDB.trophiesList) {
             for (int i = 0; i < collapse.Count; ++i) {
                 collapse[i] = EditorGUILayout.Foldout(collapse[i], "Trophy: " + t.title);
                 if (collapse[i]) {
                     GUILayout.Space(10);
                     t.trophyIcon = (Texture2D)EditorGUILayout.ObjectField("Icon", t.trophyIcon, typeof(Texture2D), false);
                     t.title = EditorGUILayout.TextField("Title", t.title);
                     t.grade = (TrophyGrade)EditorGUILayout.EnumPopup("Grade", t.grade);
                     t.details = EditorGUILayout.TextArea(t.details, GUILayout.Height(60));
                 }
             }
         }
 
         EditorGUILayout.EndScrollView();
         EditorGUILayout.EndVertical(); 
         EditorGUILayout.EndHorizontal();
 
         EditorGUILayout.BeginHorizontal();
         if (GUILayout.Button("Add")) {
             trophyDB.trophiesList.Add(new Trophy());
             collapse.Add(false);
         }
         if (GUILayout.Button("Remove")) {
             trophyDB.trophiesList.RemoveAt(trophyDB.trophiesList.Count);
             collapse.RemoveAt(collapse.Count);
         }
         EditorGUILayout.EndHorizontal();
     }
                              
 }
 
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

1 Reply

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

Answer by Bunny83 · Dec 09, 2014 at 06:31 AM

At the moment you have a nexted loop which doesn't make much sense as you want one boolean for each trophy. You should do something like this:

 // make sure we have as many booleans as we have trophies.
 while (collapse.Count < trophyDB.trophiesList.Count)
     collapse.Add(true); // expand by default
 
 for (int i = 0; i < trophyDB.trophiesList.Count; ++i)
 {
     Trophy t = trophyDB.trophiesList[i];
     collapse[i] = EditorGUILayout.Foldout(collapse[i], "Trophy: " + t.title);
     if (collapse[i])
     {
         GUILayout.Space(10);
         t.trophyIcon = (Texture2D)EditorGUILayout.ObjectField("Icon", t.trophyIcon, typeof(Texture2D), false);
         t.title = EditorGUILayout.TextField("Title", t.title);
         t.grade = (TrophyGrade)EditorGUILayout.EnumPopup("Grade", t.grade);
         t.details = EditorGUILayout.TextArea(t.details, GUILayout.Height(60));
         if (GUILayout.Button("Remove"))
         {
             trophyDB.trophiesList.RemoveAt(i);
             collapse.RemoveAt(i);
             EditorGUIUtility.ExitGUI(); // important to stop current GUI execution
         }
     }
 }

I've added the remove button to the trophy itself so you can remove arbitrary items.

Comment
Add comment · Show 2 · 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 Bunny83 · Dec 09, 2014 at 06:37 AM 0
Share

ps: Wouldn't it be more convinient to layout one trophy horizontally? I would probably go for:

 IIIIII TTTTTTTTTTTT DDDDDDDDDDDD  // I = image area
 IIIIII GGGGGGGGGGGG DDDDDDDDDDDD  // T = title
 IIIIII RRRRRRRRRRRR DDDDDDDDDDDD  // G = TrophyGrade dropdown
 IIIIII              DDDDDDDDDDDD  // R = remove button
                                   // D = details area

That wouldn't require foldouts at all ;)

avatar image Flickayy · Dec 09, 2014 at 06:15 PM 0
Share

The horizontal approach wasn't the way I wanted it to look aesthetically.

I used a for each as this approach worked at the time. Thanks for the help, going to try and implement it now ^^

EDIT:

Thanks, it works like a charm!

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

26 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

Related Questions

Handling Undo/REdo in EditorWindow when editing DB record. 0 Answers

Script work in editor but not in build 0 Answers

How do I display all editor window contentGUI in a single separateanother Editor window via script ?Window 1 Answer

Initialising List array for use in a custom Editor 1 Answer

How to fix ArgumentException? 3 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