Skip to content

Instantly share code, notes, and snippets.

@rodamn
Forked from gfsl/CompactAttribute.cs
Last active August 5, 2017 03:43
Show Gist options
  • Save rodamn/5219c75dc7ca3bdc97372bfbab2da75b to your computer and use it in GitHub Desktop.
Save rodamn/5219c75dc7ca3bdc97372bfbab2da75b to your computer and use it in GitHub Desktop.
Unity 3D Compact GUI Attribute for Vectors and Quaternions.
using UnityEngine;
public class CompactGuiAttribute : PropertyAttribute {}
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer (typeof(CompactGuiAttribute))]
public class CompactGuiDrawer : PropertyDrawer
{
const float ShrinkLabelAmount = 20f;
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
float labelWidthCache = EditorGUIUtility.labelWidth;
bool wideModeCache = EditorGUIUtility.wideMode;
EditorGUIUtility.labelWidth -= ShrinkLabelAmount;
EditorGUIUtility.wideMode = true;
EditorGUI.PropertyField (position, property, label);
EditorGUIUtility.wideMode = wideModeCache;
EditorGUIUtility.labelWidth = labelWidthCache;
}
}
@rodamn
Copy link
Author

rodamn commented Jan 26, 2017

USAGE:

  • CompactGuiAttribute.cs: Save to any non-editor folder or subfolder (e.g. Scripts/)

  • CompactGuiDrawer.cs: Save to editor folder or subfolder (e.g. Scripts/Editor/)

  • For any Unity Vector* or Quaternion property, precede with the attribute [CompactGui] as such:

public class MyObject : MonoBehavior {
  [CompactGui]
   public Vector3 positionVector;
}

COMPATIBILITY

  • Should work with any Unity 5.x
  • Tested and built with Unity 5.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment