How can I choose to either make a value constant or random between two constant in the inspector? Similar to the Particle System Inspector

I want to either give a constant value or random one from the inspector.

Similar to the inspector of the particle system.

The ParticleSystem uses it’s own solution. However i quickly created this MinMaxFloat which is a runtime script which you have to copy somewhere into your assets folder. And the corresponding MinMaxFloatDrawer which is an editor script and need to be placed in a subfolder called “editor”.

Now you can simply declare a field like this:

public MinMaxFloat myValue;

And you get a min max slider between 0 and 1. In addition you now have the MinMaxRange attribute where you can specify a custom range for the slider

[MinMaxRange(-5, 5)]
public MinMaxFloat myValue;

I’ve created this as a quick and dirty solution. The MinMaxFloat has a property to actually get a random value betwen the set limits:

float val = myValue.RandomValue; // will be between the set limits

In the inspector it looks like this:

121904-minmaxfloat.png

[SerializeField] float _value;
[SerializeField] bool _random;
[SerializeField] float _min;
[SerializeField] float _max;

public float Value
{
    get { return _random ? Random.Range(_min, _max) : _value; }
}

Then if you want some “elegant” inspector, you need to use a Custom Inspector, to filter unused members.