ProgressBarAttribute

Draws a horizontal progress bar based on the value of the property.

Use it for displaying a meter to indicate how full an inventory is, or to make a visual indication of a health bar.

namespace Sirenix.OdinInspector
{
[AttributeUsageAttribute((AttributeTargets)32767, AllowMultiple = False, Inherited = True)]
public sealed class ProgressBarAttribute : Attribute
}

Requirements

Namespace: Sirenix.OdinInspector
Assembly: Sirenix.OdinInspector
Version: 2.0.0.0

See Also

Fields

Return Type Definition Description
float B The blue channel of the color of the progress bar.
string BackgroundColorMember Optional reference to a Color field, property or method, to dynamically change the background color of the progress bar. Default background color is (0.16, 0.16, 0.16, 1).
string ColorMember Optional reference to a Color field, property or method, to dynamically change the color of the progress bar.
string CustomValueStringMember References a member by name to get a custom value label string from.
float G The green channel of the color of the progress bar.
int Height The height of the progress bar in pixels. Defaults to 12 pixels.
double Max The maximum value.
string MaxMember The name of a field, property or method to get the max values from.
double Min The minimum value.
string MinMember The name of a field, property or method to get the min values from.
float R The red channel of the color of the progress bar.
bool Segmented If true then the progress bar will be drawn in tiles.

Constructors

Definition Description
ProgressBarAttribute(double, double, float, float, float) Draws a progress bar for the value.
ProgressBarAttribute(double, string, float, float, float) Draws a progress bar for the value.
ProgressBarAttribute(string, double, float, float, float) Draws a progress bar for the value.
ProgressBarAttribute(string, string, float, float, float) Draws a progress bar for the value.

Properties

Return Type Definition Description
bool DrawValueLabel If true then there will be drawn a value label on top of the progress bar.
bool DrawValueLabelHasValue Gets a value indicating if the user has set a custom DrawValueLabel value.
TextAlignment ValueLabelAlignment The alignment of the value label on top of the progress bar. Defaults to center.
bool ValueLabelAlignmentHasValue Gets a value indicating if the user has set a custom ValueLabelAlignment value.

Example

The following example shows how ProgressBar can be used.

public class ProgressBarExample : MonoBehaviour
{
	// Default progress bar.
	[ProgressBar(0, 100)]
	public int ProgressBar;

	// Health bar.
	[ProgressBar(0, 100, ColorMember = "GetHealthBarColor")]
	public float HealthBar = 50;

	private Color GetHealthBarColor(float value)
	{
		// Blends between red, and yellow color for when the health is below 30,
		// and blends between yellow and green color for when the health is above 30.
		return Color.Lerp(Color.Lerp(
			Color.red, Color.yellow, MathUtilities.LinearStep(0f, 30f, value)),
			Color.green, MathUtilities.LinearStep(0f, 100f, value));
	}

	// Stacked health bar.
	// The ProgressBar attribute is placed on property, without a set method, so it can't be edited directly.
	// So instead we have this Range attribute on a float to change the value.
	[Range(0, 300)]
	public float StackedHealth;

	[ProgressBar(0, 100, ColorMember = "GetStackedHealthColor", BackgroundColorMember = "GetStackHealthBackgroundColor")]
	private float StackedHealthProgressBar
	{
		// Loops the stacked health value between 0, and 100.
		get { return this.StackedHealth - 100 * (int)((this.StackedHealth - 1) / 100); }
	}

	private Color GetStackedHealthColor()
	{
		return
			this.StackedHealth > 200 ? Color.cyan :
			this.StackedHealth > 100 ? Color.green :
			Color.red;
	}

	private Color GetStackHealthBackgroundColor()
	{
		return
			this.StackedHealth > 200 ? Color.green :
			this.StackedHealth > 100 ? Color.red :
			new Color(0.16f, 0.16f, 0.16f, 1f);
	}

	// Custom color and height.
	[ProgressBar(-100, 100, r: 1, g: 1, b: 1, Height = 30)]
	public short BigProgressBar = 50;
	
    // You can also reference members by name to dynamically assign the min and max progress bar values.
    [ProgressBar("DynamicMin", "DynamicMax")]
    public float DynamicProgressBar;
    
    public float DynamicMin, DynamicMax;
}