Created
February 21, 2019 18:48
-
-
Save mao-test-h/ca463811f081fe6b1ebc66e05443ac5c to your computer and use it in GitHub Desktop.
Inspectorにプログレスバーを表示する奴のサンプル
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
// 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