- Home /
How to make sliders evenly adjust to other settings
Hi, im trying to make three sliders that adjust to equal the total when added... the total is set to be 30... so if one slider paddle is all the way to 30 then all the other sliders would equal 0. Then if i were to move the slider valued at 30 down to 10, then both other sliders should equal 10 too.
The best I could do was this: using UnityEngine; using System.Collections;
public class Sliders : MonoBehaviour {
public float hSliderValue1 = 10.0F;
public float hSliderValue2 = 10.0F;
public float hSliderValue3 = 10.0F;
void OnGUI() {
hSliderValue1 = GUI.HorizontalSlider(new Rect(100, 100, 200, 60),30.0F-(hSliderValue3+hSliderValue2), 0.0F, 30.0F);
hSliderValue2 = GUI.HorizontalSlider(new Rect(100, 200, 200, 60),30.0F-(hSliderValue3+hSliderValue1), 0.0F, 30.0F);
hSliderValue3 = GUI.HorizontalSlider(new Rect(100, 300, 200, 60),30.0F-(hSliderValue2+hSliderValue1), 0.0F, 30.0F);
}
}
Help...
Answer by AlwaysSunny · Feb 05, 2015 at 04:39 AM
This should get you started, if not solve the issue. Took a few minutes to find this lil' script in my harddrive's graveyard... ;)
Edit - tailored code sample to OP's question.
private float[] set;
public float hSliderValue1 = 0.3333f;
public float hSliderValue2 = 0.3333f;
public float hSliderValue3 = 0.3333f;
void Start() {
set = new float[3];
set[0] = hSliderValue1;
set[1] = hSliderValue2;
set[2] = hSliderValue3;
}
void OnGUI() {
for (int k = 0; k < set.Length; k++) {
Rect r = new Rect(100, 100+(k*100), 200, 60);
AdjustValueSet(k, GUI.HorizontalSlider(r, set[k], 0, 1));
}
hSliderValue1 = set[0] * 30;
hSliderValue2 = set[1] * 30;
hSliderValue3 = set[2] * 30;
}
void AdjustValueSet(int changedIndex, float thisValue) {
float valueBefore = set[changedIndex];
set[changedIndex] = thisValue;
if (valueBefore == thisValue) return;
float sum = 0;
for (int i = 0; i < set.Length; i++) sum += set[i];
for (int i = 0; i < set.Length; i++) set[i] /= sum;
}
oh, and IIRC you probably need to pre-initialize your set so that one value is the maxValue, or all 3 are 1/3rd maxValue, etc. And to get the set total to = 30, you'll have to multiply each member by 30 after line 12.
void OnGUI() { for (int k = 0; k < set.Length; k++) {
Rect r = new Rect(100, 100+(i*100), 200, 60); AdjustValueSet(k, EditorGUILayout.Slider(r, set[k], 0, 30));
this whole chunk of code is throwing up a lot of errors
Sorry about that, I'd forgotten my script was an editor script.
Try replacing EditorGUILayout.Slider with GUI.HorizontalSlider
When posting about errors, please include the whole error message so we can see it. Hope it works now,
Answer by AlwaysSunny · Feb 06, 2015 at 07:35 PM
I'm no longer sure why you're having trouble getting my sample to play nice. The formula works:
given set A,
if any element changes,
sum all elements,
set each element = element / sum
You can test it with pen and paper; this is how this "self-balancing sliders" trick is done, I promise. Try scripting your own solution to this formula if you're having that much trouble getting mine to work for you.
0.25 0.25 0.50 // set of values
0.30 0.25 0.50 // 1st element changed (by manipulating slider)
0.30 + 0.25 + 0.50 = 1.05 // sum all elements
0.30 / 1.05 = 0.28 // divide elements by sum
0.25 / 1.05 = 0.23
0.50 / 1.05 = 0.47
0.28 0.23 0.47 // replace element with quotient
using System.Collections;
public class Sliders : $$anonymous$$onoBehaviour {
private float[] set;
public float hSliderValue1 = 10.0F;
public float hSliderValue2 = 10.0F;
public float hSliderValue3 = 10.0F;
void Start() {
set = new float[3];
set[0] = hSliderValue1;
set[1] = hSliderValue2;
set[2] = hSliderValue3;
}
void OnGUI() {
for (int k = 0; k < set.Length; k++) {
Rect r = new Rect(100, 100+(k*100), 200, 60);
AdjustSetValue(k, GUI.HorizontalSlider(r, set[k], 0, 30));
}
hSliderValue1 = set[0];
hSliderValue2 = set[1];
hSliderValue3 = set[2];
}
void AdjustSetValue(int changedIndex, float thisValue) {
float valueBefore = set[changedIndex];
set[changedIndex] = thisValue;
if (valueBefore == thisValue) return;
float sum = 0;
for (int k = 0; k < set.Length; k++) sum += set[k];
for (int k = 0; k < set.Length; k++) set[k] /= sum;
}
}
That is exactly what I have, if you put that on a script, and attach it to a game object you will get this!
But then if you try adjusting the sliders, it does this...
I edited the code sample in my original answer; it works okay now for me. Best,
private float[] set;
public float hSliderValue1 = 0.3333f;
public float hSliderValue2 = 0.3333f;
public float hSliderValue3 = 0.3333f;
void Start() {
set = new float[3];
set[0] = hSliderValue1;
set[1] = hSliderValue2;
set[2] = hSliderValue3;
}
void OnGUI() {
for (int k = 0; k < set.Length; k++) {
Rect r = new Rect(100, 100+(k*100), 200, 60);
AdjustValueSet(k, GUI.HorizontalSlider(r, set[k], 0, 1));
}
hSliderValue1 = set[0] * 30;
hSliderValue2 = set[1] * 30;
hSliderValue3 = set[2] * 30;
}
void AdjustValueSet(int changedIndex, float thisValue) {
set[changedIndex] = thisValue;
float sum = 0;
for (int i = 0; i < set.Length; i++) sum += set[i];
for (int i = 0; i < set.Length; i++) set[i] /= sum;
}
Your answer
Follow this Question
Related Questions
Can't get Mono 4.0 to go full screen on Mac OS - Mavericks? 3 Answers
Parsing Error? 1 Answer
Toggle Button for Sound On/Off 1 Answer
Animation from a Blender model not looping 1 Answer
rigidbody2D.MovePosition() Can't Found 2 Answers