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 mattrified_ · Mar 03, 2014 at 06:56 PM · guieditorserializationpropertiespropertydrawer

Using DuplicateCommand and DeleteCommand with Properties

So I'm working with PropertyDrawers in Unity3D, and I feel like I have most things figured out except for the DuplicateCommand and DeleteCommand. By default, when serialized properties are placed into a list or array, Delete will remove the selected item from the list, and Ctrl+D will duplicate and insert a copy. However, when you create a PropertyDrawer for a class and use that class into an array or list, you lose this functionality. I've found a way to implement it, but it seems extremely "hacky". Here is my test sample. This first code is a MonoBehaviour that takes an array of strings, which work with delete and ctrl+D and an array of custom classes:

 using UnityEngine;
 using System.Collections;
 
 public class TestMono : MonoBehaviour {
     
     public string[] stringArray;
     
     public TestClass[] testClassArray;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

Here is the custom class:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable()]
 public class TestClass
 {
     public string id;
     public int index;
     public float fValue;
 }

Here is the property drawer for said class with my hack:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 [CustomPropertyDrawer(typeof(TestClass))]
 public class TestClassDrawer : PropertyDrawer 
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     { 
         int i = GUIUtility.GetControlID(FocusType.Keyboard);
         
         label = EditorGUI.BeginProperty(position, label, property);
         
         bool focus  = (i - GUIUtility.keyboardControl == -1);
         
         Debug.Log("Has focus:  " + focus);
         
         if (focus)
         {
             if (Event.current.commandName.Contains("Delete"))
             {
                 EditorGUI.EndProperty();
                 property.DeleteCommand();
                 return;
             }
         }
         
         Rect r = position;
         r.height = 16f;
         property.isExpanded = EditorGUI.Foldout(r, property.isExpanded, label, true);
         
         if (!property.isExpanded)
         {
             EditorGUI.EndProperty();
             return;
         }
         
         EditorGUI.indentLevel += 2;
         
         r.y += r.height;
         EditorGUI.PropertyField(r, property.FindPropertyRelative("id"));
         
         r.y += r.height;
         EditorGUI.PropertyField(r, property.FindPropertyRelative("index"));
         
         r.y += r.height;
         EditorGUI.PropertyField(r, property.FindPropertyRelative("fValue"));
         
         EditorGUI.indentLevel -= 2;
         
         EditorGUI.EndProperty();
     }
 
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         float finalSize = base.GetPropertyHeight(property, label);
         if (property.isExpanded)
         {
             finalSize += 48f;
         }
         return finalSize;
     }
 }


Essentially, I'm creating a keyboard control and getting the current keyboard control and if the difference between the created control and the current control is -1, meaning the control was created right before the keyboard focus, than to react appropriately when deleting or duplicating. Also, if I delete the last item in an array I get an error in the console but it seems relatively harmless. Overall, has anyone worked with PropertyDrawers in this case more and have any insight?

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

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

20 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

Related Questions

Select a readonly value from dropdown using PropertyDrawers 0 Answers

I have a custom editor for a class which work's fine, but how do i refrence it in another class and get it ti appear? 1 Answer

Caching data for a PropertyDrawer 1 Answer

How do I implement Draggable Properties with custom Labels in Editor or PropertyDrawer? 1 Answer

Trouble setting the object reference in a 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