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 OtsegoDoom · Mar 06, 2017 at 05:06 PM · inspectorenumcustom-inspectorcustom class

Inspector - Show/hide property within custom class using enum

Hi,

I've been digging around for quite awhile now trying to figure out a solution to this problem.

In the inspector, I have a custom class, Segments. Segments has a number of variables including an enum meant to select between a number of options. Based on what the enum is set to in the inspector, I want it to show or hide different options the user can access.

I've attached a screenshot to show what I'd like to see vs. what's actually happening.

alt text

Here's the code I'm using for the editor script:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 using System.Collections.Generic;
 
 [CustomEditor (typeof(NameGenerator))]
 public class NameGeneratorEditor : Editor {
 
     public override void OnInspectorGUI () {
 
         serializedObject.Update();
 
         EditorGUILayout.PropertyField(serializedObject.FindProperty("optionSource"));
         Generator.OptionSource optionSource = (Generator.OptionSource)serializedObject.FindProperty("optionSource").enumValueIndex;
 
         switch (optionSource) {
         case Generator.OptionSource.External:
             EditorGUILayout.PropertyField(serializedObject.FindProperty("optionList") );
             break;
 
         case Generator.OptionSource.Mixed:
             EditorGUILayout.PropertyField(serializedObject.FindProperty("optionList") );
             break;
         }
 
         EditorGUILayout.PropertyField(serializedObject.FindProperty("randomizeOnStart") );
         EditorGUILayout.PropertyField(serializedObject.FindProperty("segments"), true);
 
         SerializedProperty sp = serializedObject.FindProperty("segments");
         for (int x = 0; x < sp.arraySize; x++) {
             
             SerializedProperty subObject = sp.GetArrayElementAtIndex(x);
 
             Generator.SegmentSeparator segmentSeparator = (Generator.SegmentSeparator)subObject.FindPropertyRelative("useSeparator").enumValueIndex;
 
             switch (segmentSeparator) {
 
             case Generator.SegmentSeparator.Custom:

                 EditorGUILayout.PropertyField(subObject.FindPropertyRelative("separator") );
                 break;
             }
         }
 
         serializedObject.ApplyModifiedProperties();
     }
 }

Note: I've got a working version of this towards the top of the script with the OptionSource enum (only this one is not contained within a custom class".

The values for "Use Separator" are 'Custom', 'Default' and 'None'. When the enum is set to 'Custom' I want it to show the 'Separator' field for that item only. It's currently showing the proper field, only it's not showing it in the right spot.

I think the solution is somewhere within the "EditorGUILayout.PropertyField(subObject.FindPropertyRelative("separator") );" line, but I just don't know how to get there.

Any suggestions?

inspector-help.jpg (117.3 kB)
Comment
Add comment · Show 1
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 · Mar 07, 2017 at 12:15 AM 0
Share

I'm not sure if that's the actual problem, but i guess the problem is this line:

 Generator.SegmentSeparator segmentSeparator = (Generator.SegmentSeparator)subObject.FindPropertyRelative("useSeparator").enumValueIndex;

I'm pretty sure that "enumValueIndex" does not equal the enum value but only the index of the available enum values which you could get from Enum.GetValues. The index does not necessarily represent the actual value. Can you include your enum declaration?

Try "intValue" ins$$anonymous$$d. I'm not sure if it will return the actual value of an enum, but under the hood an enum is just an integer.

edit
Ohh, i just saw the "seperator" at the end of your third screenshot. ^^ Ok so the enum at least works as expected. I'll post an answer since i found your problem ^^.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Mar 07, 2017 at 12:32 AM

Ok the problem is that you iterate through the "segments" after all the segments have already been drawn. That's because you draw the segments in one line:

 EditorGUILayout.PropertyField(serializedObject.FindProperty("segments"), true);

This single PropertyField will process the complete array since you pass "true" as parameter. If you want to "insert" or "change" how the sub elements are drawn you have to iterate the properties "manually". A SerializedProperty acts like an iterator. You can use the Next or NextVisible method to get the next SerializedProperty. Note the parameter "enterChildren".

However, is your "separator" field actually serialized? If it is it should actually show up by default. The best approach to hide certain propertes conditionally is to simply skip the property if the condition is not right when you actually stumble on that property.

Note that when using Next / NextVisible you will basically iterate "line by line" of the original inspector. So the first "child" of "segments" would be the "Size" field of the array. The next one would be the "Element0" header and so on.

Comment
Add comment · 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

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

66 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 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

What is the efficient way of showing variables depend on enum in custom inspector? 2 Answers

Creating enum using a string array 3 Answers

Expand Component on Inspector 0 Answers

How do I change inspector values of other game objects using a custom Editor Window 0 Answers

Getting the selected enum value set in the Inspector 2 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