-
-
Save JohannesMP/6ca34305208a01688fd09d3a8757368f to your computer and use it in GitHub Desktop.
Code running pack! two property drawing scripts that make it super easy to trigger code via buttons in the inspector, and get feedback! [TestButton] draws you little buttons that call a method on your MonoBehaviour, and [ProgressBar] give you a great way to show feedback from long running methods. These are *super* helpful for things like proced…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class ProgressBarAttribute : PropertyAttribute | |
{ | |
public bool hideWhenZero; | |
public string label; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
[CustomPropertyDrawer(typeof(ProgressBarAttribute))] | |
public class ProgressBarDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
if (property.propertyType != SerializedPropertyType.Float) | |
{ | |
GUI.Label(position, "ERROR: can only apply progress bar onto a float"); | |
return; | |
} | |
if ((attribute as ProgressBarAttribute).hideWhenZero && property.floatValue <= 0) | |
return; | |
var dynamicLabel = property.serializedObject.FindProperty((attribute as ProgressBarAttribute).label); | |
EditorGUI.ProgressBar(position, property.floatValue/1f, dynamicLabel == null ? property.name : dynamicLabel.stringValue); | |
} | |
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) | |
{ | |
if ((attribute as ProgressBarAttribute).hideWhenZero && property.floatValue <= 0) | |
return 0; | |
return base.GetPropertyHeight(property, label); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment