Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by InstantLife · Feb 05, 2015 at 04:12 AM · unity4.3

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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;        
 }



Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlwaysSunny · Feb 05, 2015 at 04:40 AM 0
Share

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.

avatar image InstantLife · Feb 05, 2015 at 02:54 PM 0
Share

I dont know how to use that....

avatar image AlwaysSunny · Feb 05, 2015 at 11:05 PM 0
Share

Answer edited to hopefully be more helpful.

avatar image InstantLife · Feb 06, 2015 at 12:15 AM 0
Share

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

avatar image AlwaysSunny · Feb 06, 2015 at 12:42 AM 0
Share

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,

Show more comments
avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image InstantLife · Feb 06, 2015 at 07:59 PM 0
Share

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;        
 }

}

avatar image InstantLife · Feb 06, 2015 at 08:04 PM 0
Share

That is exactly what I have, if you put that on a script, and attach it to a game object you will get this!

alt text

But then if you try adjusting the sliders, it does this...

alt text

screencap1.png (136.5 kB)
screencap2.png (133.5 kB)
avatar image AlwaysSunny · Feb 06, 2015 at 08:19 PM 0
Share

I edited the code sample in my original answer; it works okay now for me. Best,

avatar image InstantLife · Feb 06, 2015 at 08:28 PM 0
Share

Can you comment that code that works?

avatar image AlwaysSunny · Feb 06, 2015 at 09:20 PM 0
Share
  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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges