|
| 1 | +# Min/max Range Attribute |
| 2 | + |
| 3 | + |
| 4 | +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. |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +[](https://assetstore.unity.com/packages/tools/utilities/min-max-range-attribute-302500) |
| 9 | + |
| 10 | +## Features |
| 11 | +- Intuitive, compact inspector representation. |
| 12 | +- Support for Unity's `Vector2` and `Vector2Int` types. |
| 13 | +- Easy to use: just add the attribute to a serialized field of a supported type. |
| 14 | +- Custom floating-point decimal places (0 to 3). |
| 15 | +- 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. |
| 16 | +- Uses built-in Unity's [`EditorGUILayout.MinMaxSlider`](https://docs.unity3d.com/ScriptReference/EditorGUILayout.MinMaxSlider.html) under the hood. |
| 17 | + |
| 18 | + |
| 19 | +## Usage |
| 20 | +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`](https://docs.unity3d.com/ScriptReference/EditorGUILayout.MinMaxSlider.html), 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 |
| 21 | + |
| 22 | +### Vector2 usage |
| 23 | +When using the attribute on a field of type `Vector2`, its constructor takes 3 arguments: |
| 24 | +- `minLimit`: the minimum possible value (lower bound). |
| 25 | +- `maxLimit`: the maximum possible value (upper bound). |
| 26 | +- `decimals`: how many decimal places the inspector should display. Default is 1 and values must be in the [0, 3] range. |
| 27 | + |
| 28 | +For example, the field below has `minLimit` equal to 0, `maxLimit` equal to 10 and `decimals` equal to 3: |
| 29 | +```csharp |
| 30 | +[MinMaxRange(0f, 10f, 3)] |
| 31 | +[SerializeField] private Vector2 _optimalSpeed = new (3.141f, 5.789f); |
| 32 | +``` |
| 33 | + |
| 34 | +And its inspector representation is: |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +If `decimals` is 2 (`MinMaxRange(0f, 10f, 2)`): |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +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: |
| 43 | +```csharp |
| 44 | +[MinMaxRange(0f, 10f)] |
| 45 | +[SerializeField] private Vector2 _optimalSpeed = new (3.141f, 5.789f); |
| 46 | +``` |
| 47 | + |
| 48 | +Which will be displayed as: |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +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. |
| 53 | + |
| 54 | +### Vector2Int usage |
| 55 | +When using the attribute on a field of type `Vector2Int`, its constructor takes 2 arguments, similar to `RangeAttribute`'s parameters: |
| 56 | +- `minLimit`: the minimum possible value (lower bound). |
| 57 | +- `maxLimit`: the maximum possible value (upper bound). |
| 58 | + |
| 59 | +For example, the field below has `minLimit` equal to 0 and `maxLimit` equal to 10: |
| 60 | +```csharp |
| 61 | +[MinMaxRange(0, 10)] |
| 62 | +[SerializeField] private Vector2Int _rewardRange = new(2, 4); |
| 63 | +``` |
| 64 | +And its inspector representation is: |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +## Importing |
| 69 | +The first step is to import the library into your Unity project. There are three ways to do so: |
| 70 | +- [Via the Asset Store](#import-via-unity-asset-store). |
| 71 | +- Via the Package Manager: |
| 72 | + - [Using a git URL](#import-using-a-git-url). |
| 73 | + - [Using OpenUPM](#import-with-openupm). |
| 74 | + |
| 75 | +### Import via Unity Asset Store |
| 76 | +Click on the image below to visit the package's page on the Unity Asset Store. One there, follow the instructions to import it into your project. |
| 77 | + |
| 78 | +[](https://assetstore.unity.com/packages/tools/utilities/min-max-range-attribute-302500) |
| 79 | + |
| 80 | +### Import using a git URL |
| 81 | +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": |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +Next, enter the following in the "URL" input field to install the latest version of the attribute: |
| 86 | +``` |
| 87 | +https://github.com/lazysquirrellabs/min_max_range_attribute.git?path=Assets/Lazy Squirrel Labs/MinMaxRangeAttribute |
| 88 | +``` |
| 89 | +Finally, click on the "Add" button. The importing process should start automatically. Once it's done, the attribute can be used in your project. |
| 90 | + |
| 91 | +### Import with OpenUPM |
| 92 | +Min/max Range Attribute is available as a package on [OpenUPM](https://openupm.com/packages/com.lazysquirrellabs.minmaxrangeattribute/). To import it into your project via the command line, run the following command: |
| 93 | +``` |
| 94 | +openupm add com.lazysquirrellabs.minmaxrangeattribute |
| 95 | +``` |
| 96 | +Once the importing process is complete, the attribute can be used in your project. |
| 97 | + |
| 98 | +## Compatibility and dependencies |
| 99 | +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. |
| 100 | + |
| 101 | +## Contributing |
| 102 | +If you would like to report a bug, please create an [issue](https://github.com/lazysquirrellabs/min_max_range_attribute/issues). 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](https://matheusamazonas.net/contact.html). |
| 103 | + |
| 104 | +## Getting help |
| 105 | +Use the [issues page](https://github.com/lazysquirrellabs/min_max_range_attribute/issues) 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. |
| 106 | + |
| 107 | +## License |
| 108 | +Min/max Range Attribute is distributed under the terms of the MIT license. For more information, check the [LICENSE](https://github.com/lazysquirrellabs/min_max_range_attribute/blob/main/LICENSE) file in this repository. |
0 commit comments