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
3
Question by GamezAtWork · May 10, 2017 at 10:14 AM · editorpropertydrawer

Nested Custom Property Drawers

Hi guys,

Does anybody know whether it is possible to have custom property drawers in custom property drawers, and if so, how?

Because when I tried doing so, the custom property inside the property displays the default version instead of my custom property drawer...

Case in point in attachment alt text In the above variable Command (which is of type Command), I drew that using my own Command CustomPropertyDrawer, so it looks different. But in the below variable Test (which is of type BaseTestClass), which has a Command variable, it uses the default ScriptableObject property drawer...

Edit Here is the snippet of the Command class and property drawer

Command.cs

     [System.Serializable]
     public class Command : ScriptableObject {
         public virtual string commandName{
             get { return this.GetType().Name;}
         }
         public Sprite uiImage;
     }


VirtualClassEditor.cs

 public abstract class VirtualClassEditor<BaseClass> : PropertyDrawer where BaseClass:ScriptableObject{
     
         List<string> allTypeStrings = new List<string>();
         List<Type> allTypes = new List<Type>();
     
         int currIndex = -1;
     
         public override float GetPropertyHeight (SerializedProperty property, GUIContent label) {
     
             if (property.objectReferenceValue != null)
             {
                 SerializedObject childObj = new UnityEditor.SerializedObject(property.objectReferenceValue);
                 SerializedProperty ite = childObj.GetIterator(); 
     
                 float totalHeight = EditorGUI.GetPropertyHeight (property, label, true) + EditorGUIUtility.standardVerticalSpacing;
     
                 while (ite.NextVisible(true))
                 {
                     totalHeight += EditorGUI.GetPropertyHeight(ite, label, true) + EditorGUIUtility.standardVerticalSpacing;
                 }
     
                 return totalHeight;
             }
             else
             {
                 return EditorGUI.GetPropertyHeight(property, label, true);
             }
         }
     
         public void UpdateTypeStrings(SerializedProperty property)
         {
             allTypeStrings = new List<string>();
             allTypes = Methods.GetSubclasses<BaseClass>(true);
             foreach(System.Type type in allTypes)
             {
                 allTypeStrings.Add(type.Name);
             }
     
             BaseClass actualVal = (BaseClass)property.objectReferenceValue;
             currIndex = -1;
             if (actualVal != null)
             {
                 for (int j = 0; j < allTypeStrings.Count; ++j)
                 {
                     if (allTypeStrings[j] == actualVal.GetType().Name)
                         currIndex = j;
                 }
             }
         }
     
         public void SetupClassProperty(Rect position, SerializedProperty property, GUIContent label)
         {
             label = EditorGUI.BeginProperty(position, label, property);
     
             Rect classRect = new Rect(position.x, position.y, position.width, EditorGUI.GetPropertyHeight(property));
     
             EditorGUI.BeginChangeCheck ();
     
             int newIndex = EditorGUI.Popup(classRect, property.displayName, currIndex, allTypeStrings.ToArray());
     
             if (EditorGUI.EndChangeCheck())
             {
                 property.objectReferenceValue = ScriptableObject.CreateInstance(allTypes[newIndex]);
                 Debug.Log("Changed to " + property.objectReferenceValue.GetType().Name);
             }
     
             EditorGUI.EndProperty();
         }
     
         public void SetupProperties(Rect position, SerializedProperty property, GUIContent label)
         {
             if (property.objectReferenceValue != null)
             {
                 EditorGUI.indentLevel += 1;
     
                 SerializedObject childObj = new UnityEditor.SerializedObject(property.objectReferenceValue);
     
                 SerializedProperty ite = childObj.GetIterator();
                 float prevHeight = EditorGUI.GetPropertyHeight(property, label, true);
     
                 while (ite.NextVisible(true))
                 {
                     Rect newRect = new Rect(position.x, position.y + prevHeight + EditorGUIUtility.standardVerticalSpacing, position.width, EditorGUI.GetPropertyHeight(ite, label, true));
                     prevHeight += newRect.height + EditorGUIUtility.standardVerticalSpacing;
                     EditorGUI.PropertyField(newRect, ite, true);
     
                 }
                 childObj.ApplyModifiedProperties();
             }
     
         }
     
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
     
             int oldIndent = EditorGUI.indentLevel;
     
             UpdateTypeStrings(property);
             SetupClassProperty(position, property, label);
             SetupProperties(position, property, label);
     
             EditorGUI.indentLevel = oldIndent;
     
         }
     
     }

CommandEditor.cs

 [CustomPropertyDrawer(typeof(Command))]
 public class CommandEditor: VirtualClassEditor<Command>{
 
 }


Another note is that it apparently works when I have an array of Commands, it just doesn't work if it is part of another class...

nestedcustompropertydrawers.png (47.5 kB)
Comment
Add comment · Show 3
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 Adam-Mechtley · May 10, 2017 at 12:55 PM 0
Share

Can you please share a code snippet of the definition for your Command custom PropertyDrawer? Basically just the class and the attributes on it.

avatar image GamezAtWork · May 10, 2017 at 03:07 PM 0
Share

Alright, I updated it with the Command class and the Command Property Drawer!

avatar image Adam-Mechtley · May 10, 2017 at 04:28 PM 0
Share

I'm not able to reproduce this with the following example. What does your property drawer look like for the class that contains Command as fields?

// NewBehaviourScript.cs

 using System;
 using UnityEngine;
 
 [Serializable]
 public class CommandContainer
 {
     public int someInt;
     public Command command;
 }
 
 public class NewBehaviourScript : $$anonymous$$onoBehaviour
 {
     public Command command;
 
     public CommandContainer commandContainer;
 }

//CommandContainerDrawer.cs

 using UnityEditor;
 using UnityEngine;
 
 [CustomPropertyDrawer(typeof(CommandContainer))]
 public class CommandContainerDrawer : PropertyDrawer
 {
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return EditorGUI.GetPropertyHeight(property, label);
     }
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.PropertyField(position, property, true);
     }
 }

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

115 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

Related Questions

Removing / modifying in Inspector a generic variable derived from a non-generic variable 1 Answer

Property Drawer ArgumentException 1 Answer

Need quick editor scripting help 1 Answer

Change the color of a variable field if it is empty 0 Answers

How to create Generics Property Type Property Drawer 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