I made a simple little custom attribute that I use to place short fields or checkboxes in the same line in the editor window.
It’s an easy way to quickly save space without having to write a full blown custom editor.
But… it doesn’t retain the Tooltip.
Is there an easy way to do this?
Here is usage and code:
[Split1(true, 50)]
[Tooltip("Yadda yadda yadda.")]
public bool alpha;
[Split2(true, 50, 18f)]
[Tooltip("Yadda yadda yadda.")]
public bool beta;
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomPropertyDrawer (typeof (Split1Attribute))]
public class Split1Drawer : PropertyDrawer {
public override void OnGUI ( Rect position, SerializedProperty property, GUIContent label ) {
Split1Attribute Attribute = (Split1Attribute)attribute;
float percent = Attribute.splitPercent * .01f;
float padding = 5f; // space on either side of split line
float split = ( position.width * percent ) - padding;
float length = (Attribute.showLabel == true) ? split * .5f : split;
float height = GetPropertyHeight( property, GUIContent.none );
Rect labelRect = new Rect ( position.x, position.y, length, height );
Rect valueRect = new Rect ( position.x + split - length, position.y, length, height );
if ( Attribute.showLabel == true ) {
EditorGUI.LabelField( labelRect, label.text, EditorStyles.label );
}
EditorGUI.PropertyField( valueRect, property, GUIContent.none );
}
}
[CustomPropertyDrawer (typeof (Split2Attribute))]
public class Split2Drawer : PropertyDrawer {
public override float GetPropertyHeight ( SerializedProperty property, GUIContent label ) {
return -2f; // remove vertical padding and don't create a new line
}
public override void OnGUI ( Rect position, SerializedProperty property, GUIContent label ) {
Split2Attribute Attribute = (Split2Attribute)attribute;
float percent = Attribute.splitPercent * .01f;
float padding = 5f; // space on either side of split line
float split = ( position.width * percent ) - padding;
float invertSplit = ( position.width * (1f - percent) ) - padding;
float invertLength = (Attribute.showLabel == true) ? invertSplit * .5f : invertSplit;
float start = position.x + split + ( padding * 2f );
Rect labelRect = new Rect ( start, position.y - Attribute.height, invertLength, Attribute.height-2f );
Rect valueRect = new Rect ( start + invertSplit - invertLength, position.y - Attribute.height, invertLength, Attribute.height-2f );
if ( Attribute.showLabel == true ) {
EditorGUI.LabelField( labelRect, label.text, EditorStyles.label );
}
EditorGUI.PropertyField( valueRect, property, GUIContent.none );
}
}
(As a side note: For the Split2 attribute, I’d love to automatically get the line height of the previous field, but I didn’t see any way to do it. So right now, it’s simply manually passed as an argument.)