Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.