文章目录
- 1 说明
- 2 Number 特性
- 2.1 MaxValue / MinValue
- 2.2 MinMaxSlider
- 2.3 ProgressBar
- 2.4 PropertyRange
- 2.5 Unit
- 2.6 Wrap
1 说明
本文介绍 Odin Inspector 插件中有关 Number 特性的使用方法。
2 Number 特性
2.1 MaxValue / MinValue
在 Inspector 窗口中对象能够被设置的最小 / 大值。超过该范围则会有错误提示。
double maxValue/minValue
最大 / 小值。
string Expression
用于解析最大 / 小值的字符串。可以是字段、属性、方法名或表达式。
// MinMaxValueValueExamplesComponent.cs
using Sirenix.OdinInspector;
using UnityEngine;
public class MinMaxValueValueExamplesComponent : MonoBehaviour
{
// Ints
[Title("Int")]
[MinValue(0)]
public int IntMinValue0;
[MaxValue(0)]
public int IntMaxValue0;
// Floats
[Title("Float")]
[MinValue(0)]
public float FloatMinValue0;
[MaxValue(0)]
public float FloatMaxValue0;
// Vectors
[Title("Vectors")]
[MinValue(0)]
public Vector3 Vector3MinValue0;
[MaxValue(0)]
public Vector3 Vector3MaxValue0;
}
2.2 MinMaxSlider
将 Vector2 向量表示为 [min, max] 区间,并在 Inspector 窗口中以滑动条方式显示。其中,x 为最小值,y 为最大值。
float minValue/maxValue
最小 / 大值。
string minValueGetter/maxValueGetter
获取最小 / 大值的方法名称。
string minMaxValueGetter
获取最小、大值对的方法名称。
bool showFields = false
是否显示对象名称。
// MinMaxSliderExamplesComponent.cs
using Sirenix.OdinInspector;
using UnityEngine;
public class MinMaxSliderExamplesComponent : MonoBehaviour
{
[MinMaxSlider(-10, 10)]
public Vector2 MinMaxValueSlider = new Vector2(-7, -2);
[MinMaxSlider(-10, 10, true)]
public Vector2 WithFields = new Vector2(-3, 4);
[InfoBox("You can also assign the min max values dynamically by referring to members.")]
[MinMaxSlider("DynamicRange", true)]
public Vector2 DynamicMinMax = new Vector2(25, 50);
[MinMaxSlider("Min", 10f, true)]
public Vector2 DynamicMin = new Vector2(2, 7);
[InfoBox("You can also use attribute expressions with the @ symbol.")]
[MinMaxSlider("@DynamicRange.x", "@DynamicRange.y * 10f", true)]
public Vector2 Expressive = new Vector2(0, 450);
public Vector2 DynamicRange = new Vector2(0, 50);
public float Min { get { return this.DynamicRange.x; } }
public float Max { get { return this.DynamicRange.y; } }
}
2.3 ProgressBar
为 value 绘制进度条。
double min/max
最小 / 大值区间。
float r = 0.15f, float g = 0.47f, float b = 0.74f
进度条颜色。
int Height
进度条高度,默认为 12 像素。
bool Segmented
进度条是否分段显示。
string ColorGetter/BackgroundColorGetter
进度条 / 背景条颜色的获取方法。
bool DrawValueLabel
是否绘制对象标签。
// ProgressBarExamplesComponent.cs
using Sirenix.OdinInspector;
using UnityEngine;
public class ProgressBarExamplesComponent : MonoBehaviour
{
[ProgressBar(0, 100)]
public int ProgressBar = 50;
[HideLabel]
[ProgressBar(-100, 100, r: 1, g: 1, b: 1, Height = 30)]
public short BigColoredProgressBar = 50;
[ProgressBar(0, 10, 0, 1, 0, Segmented = true)]
public int SegmentedColoredBar = 5;
[ProgressBar(0, 100, ColorGetter = "GetHealthBarColor")]
public float DynamicHealthBarColor = 50;
// The min and max properties also support attribute expressions with the $ symbol.
[BoxGroup("Dynamic Range")]
[ProgressBar("Min", "Max")]
public float DynamicProgressBar = 50;
[BoxGroup("Dynamic Range")]
public float Min;
[BoxGroup("Dynamic Range")]
public float Max = 100;
[Range(0, 300)]
[BoxGroup("Stacked Health"), HideLabel]
public float StackedHealth = 150;
[HideLabel, ShowInInspector]
[ProgressBar(0, 100, ColorGetter = "GetStackedHealthColor", BackgroundColorGetter = "GetStackHealthBackgroundColor", DrawValueLabel = false)]
[BoxGroup("Stacked Health")]
private float StackedHealthProgressBar {
get { return this.StackedHealth % 100.01f; }
}
private Color GetHealthBarColor(float value) {
return Color.Lerp(Color.red, Color.green, Mathf.Pow(value / 100f, 2));
}
private Color GetStackedHealthColor() {
return
this.StackedHealth > 200 ? Color.white :
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);
}
}
2.4 PropertyRange
创建滑块控件,将属性的值设置在指定范围之间。
double min/max
最小 / 大值。
string minGetter/maxGetter
获取最小、大值的方法名称。
// PropertyRangeExampleComponent.cs
using Sirenix.OdinInspector;
using UnityEngine;
public class PropertyRangeExampleComponent : MonoBehaviour
{
[Range(0, 10)]
public int Field = 2;
[InfoBox("Odin's PropertyRange attribute is similar to Unity's Range attribute, but also works on properties.")]
[ShowInInspector, PropertyRange(0, 10)]
public int Property { get; set; }
[InfoBox("You can also reference member for either or both min and max values.")]
[PropertyRange(0, "Max"), PropertyOrder(3)]
public int Dynamic = 6;
[PropertyOrder(4)]
public int Max = 100;
}
2.5 Unit
为 value 显示单位。
Units unit
显示的单位(枚举)。
Units @base
基础单位。
Units display
在 Inspector 窗口中显示的单位(经过转换)。
bool DisplayAsString
如果为 true,则绘制为只读文本。
bool ForceDisplayUnit
如果为 true,则禁用使用右键单击上下文菜单更改显示单位的选项。
// UnitExampleComponent.cs
using Sirenix.OdinInspector;
using UnityEngine;
#if UNITY_EDITOR // Editor namespaces can only be used in the editor.
using Sirenix.Utilities.Editor;
#endif
public class UnitExampleComponent : MonoBehaviour
{
// Kilogram unit. Change the display by right-clicking.
// Try entering '6 lb'.
[Unit(Units.Kilogram)]
public float Weight;
// Meters per second unit, displayed as kilometers per hour in the inspector.
// Try entering '15 mph'.
[Unit(Units.MetersPerSecond, Units.KilometersPerHour)]
public float Speed;
// Meters, displayed as centimeters for finer control.
[Unit(Units.Meter, Units.Centimeter)]
public float Distance;
// The speed value, shown as miles per hours. Excellent for debugging values in the inspector.
[ShowInInspector, Unit(Units.MetersPerSecond, Units.MilesPerHour, DisplayAsString = true, ForceDisplayUnit = true)]
public float SpeedMilesPerHour => Speed;
}
2.6 Wrap
在 Inspector 窗口中设置 value 的值超出指定范围时,将该值循环设置(求余)在指定范围内。
double min/max
最小 / 大值。
// WrapExamplesComponent.cs
using Sirenix.OdinInspector;
using UnityEngine;
public class WrapExamplesComponent : MonoBehaviour
{
[Wrap(0f, 100f)]
public int IntWrapFrom0To100;
[Wrap(0f, 100f)]
public float FloatWrapFrom0To100;
[Wrap(0f, 100f)]
public Vector3 Vector3WrapFrom0To100;
[Wrap(0f, 360)]
public float AngleWrap;
[Wrap(0f, Mathf.PI * 2)]
public float RadianWrap;
}