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 Larry-W · Feb 18, 2015 at 06:05 AM · c#uislider

UI Slider; -x to +x range, 0 center; how can I get it to fill both directions starting from center?

Situation:

I have a slider, with default value 0, and a range that goes from -5 to +5, with "whole numbers" checked; the slider is in it's default horizontal position.

When the slider is at zero, half of it will always be filled (using either l to r, or r to l direction). This is what I am trying to get around.

I would like for it to have 0% fill at 0, and to either go left or right depending on whether its handle is dragged to a positive or negative value.

And now for a rudimentary ASCII example:

For value +3, whitespace is no fill, - is fill, # is a notch in the whole number slider

 -5    0--- 5

Instead of:

 -5----0---  5

Or:

 -5    0   --5

 

Anyways... I'm not really sure where to start, would love to hear your suggestions!

Thanks

PS: C# preferred... but I suppose beggars can't be choosers :)

Comment
Add comment · Show 1
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 Fariel · Feb 18, 2015 at 07:48 AM 0
Share

I'm taking a wild guess in the dark here, but, could you not solve this by scaling the slider bar? Start it at 0 x scaling, and as it's dragged in a direction, scale it based on the slider value.

This is pretty similar to how I've handled health bars before. You just need to know your constant "max" values. (example: 5 = 100% positive x scale, 0 = 0% x scale, -5 = -100% x scale.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Veldars · Jul 10, 2015 at 01:27 PM

Hi, I wrote this script, that you can apply on a Slider... I use it to do that : alt text

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 [RequireComponent (typeof(Slider))]
 [RequireComponent (typeof(RectTransform))]
 public class SliderSwitcher : MonoBehaviour {
 
     private Slider    _slider;
     private float    _sliderSize;
 
     void Awake() {
         _slider = GetComponent<Slider> ();
     }

     void Update() {
         UpdateSliderSense ();
     }

     public void UpdateSliderSense() {
         if (_sliderSize == 0) {
             _sliderSize = GetComponent<RectTransform> ().rect.width;
             _sliderSize = _sliderSize / (_slider.maxValue - _slider.minValue);
         }
 
         _slider.fillRect.rotation = new Quaternion (0,0,0,0);
         _slider.fillRect.pivot = new Vector2 (_slider.fillRect.transform.parent.localPosition.x, _slider.fillRect.pivot.y);
         if (_slider.value > 0) {
             _slider.fillRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, _sliderSize * _slider.value);
         } else {
             _slider.fillRect.Rotate(0, 0, 180);
             _slider.fillRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, -1 * _sliderSize * _slider.value);
         }
         _slider.fillRect.localPosition = new Vector3 (0, 0, 0);
     }
 }

I hope this help.


slider.png (2.6 kB)
Comment
Add comment · 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
1

Answer by Eloujah · Jul 26, 2020 at 04:22 AM

And here is an even better refact!

 using UnityEngine;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(Slider))]
 public class SliderSwitcher : MonoBehaviour
 {
     private Slider _slider;
     private float center = 0.5f;
 
     void Awake() {
         _slider = GetComponent<Slider>();
     }
 
     void Update() {
         _slider.fillRect.anchorMin = new Vector2(Mathf.Clamp(_slider.handleRect.anchorMin.x, 0, center), 0);
         _slider.fillRect.anchorMax = new Vector2(Mathf.Clamp(_slider.handleRect.anchorMin.x, center, 1), 1);
     }
 }

Comment
Add comment · 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
0

Answer by Eloujah · Jul 26, 2020 at 04:19 AM

If anyone is still looking for this kind of slider behaviour. Here is a simplier version of the script:

 using UnityEngine;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(Slider))]
 public class SliderSwitcher : MonoBehaviour
 {
     private Slider _slider;
 
     void Awake() {
         _slider = GetComponent<Slider>();
     }
 
     void Update() {
         UpdateSliderSense();
     }
 
     public void UpdateSliderSense() {
         if (_slider.value > 0) {
             _slider.fillRect.anchorMin = new Vector2(0.5f, 0);
             _slider.fillRect.anchorMax = new Vector2(_slider.handleRect.anchorMin.x, 1);
         } else {
             _slider.fillRect.anchorMin = new Vector2(_slider.handleRect.anchorMin.x, 0);
             _slider.fillRect.anchorMax = new Vector2(0.5f, 1);
         }
     }
 }
Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Slider inside table view cell,Slider inside a TSTableViewCell 0 Answers

How to save slider values? 3 Answers

In terms of performance, is changing the position of a whole canvas better than changing the position of a UI element? 1 Answer

Animating UI Images - Wondering if there is a more formal way of doing things... 0 Answers

Dynamic slider size with the new UI 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