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 /
  • Help Room /
avatar image
1
Question by jamesbean · May 04, 2021 at 03:07 AM · editorserializationpropertydrawergenerics

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

I'm doing some tests using generic variables to pass on data, with the following structure:

  • A regular, non-generic base class (to be able to create arrays and lists of the variables.

  • Derived from that, a generic class (which is the base for the variables).

  • Finally, a type-specific class, derived from the generic, to assign values.

Checking on the current documentation, I understood that the only way to show these variables in the inspector was using the [SerializeReference] attribute. And it kind of works:

Original Look

These variables use too much space on the screen, so I gave it a try to simplify it with a custom inspector. The following is the code I used, taking the hints from this post.

 // Generic class where the drawing is defined
 public class GVariablePropertyDrawer<T> : PropertyDrawer
     {
         public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
         {
             EditorGUI.BeginProperty(position, label, property);
 
             position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
             var indent = EditorGUI.indentLevel;
             EditorGUI.indentLevel = 0;
 
             var labelRect = new Rect(position.x, position.y, 38, position.height);
             var nameRect = new Rect(position.x + 38, position.y, position.width / 2 - 40, position.height);
             var valueRect = new Rect(position.x + position.width / 2, position.y, position.width / 2, position.height);
 
             GUIContent myLabel = new GUIContent("Name:");
             EditorGUI.LabelField(labelRect, myLabel);
             EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("variableName"), GUIContent.none);
             EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("DefaultValue"), GUIContent.none);
 
             EditorGUI.indentLevel = indent;
 
             EditorGUI.EndProperty();
         }
     }
 
 // Class-bound types for the editor
     [CustomPropertyDrawer(typeof(IntVariable))] public class GVariablePropertyDrawer_Int : GVariablePropertyDrawer<int> { }
     [CustomPropertyDrawer(typeof(FloatVariable))] public class GVariablePropertyDrawer_Float : GVariablePropertyDrawer<float> { }
     [CustomPropertyDrawer(typeof(BoolVariable))] public class GVariablePropertyDrawer_Bool : GVariablePropertyDrawer<bool> { }
     [CustomPropertyDrawer(typeof(TransformVariable))] public class GVariablePropertyDrawer_Transform : GVariablePropertyDrawer<Transform> { }
     [CustomPropertyDrawer(typeof(Vector2Variable))] public class GVariablePropertyDrawer_Vector2 : GVariablePropertyDrawer<Vector2> { }
     [CustomPropertyDrawer(typeof(Vector3Variable))] public class GVariablePropertyDrawer_Vector3 : GVariablePropertyDrawer<Vector3> { }
     [CustomPropertyDrawer(typeof(LayerMaskVariable))] public class GVariablePropertyDrawer_LayerMask : GVariablePropertyDrawer<LayerMask> { }

This is the new look: New lookt

Now, I think there is an improvement, but the top bar (where the origin of the class as a Base Class is hinted at -and in fact where the generic type can be chosen from) is still a big hit on readability, and I do not know how it could be possible to either remove it or modify it, so as to make it less intrusive.

Ideally, it would become a small tag to identify the variable type, maybe still fulfilling the function of "selector" for the derived type, placed where the "name" label currently is, so it can all fit within a single line.

Does anyone know if it can be done and how, or if it can't, why?

Thank you for your time and attention.

newlook.jpg (25.5 kB)
originallook.jpg (32.7 kB)
Comment
Add comment · Show 2
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 oscarAbraham · May 04, 2021 at 04:22 AM 1
Share

That top bar is not part of your property drawer. It's also not part of the default UnityEditor. Are you using some plugin or package to draw that list? Unity's included reorderable lists don't look like that, so maybe that's the code that is drawing that top bar. We need to see the code that draws the top bar in order to tell you how to change it. Can you share it?

On a separate note, you can save creating all those different property drawers by using:

 [CustomPropertyDrawer(typeof(GBaseVariable), true)]
 public class GVariablePropertyDrawer : PropertyDrawer

Or, if GBaseVariable is generic:

 [CustomPropertyDrawer(typeof(GBaseVariable<>), true)]
 public class GVariablePropertyDrawer : PropertyDrawer
avatar image jamesbean oscarAbraham · May 09, 2021 at 05:01 PM 0
Share

Hello, and thank you for the advice! You were right: that top bar was created by the Odin Serializer plugin - and thankfully they have a quick way to hide it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jamesbean · May 09, 2021 at 05:37 PM

As @oscarAbraham noted, that bar was not being generated by Unity. Turns out it was created by the Odin Serializer plugin - and thankfully there is a quick way to remove it using an attribute in the same plugin:

 [Serializable]
 [HideReferenceObjectPicker]
 public abstract class Variable<T> : BaseVariable
 // ...

With that, the selection bar is removed from the inspector.

I made another quick-and-dirty test to set up the name of the variable from the object type:

 // ...
  string varName = (typeof(T).ToString());
 varName = varName.Substring(varName.LastIndexOf('.') + 1);
 GUIContent myLabel = new GUIContent(varName);
 // ...

And the result looks like this: alt text


There's ways to make it prettier, but that's the answer I was looking for. Hopefully this will help someone else.


newerlook.jpg (28.4 kB)
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

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

How to create Generics Property Type Property Drawer 0 Answers

EditorWindow and serialization of scene objects 0 Answers

Property Drawer ArgumentException 1 Answer

Inspector popup list for custom enum-like class 0 Answers

Need quick editor scripting help 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