• 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 HappyMoo · Dec 31, 2013 at 10:46 AM · guieditorinspectorpropertydrawer

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

For the sake of simplicity I will explain this on a simple example. I'm having a Monobehaviour or a custom [Serializable] class that holds multiple ints:

 class HoldingInts : MonoBehaviour {
     public int A;
     public int B;
     public int C;
 }

The default Inspector puts a label in front of those three properties that I can click and drag to change the number without having to type.

Now, these take a lot of space, so I want to have an Editor or PropertyDrawer that put them side by side, like the X,Y,Z components of a Vector3 for example, so I do this:

 GUIStyle overflowStyle = new GUIStyle();
 overflowStyle.clipping = TextClipping.Overflow;
 GUILayoutOption w8 = GUILayout.Width(8);
 GUILayoutOption w25 = GUILayout.Width(25);
 GUILayoutOption xw = GUILayout.ExpandWidth(true);
 
 EditorGUILayout.BeginHorizontal();
 EditorGUILayout.PrefixLabel("My Ints");
 EditorGUILayout.LabelField("A", overflowStyle, w8);
 EditorGUILayout.PropertyField(prop.FindPropertyRelative("A"), GUIContent.none, w25, xw);
 EditorGUILayout.LabelField("B", overflowStyle, w8);
 EditorGUILayout.PropertyField(prop.FindPropertyRelative("B"), GUIContent.none, w25, xw);
 EditorGUILayout.LabelField("C", overflowStyle, w8);
 EditorGUILayout.PropertyField(prop.FindPropertyRelative("C"), GUIContent.none, w25, xw);
 EditorGUILayout.EndHorizontal();

Which gives me what I want visually, but my custom labels are not connected to the properties, so I can't click and drag them to change the numbers.

Is it possible to connect those custom labels to the properties?

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 ArkaneX · Jan 03, 2014 at 01:58 AM

After some tinkering, I came to the following solution:

 [CustomPropertyDrawer(typeof(HoldingInts))]
 public class HoldingIntsInspector : PropertyDrawer
 {
     private float a = 17;
     public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
     {
         float width = position.width * 0.25f;
 
         Rect rect = new Rect(position);
         rect.width = width;
 
         EditorGUI.LabelField(rect, label);
         rect.x += width;
 
         float labelWidthTmp = EditorGUIUtility.labelWidth;
         EditorGUIUtility.labelWidth = 24f; // your label width (8) x3
         EditorGUI.PropertyField(rect, prop.FindPropertyRelative("A"), new GUIContent("A"));
         rect.x += width;
         EditorGUI.PropertyField(rect, prop.FindPropertyRelative("B"), new GUIContent("B"));
         rect.x += width;
         EditorGUI.PropertyField(rect, prop.FindPropertyRelative("C"), new GUIContent("C"));
 
         EditorGUIUtility.labelWidth = labelWidthTmp;
     }
 }

You will probably need to tune this a bit to get perfect effect, but this should be a decent starting point. I went PropertyDrawer way, as I never used it before, so I was able to learn something new as well ;)

Comment
Add comment · Show 1 · 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
avatar image HappyMoo · Jan 03, 2014 at 05:28 AM 0
Share

Oh wow... there's a static labelWidth... not even a GUIStyle or a List of GUILayoutOptions... just a float labelWidth...

The Design of this GUI system is scary.

Thanks a lot!

This is my current solution:

 EditorGUILayout.BeginHorizontal();
 EditorGUILayout.PrefixLabel("Segments");
 
 // keep the originals
 float lw = EditorGUIUtility.labelWidth;
 TextClipping tc = EditorStyles.label.clipping;
 RectOffset bord = EditorStyles.label.border;
 RectOffset pad = EditorStyles.label.padding;
 
 // alter values
 EditorGUIUtility.labelWidth = 8f;
 EditorStyles.label.clipping = TextClipping.Overflow;
 EditorStyles.label.border = new RectOffset(0, 0, 0, 0);
 EditorStyles.label.padding = new RectOffset(0, 0, 0, 0);
 
 EditorGUILayout.PropertyField(prop.FindPropertyRelative("xSegments"), new GUIContent("X"), w25, xw);
 EditorGUILayout.PropertyField(prop.FindPropertyRelative("ySegments"), new GUIContent("Y"), w25, xw);
 EditorGUILayout.PropertyField(prop.FindPropertyRelative("zSegments"), new GUIContent("Z"), w25, xw);
 
 // no one saw something
 EditorGUIUtility.labelWidth = lw;
 EditorStyles.label.clipping = tc;
 EditorStyles.label.border = bord;
 EditorStyles.label.padding = pad;
 
 EditorGUILayout.EndHorizontal();

And it's still a bit off. Looks like it's another debugging session to diff two GUIStyles manually.

But... they can be dragged!!! :D

Thanks again!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How do I prevent "Argument Exception: Getting control 1's position in a group with only 1 controls when doing repaint" in OnGUI function of my CustomPropertyDrawer? 2 Answers

Are there any Editor fields which are Drag and Drop? 0 Answers

How to add UnityEvent using Custom Inspector? 1 Answer

(Solution) - Can't use GUILayout stuff in PropertyDrawer.OnGUI? 2 Answers

EditorGUILayout.TextField text get's deleted instantly. 2 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