forked from Shardstone/trail-into-darkness
70 lines
2.9 KiB
C#
70 lines
2.9 KiB
C#
using UnityEngine;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
namespace Jovian.InspectorTools {
|
|
public class CompactVector4Attribute : PropertyAttribute {
|
|
public CompactVector4Attribute() {
|
|
}
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
namespace Jovian.InspectorTools.Internal {
|
|
[CustomPropertyDrawer(typeof(CompactVector4Attribute))]
|
|
public class CompactVector4AttributeDrawer : PropertyDrawer {
|
|
|
|
private const int CHILD_COUNT = 4;
|
|
private const float LABEL_WIDTH = 13f;
|
|
private const float INDENT_WIDTH = 15f;
|
|
private const float CHILD_HORIZONTAL_SPACING = 4f;
|
|
private static readonly string[] childPropertyNames = new string[] { "x", "y", "z", "w" };
|
|
private bool IsCompactLayout => Screen.width <= 333f;
|
|
private bool IsNoLabelLayout => Screen.width <= 240f;
|
|
|
|
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
|
|
if(IsCompactLayout) { // Unity Vector2 + Vector3 go multi-line at this point
|
|
return EditorGUIUtility.singleLineHeight * 2f + EditorGUIUtility.standardVerticalSpacing;
|
|
}
|
|
return EditorGUIUtility.singleLineHeight;
|
|
}
|
|
|
|
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
|
|
if(property.propertyType != SerializedPropertyType.Vector4) {
|
|
GUI.Label(position, "Error, SerializedProperty must be 'Vector4' type");
|
|
return;
|
|
}
|
|
|
|
EditorGUI.BeginProperty(position, label, property);
|
|
|
|
if(IsCompactLayout) {
|
|
EditorGUIUtility.labelWidth = position.width;
|
|
}
|
|
Rect labelRect = new Rect(position) { height = EditorGUIUtility.singleLineHeight };
|
|
Rect fieldRect = EditorGUI.PrefixLabel(labelRect, label);
|
|
fieldRect.height = EditorGUIUtility.singleLineHeight;
|
|
|
|
if(IsCompactLayout) {
|
|
fieldRect.y = labelRect.yMax + EditorGUIUtility.standardVerticalSpacing;
|
|
fieldRect.x = labelRect.x + (IsNoLabelLayout ? 0f : INDENT_WIDTH); // 15 = single indent
|
|
fieldRect.width = position.xMax - fieldRect.xMin;
|
|
}
|
|
|
|
EditorGUIUtility.labelWidth = IsNoLabelLayout ? 0.01f : LABEL_WIDTH;
|
|
float childSpacing = IsNoLabelLayout ? CHILD_HORIZONTAL_SPACING / 2f : CHILD_HORIZONTAL_SPACING;
|
|
float childWidth = ((fieldRect.width + childSpacing) / CHILD_COUNT) - childSpacing;
|
|
|
|
for(int i = 0; i < CHILD_COUNT; i++) {
|
|
SerializedProperty childProperty = property.FindPropertyRelative(childPropertyNames[i]);
|
|
Rect childRect = new Rect(fieldRect);
|
|
childRect.width = childWidth; // add spacing
|
|
childRect.x += i * (childWidth + childSpacing);
|
|
EditorGUI.PropertyField(childRect, childProperty);
|
|
}
|
|
|
|
EditorGUI.EndProperty();
|
|
}
|
|
}
|
|
}
|
|
#endif |