Skip to content

matheusamazonas/min_max_range_attribute

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Min/max Range Attribute

openupm

A bounded (i.e., with a minimum and maximum) range attribute for Unity's Vector2 and Vector2Int fields that draws fields as min/max range sliders, easing the definition of bounded ranges on the inspector.

Five examples of attribute usage on the Unity inspector.

Features

  • Intuitive, compact inspector representation.
  • Support for Unity's Vector2 and Vector2Int types.
  • Easy to use: just add the attribute to a serialized field of a supported type.
  • Custom floating-point decimal places (0 to 3).
  • Error prevention. Unlike using separate fields for minimum and maximum, value integrity is guaranteed (e.g., the minimum will never be greater than the maximum) by the inspector.
  • Uses built-in Unity's EditorGUILayout.MinMaxSlider under the hood.

Usage

To use the attribute, simply add it to a serialized field of a supported type (Vector2 or Vector2Int). Its inspector representation will be a MinMaxSlider, a slider than can be used to represent a range within minimum and maximum limits. The left handle controls the vector's x component and the right handle controls the vector's

Vector2 usage

When using the attribute on a field of type Vector2, its constructor takes 3 arguments:

  • minLimit: the minimum possible value (lower bound).
  • maxLimit: the maximum possible value (upper bound).
  • decimals: how many decimal places the inspector should display. Default is 1 and values must be in the [0, 3] range.

For example, the field below has minLimit equal to 0, maxLimit equal to 10 and decimals equal to 3:

[MinMaxRange(0f, 10f, 3)]
[SerializeField] private Vector2 _optimalSpeed = new (3.141f, 5.789f);

And its inspector representation is:

If decimals is 2 (MinMaxRange(0f, 10f, 2)):

The default value of decimals is 1, so we might as well omit the parameter if we would like to display only 1 decimal place:

[MinMaxRange(0f, 10f)]
[SerializeField] private Vector2 _optimalSpeed = new (3.141f, 5.789f);

Which will be displayed as:

Keep in mind that the decimals parameter only controls how the value labels will be displayed on the inspector. It doesn't control the values' precision.

Vector2Int usage

When using the attribute on a field of type Vector2Int, its constructor takes 2 arguments, similar to RangeAttribute's parameters:

  • minLimit: the minimum possible value (lower bound).
  • maxLimit: the maximum possible value (upper bound).

For example, the field below has minLimit equal to 0 and maxLimit equal to 10:

[MinMaxRange(0, 10)]
[SerializeField] private Vector2Int _rewardRange = new(2, 4);

And its inspector representation is:

Importing

The first step is to import the library into your Unity project. There are two ways to do so: via the Package Manager using a git URL, and via OpenUPM.

Import using a git URL

This approach uses Unity's Package Manager to add the attribute to your project using the repo's git URL. To do so, navigate to Window > Package Manager in Unity. Then click on the + and select "Add package from git URL":

Next, enter the following in the "URL" input field to install the latest version of the attribute:

https://github.com/matheusamazonas/min_max_range_attribute.git?path=Assets/Libraries/MinMaxRangeAttribute#latest

Finally, click on the "Add" button. The importing process should start automatically. Once it's done, the attribute can be used in your project.

Import with OpenUPM

Min/max Range Attribute is available as a package on OpenUPM. To import it into your project via the command line, run the following command:

openupm add com.sneakysquirrellabs.minmaxrangeattribute

Once the importing process is complete, the attribute can be used in your project.

Compatibility and dependencies

The Min/max Range Attribute requires Unity 2021.3.X or above, its target API compatibility level is .NET Standard 2.1, and it does not depend on any other packages.

Contributing

If you would like to report e bug, please create an issue. If you would like to contribute with bug fixing or small improvements, please open a Pull Request. If you would like to contribute with a new feature, contact the developer.

Getting help

Use the issues page if there's a problem with your setup, if something isn't working as expected, or if you would like to ask questions about the tool and its usage.

License

Min/max Range Attribute is distributed under the terms of the MIT license. For more information, check the LICENSE file in this repository.

About

A bounded (i.e., with a minimum and maximum) range attribute for Unity's Vector2 and Vector2Int fields.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages