Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Gamepad precision with sliders

Discussion in 'UGUI & TextMesh Pro' started by Ellenack, Jan 25, 2016.

  1. Ellenack

    Ellenack

    Joined:
    Feb 16, 2014
    Posts:
    41
    Hi.

    I am currently working on a menu where I need to move sliders. I am trying to setup the pad controls, and am facing an issue : how do I set up the precision of the slider ? What I mean is that when I move the slider with the joystick, it moves by big steps (I think it's a percentage of the slider max value), and I need more precision. Where can I change this value ?
     
  2. Ellenack

    Ellenack

    Joined:
    Feb 16, 2014
    Posts:
    41
    Still looking for an answer, if someone knows. :)
     
  3. TheSniperFan

    TheSniperFan

    Joined:
    Jul 18, 2013
    Posts:
    712
    I don't think the default sliders have that option, but you can always update the value by hand using a script.
     
  4. Travlynn02

    Travlynn02

    Joined:
    Feb 7, 2017
    Posts:
    4
    I had the same problem, my solutions required two steps: Create input entry - Edit->Project Settings->Input (I called mine SliderHorizontal) and I created a small C# script that I attache to the slider object. It works for me and I now have full navigation and and smooth slider control from my game pads.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5.  
    6. /*========================================================================>
    7. * Moves slider with game pad controller defined in Project Input
    8. *   example: SliderHorizontal uses right joystick on all attached Gamepads
    9. * USE AT YOUR OWN RISK
    10. * Feel free to use and improve.
    11. * T. Womack 5 - 2018 Unity 2018
    12. *  ======================================================================>
    13. */
    14. public class SliderMover : MonoBehaviour
    15. {
    16.     private Slider mySlider;
    17.     private GameObject thisSlider;
    18.     private float sliderChange;
    19.     private float maxSliderValue;
    20.     private float minSliderValue;
    21.     private float sliderRange;
    22.     private const float SLIDERSTEP = 100.0f; //used to detrime how fine to change value
    23.     private const string SLIDERMOVE = "SliderHorizontal";
    24.  
    25.     //Initialize values
    26.     private void Awake()
    27.     {
    28.         mySlider = GetComponentInParent<Slider>();
    29.         thisSlider = gameObject; //used to deterine when slider has 'focus'
    30.         maxSliderValue = mySlider.maxValue;
    31.         minSliderValue = mySlider.minValue;
    32.         sliderRange = maxSliderValue - minSliderValue;
    33.     }
    34.  
    35.     private void Update()
    36.     {
    37.         //If slider has 'focus'
    38.         if (thisSlider == EventSystem.current.currentSelectedGameObject)
    39.         {
    40.             sliderChange = Input.GetAxis(axisName: SLIDERMOVE) * sliderRange / SLIDERSTEP;
    41.             float sliderValue = mySlider.value;
    42.             float tempValue = sliderValue + sliderChange;
    43.             if (tempValue <= maxSliderValue && tempValue >= minSliderValue)
    44.             {
    45.                 sliderValue = tempValue;
    46.             }
    47.             mySlider.value = sliderValue;
    48.         }
    49.     }
    50. }
     
  5. Travlynn02

    Travlynn02

    Joined:
    Feb 7, 2017
    Posts:
    4
    Works best with Explicit Navigation on slider object.
     
  6. DasMaeh

    DasMaeh

    Joined:
    Oct 18, 2017
    Posts:
    14
    Thanks for this very good solution Travlynn02, but this script works on top of the regular input method.
    The Axis Input has a deadzone of 0.5f.
    The behavior is like:
    0.0 to 0.5 - nothing happens
    0.5 to 0.75 - only your script does change the slider's value
    above 0.75 - your script AND the regular method that jumps in 0.1f increments apply at the same time.

    How can I prevent the regular input method from taking part?
     
  7. SniperED007

    SniperED007

    Joined:
    Sep 29, 2013
    Posts:
    341
    If you don't need mouse support then you could just disable the Sliders Interactable state.
    Or if you do need mouse support you could create your own Slider class like this (as well as still using the above SliderMover script):

    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2. using UnityEngine.UI;
    3.  
    4. public class ED_Slider : Slider
    5. {
    6.     public override void OnMove(AxisEventData eventData)
    7.     {
    8.         if (!IsActive() || !IsInteractable())
    9.         {
    10.             base.OnMove(eventData);
    11.             return;
    12.         }
    13.     }
    14. }