I’m currently working on a system for procedural generation using the built-in terrain engine in Unity. I’d like to allow people to specify the numbers they want for the various bits of the TerrainData, but I’ve run into a wall with some of them. Specifically, certain resolution settings have to be a power of two to function properly.
Is there a way to restrict the available choices using [Range()] or a similar attribute that would make this happen? I can’t seem to find one that would work in the Editor.
The reason for the Delayed attribute is that without it - two things happen:
Each time the value is changed (even while typing), the method gets called and this causes a noticeable lag. This is especially apparent when using the “click and slide” feature on the property name.
When the user uses the “click and slide” feature on the property name and tries to make the value smaller, it goes crazy
So, the delayed attribute causes the OnValidate to be called only on Enter or when you move to another field. It also removes the “Click and slide” feature.
**Here’s the complete code **
[Delayed]
public int TheValue;
private void OnValidate() {
TheValue = ToNearest(TheValue);
}
// adapted from: http://stackoverflow.com/questions/31997707/rounding-value-to-nearest-power-of-two
int ToNearest(int x) {
return (int) Mathf.Pow(2, Mathf.Round(Mathf.Log(x) / Mathf.Log(2)));
}