- Home /
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.
Answer by Bunny83 · Jul 31, 2018 at 10:05 PM
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:
Answer by UnityCoach · Jul 31, 2018 at 09:53 PM
[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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Custom Editor DragAndDrop for List 1 Answer
Automatically create enum based on class children types? (C#) 1 Answer