I am using a Custom Editor Inspector for my MonoBehaviour class and I would like to know I it’s possible to constrain a variable. Let me illustrate this with an example.
Let’s imagine I have an integer value but I would like to authorize only some values in the Inspector like 1, 5, 99, whatever.
I would like to know if there is way to do that for any type of variables using editor features. I know that I can define an Enum or use a Dictionnary with some index but is there another way ?
[SerializeField]
private int someInteger;
public int SomeInteger
{
get
{
return someInteger;
}
set
{
if (value > 10)
Debug.LogError("someInteger must be less than 10!");
else
someInteger = value;
}
}
@KelsoMRK Thank you for your answer but I would like to do it in the inspector. It’s more like an enum but I don’t want to use an enum, I would prefere use and editor feature or function. Is it possible ?
If you’re really constraining only to specific values (e.g., 1, 5, 99), then you’ll definitely need to either write your own property attribute or handle it specially in the custom editor draw method. If you write a property attribute, you can use it more generally, something like:
Thanks @LightStriker_1 , EditorGUI.Popup is what I was looking for !
Thanks @TonyLi I thought that something like [ConstrainValues(1,5,99)] already exist in Unity but I was wrong.