Unity Editor - Class variable value contrain

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 ?

Thanks a lot.

[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 ?

Everything is possible if you take the time to code it. You can use the EditorGUI.Popup to set the value you want as a drop down list.

Otherwise, there’s the Advanced Inspector that has a similar feature.
http://lightstrikersoftware.com/docs/AdvancedInspector_Manual.pdf
Check the “Restrict” attribute on page 39.

If you’re constraining to a range (e.g., 0 to 100), you can use the Range attribute in your MonoBehaviour:

[Range(0,100)]
public int someInteger;

In your editor class, let Unity draw the property, which will take the attribute into account:

EditorGUILayout.PropertyField(serializedObject.FindProperty("someInteger"));

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:

[ConstrainValues(1,5,99)]
public int someInteger;

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.