Skip to content

Instantly share code, notes, and snippets.

@romainPechot
Last active October 11, 2016 11:57
Show Gist options
  • Save romainPechot/09d74b3591c723030914159dae232f54 to your computer and use it in GitHub Desktop.
Save romainPechot/09d74b3591c723030914159dae232f54 to your computer and use it in GitHub Desktop.
Progress Bar Attribute/Drawer for Unity float Inspector
using UnityEngine;
public class ProgressBarAttribute : PropertyAttribute
{
public string name = "Float";
public ProgressBarAttribute(string name)
{
this.name = name;
}// ProgressBarAttribute()
}// ProgressBarAttribute : PropertyAttribute
using UnityEngine;
using UnityEditor;
// put it in an Editor folder
[CustomPropertyDrawer(typeof(ProgressBarAttribute))]
public class ProgressBarAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// make sure we are drawing a float
if(property.propertyType == SerializedPropertyType.Float)
{
float value = property.floatValue;
ProgressBarAttribute progressBarAttribute = (ProgressBarAttribute)attribute;
EditorGUI.ProgressBar(position, value, string.Format("{0} = {1:0.000}", progressBarAttribute.name, value));
}
else EditorGUI.HelpBox(position, "ProgressBarAttribute can only be applied to a Float !", MessageType.Warning);
}// OnGUI()
}// ProgressBarAttributeDrawer : PropertyDrawer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment