Skip to content

Instantly share code, notes, and snippets.

@mao-test-h
Created February 21, 2019 18:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mao-test-h/ca463811f081fe6b1ebc66e05443ac5c to your computer and use it in GitHub Desktop.
Save mao-test-h/ca463811f081fe6b1ebc66e05443ac5c to your computer and use it in GitHub Desktop.
Inspectorにプログレスバーを表示する奴のサンプル
// usage:
// [SerializeField] [ProgressBar] float _sample;
// [SerializeField] [ProgressBar("hoge")] float _sample;
// [SerializeField] [ProgressBar(0f, 1f)] float _sample;
// [SerializeField] [ProgressBar(0f, 1f, "hoge")] float _sample;
namespace Sample
{
using System;
using UnityEngine;
[AttributeUsage(AttributeTargets.Field)]
public sealed class ProgressBarAttribute : PropertyAttribute
{
public readonly float Min;
public readonly float Max;
public readonly string Label;
public ProgressBarAttribute(float min, float max, string label = "")
{
this.Min = min;
this.Max = max;
this.Label = label;
}
public ProgressBarAttribute(string label) : this(0f, 1f, label)
{
}
public ProgressBarAttribute() : this(0f, 1f, "")
{
}
}
}
#if UNITY_EDITOR
namespace Sample.Editor
{
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(ProgressBarAttribute))]
public sealed class ProgressBarDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var attr = (ProgressBarAttribute) base.attribute;
var current = Mathf.Lerp(attr.Min, attr.Max, property.floatValue / attr.Max);
EditorGUI.ProgressBar(position, current, attr.Label);
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment