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
0
Question by BillyGraban · Jul 23, 2020 at 12:09 AM · editorinspectoreditor-scriptingeditorgui

CustomPropertyDrawer with UnityEvent

I am trying to work around the difficulties of inheritance and the Inspector. My current thought is to have a Custom Property Drawer for my type and a drop-down list that defines which data the Inspector shows. However I am getting some very strange results.

 // ActionList is a container of CompanyActions
 [System.Serializable]
 public class BaseAction : UnityEvent<ActionList> { }
 
 [System.Serializable]
 public class CompanyAction
 {
     public BaseAction action;
 }

 [CustomPropertyDrawer(typeof(CompanyAction))]
 public class CompanyActionDrawer : PropertyDrawer
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
         EditorGUI.BeginProperty(position, label, property);
         EditorGUI.PropertyField(position, property.FindPropertyRelative("action"));
         EditorGUI.EndProperty();
     }
 }

The final result is definitely broken. I cannot collapse any of the UnityEvent types, and the height of the PropertyDrawer is not being calculated correctly: alt text

Any ideas on where to go from here?

screen-shot-2020-07-22-at-50444-pm.png (49.1 kB)
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
0

Answer by WaqasHaiderDev · Jul 23, 2020 at 12:47 AM

I am total new (I don't consider my self even beginer) in editor scripting but recently I had to change some standard assets scripts to fit my needs. I saw there property drawers are used in some scripts e.g. waypoint circuit script. Below is that portion of the script.

 namespace UnityStandardAssets.Utility.Inspector
 {
 #if UNITY_EDITOR
     [CustomPropertyDrawer(typeof(WaypointCircuit.WaypointList))]
     public class WaypointListDrawer : PropertyDrawer
     {
         private float lineHeight = 18;
         private float spacing = 4;
 
 
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             EditorGUI.BeginProperty(position, label, property);
 
             float x = position.x;
             float y = position.y;
             float inspectorWidth = position.width;
 
             // Draw label
 
 
             // Don't make child fields be indented
             var indent = EditorGUI.indentLevel;
             EditorGUI.indentLevel = 0;
 
             //var items = property.FindPropertyRelative("itemsGo");
             //var titles = new string[] { "Waypoints", "", "", "" };
             //var props = new string[] { "waypoints", "^", "v", "-" };
             var widths = new float[] { .7f, .1f, .1f, .1f };
             float lineHeight = 18;
             //bool changedLength = false;
             //if (items.arraySize > 0)
             //{
             //    for (int i = 0; i < items.arraySize; i++)
             //    {
             //        var item = items.GetArrayElementAtIndex(i);
 
             //        float rowX = x;
             //        for (int n = 0; n < props.Length; ++n)
             //        {
             //            float w = widths[n] * inspectorWidth;
 
             //            // Calculate rects
             //            Rect rect = new Rect(rowX, y, w, lineHeight);
             //            rowX += w;
 
             //            if (i == -1)
             //            {
             //                EditorGUI.LabelField(rect, titles[n]);
             //            }
             //            else
             //            {
             //                if (n == 0)
             //                {
             //                    EditorGUI.ObjectField(rect, item.objectReferenceValue, typeof(GameObject), true);
             //                }
             //                else
             //                {
             //                    if (GUI.Button(rect, props[n]))
             //                    {
             //                        switch (props[n])
             //                        {
             //                            case "-":
             //                                items.DeleteArrayElementAtIndex(i);
             //                                items.DeleteArrayElementAtIndex(i);
             //                                changedLength = true;
             //                                break;
             //                            case "v":
             //                                if (i > 0)
             //                                {
             //                                    items.MoveArrayElement(i, i + 1);
             //                                }
             //                                break;
             //                            case "^":
             //                                if (i < items.arraySize - 1)
             //                                {
             //                                    items.MoveArrayElement(i, i - 1);
             //                                }
             //                                break;
             //                        }
             //                    }
             //                }
             //            }
             //        }
 
             //        y += lineHeight + spacing;
             //        if (changedLength)
             //        {
             //            break;
             //        }
             //    }
             //}
             //else
             //{
             //    // add button
             //    var addButtonRect = new Rect((x + position.width) - widths[widths.Length - 1] * inspectorWidth, y,
             //                                 widths[widths.Length - 1] * inspectorWidth, lineHeight);
             //    if (GUI.Button(addButtonRect, "+"))
             //    {
             //        items.InsertArrayElementAtIndex(items.arraySize);
             //    }
 
             //    y += lineHeight + spacing;
             //}
 
             // add all button
             var addAllButtonRect = new Rect(x, y, inspectorWidth, lineHeight);
             if (GUI.Button(addAllButtonRect, "Assign using all child objects"))
             {
                 var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit;
                 
                 if (circuit.waypointCubePrefab != null && circuit.waypointCubes.Count == 0)
                 {
 
                     var children = new GameObject[circuit.transform.childCount];
                     
                     for(int i=0; i < circuit.transform.childCount; i++)
                     {
                         children[i] = circuit.transform.GetChild(i).transform.gameObject;
                     }
                     //foreach (Transform child in circuit.transform)
                     //{
                     //    children[n++] = child;
                     //}
                     // Array.Sort(children, new TransformNameComparer());
                     int n = 0;
                     circuit.waypointList.itemsGo = new GameObject[children.Length];
                     for (n = 0; n < children.Length; ++n)
                     {
                         circuit.waypointList.itemsGo[n] = children[n].transform.gameObject;
                     }
                     circuit.ConvertWaypointsToCubes();
                 }
                 else
                 {
                     Debug.LogWarning("Either there are no way points or prefab is null or the Waypoints are already assigned.");
                 }
             }
             y += lineHeight + spacing;
 
             // show/hide cubes button
             var showHideCubesButtonRect = new Rect(x, y, inspectorWidth, lineHeight);
             if (GUI.Button(showHideCubesButtonRect, "Show/Hide Waypoint Cubes"))
             {
                 var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit;
                 circuit.ShowHideCubes();
             }
             y += lineHeight + spacing;
 
             // show/hide numbers button
             var showHideNumbersButtonRect = new Rect(x, y, inspectorWidth, lineHeight);
             if (GUI.Button(showHideNumbersButtonRect, "Show/Hide Waypoint Numbers"))
             {
                 var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit;
                 circuit.ShowHideWaypointNumbers();
             }
             y += lineHeight + spacing;
 
             // rename all button
             var renameButtonRect = new Rect(x, y, inspectorWidth, lineHeight);
             if (GUI.Button(renameButtonRect, "Auto Rename numerically from this order"))
             {
                 var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit;
                 int n = 0;
                 foreach (Transform child in circuit.waypointList.items)
                 {
                     child.name = "Waypoint " + (n++).ToString("000");
                 }
             }
             y += lineHeight + spacing;
 
             // correct Child Objects Rotation button
             var correctWaypointsRotationButton = new Rect(x, y, inspectorWidth, lineHeight);
             if (GUI.Button(correctWaypointsRotationButton, "Correct WayPoints Rotations"))
             {
                 var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit;
                 circuit.MakeWaypointsRotationAlongRoute();
             }
             y += lineHeight + spacing;
 
             // spawn prefab button
             var spawnPrefabButtonRect = new Rect(x, y, inspectorWidth, lineHeight);
             if (GUI.Button(spawnPrefabButtonRect, "Spawn Traffic Spawners Along Route"))
             {
                 var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit;
                 circuit.SpawnPrefabAlongRoutes();
             }
             y += lineHeight + spacing;
 
             // destroy mesh renderers button
             var destroyMeshRendererButtonRect = new Rect(x, y, inspectorWidth, lineHeight);
             if (GUI.Button(destroyMeshRendererButtonRect, "Remove Mesh Renderers from waypoints Permanently"))
             {
                 var circuit = property.FindPropertyRelative("circuit").objectReferenceValue as WaypointCircuit;
                 circuit.DestroyMeshRenderers();
             }
             y += lineHeight + spacing;
 
 
             // Set indent back to what it was
             EditorGUI.indentLevel = indent;
             EditorGUI.EndProperty();
         }
 
 
         public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
         {
             SerializedProperty items = property.FindPropertyRelative("items");
             float lineAndSpace = lineHeight + spacing;
             return 150 + (items.arraySize * lineAndSpace) + lineAndSpace;
         }
 
 
         // comparer for check distances in ray cast hits
         public class TransformNameComparer : IComparer
         {
             public int Compare(object x, object y)
             {
                 return ((Transform)x).name.CompareTo(((Transform)y).name);
             }
         }
     }
 #endif



There I faced similar issue when height was not adjusted properly and I changed the value (You can see there is written 150 near to bottom lines. Increasing this line helped me. As I said, I don't have much idea of all this but this helped me. And I was basically wanting to have buttons in the inspector so that I could run my monoscript functions in editor mode.. For that I found a Free tool BUTTON INSPECTON on unity asset store. You can go through the above example to check how it is adjusting height and using property drawer.

PS : I wanted to put this as comment instead of answer but unity answers did not let me exceed more than 3000 characters so I had to put it as answers.

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

184 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Custom Inspector Color Spacer 1 Answer

How to properly handle Undo events in custom inspector? 0 Answers

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

Get the highlighted variable in the inspector 0 Answers

Custom inspector editor - how to put new editor fields in a specific place 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