- Home /
 
"Automatic" value adjustments?
Hi!
Im trying to create a script for the editor that has several sliders for the probability of something. I would like the sum of all the slider values to not exceed 100, but instead adjust the other sliders accordingly. (EG when moving slider A up, slider B and C moves down accordingly so the sum doesn't exceed 100)
The problem I'm having is that I just can't make it work. My current code:
 TargetScript t = (TargetScript)target;
 EditorGUIUtility.LookLikeInspector ();
 DrawDefaultInspector();
 EditorGUI.indentLevel = 0;
 probFold = EditorGUILayout.Foldout (probFold, "Probabilities");
 
 if (probFold)
 {
     oldVals = new List<float>(t.probabilities);
     for (int i = 0; i < t.probabilities.Count; i ++)
     {
         EditorGUI.indentLevel = 2;
         t.probabilities[i] = EditorGUILayout.Slider (t.probabilities[i], 0f, 100f);    
         if (t.probabilities[i] < oldVals[i] || t.probabilities[i] > oldVals[i])
         {
             ignoreIndex = i;
         }
     }
 }
 
 if (GUI.changed)
 {
     for (int i = 0; i < t.probabilities.Count; i ++)
     {
         if (i != ignoreIndex)
         {
             if (t.probabilities[i] > oldVals[i])
             {
                 Debug.Log ("More");
                 t.probabilities[i] -= (t.probabilities[i] - oldVals[i]) / (t.probabilities.Count - 1);
             }
             else if (t.probabilities[i] < oldVals[i])
             {
                 Debug.Log ("Less");
                 t.probabilities[i] += (t.probabilities[i] - oldVals[i]) / (t.probabilities.Count - 1);
             }
         }                
     }
 }
 
               It Logs wheter I move the slider up or down, but nothing more than that. I think it has something to do with me not understanding the inspector GUI loop correctly :/
Does anyone have any idea on how I can do this correctly?
Your answer
 
             Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Get UI Slider Value 3 Answers
Multiple Cars not working 1 Answer
Slider to modify a countdown time in another scene 0 Answers
Slider moving on hover 0 Answers