Game Development Reference
In-Depth Information
This public variable will be displayed in the Object Inspector with an edit field that
allows the user to type in any valid floating point number thereby setting the value
of MyNumber , as shown in the following screenshot:
Entering in floating point values from the Object Inspector
This code works fine and is suitable in many cases, but there are times when it's
preferable to validate the numerical entry to within a range, clipping the numbers
between a minimum and maximum. You can do this in the code using the
Mathf.Clamp function but you can also validate the entry using an attribute.
You can attach a Range attribute to the floating point variable ( MyNumber ) to
display a slider instead of an edit box, as shown in the following code:
[Range(0f,1f)]
public float MyNumber = 0;
More information on attributes can be found in the online Unity
documentation at http://unity3d.com/learn/tutorials/
modules/intermediate/scripting/attributes .
When this code is compiled, the MyNumber variable displays differently in the Object
Inspector, honoring the numerical range between 0 and 1 , as shown in the following
screenshot. Notice that all numbers provided to the Range attribute as arguments
must be explicit values known at compile time and not expressions, which depend on
variables that can vary at runtime. All attribute values must be known at compile time.
 
Search WWH ::




Custom Search