Optional 'isEnforced' argument for PropertyRange

Issue #135 open
Johannes P. created an issue

The Slider provided by PropertyRangeAttribute is useful, but it provides a hard limit on the range of values.

There are use cases where a designer might want to define a 'suggested' range, but still have the option to enter values outside of that range using the input field or in code.

I would like to propose the addition of an isEnforced argument to PropertyRangeArgument.

Usage would be as follows:

[PropertyRange(1, 10, isEnforced: true)]
int Enforced; // Cannot use UI to go below 1 or above 10

[PropertyRange(1, 10, isEnforced: false)]
int NotEnforced; // User can use input field to go below 1 or above 10

[PropertyRange(1, 10)]
int StillEnforced; // Default behavior has isEnforced = true

Visually the UI would look identical in all cases (a slider and an input field, both of which represent the value of the underlying field/property). The UI behaviour would be as follows:

  • If isEnforced is true, or not provided, the behavior is identical to what it is right now:
    • The slider is limited to range [min, max], and cannot be moved outside that range.
    • The input field is limited to range [min, max], and any values outside that range are snapped back to be within that range.
    • In terms of interacting with the field/property that has this attribute with code, the behavior is identical to [UnityEngine.Range(min, max)].
  • If isEnforced is false, the behavior is slightly different:
    • The slider is still limited to range [min, max] - this has not changed
    • However the user is able to enter values outside of range [min, max] using the input field. If a value outside the range is entered, it is stored, and the slider appears at the corresponding min or max location (whichever is closer to the given value),
    • If the user touches the slider after having entered a value outside of the range, the value will immediately snap to the corresponding value on the slider. It is still impossible to go beyond the recommended range using the slider.
    • In terms of interacting with the field/property that has this attribute with code, the behavior is as if no attributes were present. No limits is enforced.

I feel that allowing designers to define bounds that they consider reasonable in most scenarios, but can still go outside of in special cases, will provide more freedom than the current behavior.

This seems like one of the perfect ways that Odin could set itself apart as being more useful than the default Unity attributes, since right now PropertyRangeAttribute is basically identical to UnityEngine.RangeAttribute.

Alternatively, if the distinction between enforced and not should be made more clear, a new attribute such as PropertyUIRangeAttribute or SuggestedRangeAttribute could be added instead.

Comments (9)

  1. Log in to comment