jacobdufault / fullinspector

Full Inspector supercharges Unity's inspector
MIT License
110 stars 27 forks source link

TWO PROBLEMS with "Step behavior" in InspectorRange attribute #137

Open MergandevinasanderExTenebris opened 8 years ago

MergandevinasanderExTenebris commented 8 years ago

INTRO

Hello, Dear Developer! First of all let me thank you for your really GREAT product! It's extremely useful! Now I'll do my best to describe the problem. (Sorry for my possible bad English. It's not my native).

FIRST PROBLEM (there is no a constructor)

The default behavior (without Step) is OK. Everything works fine. But if we want to add some "step behavior" to our variable, for example:

        [InspectorRange(0f, 1f, 0.05f)]
        public float Range;

we will get an error: "error CS1729: The type 'FullInspector.InspectorRangeAttribute' does not contain a constructor that takes '3' arguments". If we look at the implementation (file 'FullInspector2\Modules\InspectorRange\InspectorRangeAttribute.cs') we will see that there is the only one constructor wIth two default parameters min and max, but there is no an implementation with third Step parameter.

SECOND PROBLEM (wrong calculations with the step)

Let's add the constructor with third step parameter, it's pretty simple, because we already have the calculation logic in Editor/InspectorRangeAttributeEditor.cs file:

        public InspectorRangeAttribute(float min, float max, float step) {
            Min = min;
            Max = max;
            Step = step;
        }

It seems everything is beautiful now but it is not true. Lets create some int variable with range (1, 11) and step = 2:

        [InspectorRange(1f, 11f, 2f)]
        public int RangeWithStep;

We should get the values: 1, 3, 5, 7, 9, 11, don't we? BUT it gives us the wrong values: 0, 2, 4, 6, 8, 10.

jacobdufault commented 8 years ago

I'll take a look at this over the next few days.

nlebedenco commented 7 years ago

@jacobdufault You're calculating the number of steps from 0 to value but you should calculate the number of relative steps from attributes.Min to value instead. This way integer steps won't be off. Try something like :

float value = EditorGUI.Slider(region, label, Convert.ToSingle(element), attribute.Min, attribute.Max;
float result = Mathf.RoundToInt((value - attribute.Min) / attribute.Step) * attribute.Step + attribute.Min;