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
2
Question by nikita-kubiki · Sep 23, 2019 at 08:00 PM · inspectoreditorguipropertiespropertydrawer

How to get the target object value from a PropertyDrawer for an array of objects?

I have a serializable class. Something like:

 [Serializable]
 public class Data
 {
     public Category Category;
     public int Size1D;
     public Vector3 Size3D;
 }
 
 public enum Category
 {
     Cat1=0,
     Cat2=1
 }

I want to be able to describe an array of such objects inside a MonoBehaviour via inspector

 public class Definition : MonoBehaviour
 {
     public Data[] Data;
 }

in such a way that for each array element, Size1D and Size3D properties are available for editing based on Category value, i.e. if Category==Cat1 then Size1D is available for editing in the inspector and Size3D is not and vice versa for Category==Cat2. In order to do this I implemented a PropertyDrawer for Data class:

 [CustomPropertyDrawer(typeof(Data))]
 public class DataPropertyDrawer : PropertyDrawer
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         // some GUI stuff here
     }
 }

The problem appears when I try to condition the GUI based on Category. In order to do this I need to retrieve the Category value. As I understand the way to do this is by using an expression like this

 fieldInfo.GetValue(property.serializedObject.targetObject)

inside OnGUI. I expected it to return the corresponding Data object, but it returns Data[], i.e. an array of Data objects that MonoBehaviour property is assigned to. This seems confusing to me since OnGUI renders stuff for each of the Data objects, but the corresponding target value is array and not the corresponding element.

I suspect I could go another way and hide Data[] inside some other class and write a PropertyDrawer for it instead, but this seems like a much more complex solution. Is there a way to implement a PropertyDrawer for Data class that would have the desired behaviour?

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 WarmedxMints · Sep 23, 2019 at 08:24 PM

You need to get the enum value of the category and then display depending on it. You need to find the relative property.

 using UnityEditor;
 using UnityEngine;
 
 [CustomPropertyDrawer(typeof(Data))]
 public class DataPropertyDrawer : PropertyDrawer
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.BeginProperty(position, label, property);
 
         var indent = EditorGUI.indentLevel;
         EditorGUI.indentLevel = 0;
 
         var carRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
         var secondRect = new Rect(position.x, position.y + 20f, position.width, EditorGUIUtility.singleLineHeight);
 
         var category = property.FindPropertyRelative("Category");
 
         category.intValue = EditorGUI.Popup(carRect, "Catergory", category.intValue, category.enumNames);
 
         switch ((Category)category.intValue)
         {
             case Category.Cat1:
                 var size1d = property.FindPropertyRelative("Size1D");
                 size1d.intValue = EditorGUI.IntField(secondRect, "Size1D", size1d.intValue);
                 break;
             case Category.Cat2:
                 var size3d = property.FindPropertyRelative("Size3D");
                 size3d.vector3Value = EditorGUI.Vector3Field(secondRect, "Size3D", size3d.vector3Value);
                 break;
         }
 
         EditorGUI.indentLevel = indent;
 
         EditorGUI.EndProperty();
     }
 
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return (20 - EditorGUIUtility.singleLineHeight) + (EditorGUIUtility.singleLineHeight * 2);
     }
 }
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

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

Property Drawer and Children 3 Answers

How to get width at which Inspector does line breaks (for Vectors) 1 Answer

How to show type text in EditorGUI.ObjectField? 1 Answer

Inspector Overlapping Text Label at a Position 1 Answer

Draw on Multiple Lines with EditorGUI 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