Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Resolved Cannot get Slider's value correctly

Discussion in 'Scripting' started by GuirieSanchez, Dec 5, 2022.

  1. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    I have a "Slider.OnValueChanged" listener that gets the float marked by the slider.
    The slider's value goes from "600" to "9.999998e+08".

    In the function, I have:

    Code (CSharp):
    1.  private void HandleSliderValueChanged(float value)
    2.     {
    3.         Debug.Log("VALUE: " + value);
    4.  
    5.          _myInputField.text = value.ToString("F0");
    6.  
    7.          _valueText.text = _myInputField.text.ToString("n0");
    8.  
    9.      
    10.     }
    My question is, why
    _myInputField.text
    and
    _valueText.text
    get different numbers?
    _myInputField.text
    gets "999999799",
    while
    _valueText.text
    gets "999,999,800" (= 999999800).

    I noticed that the slider max value appears on the "e+0" formatting. Maybe it's got something to do with the issue, in which case, I don't know how to solve it.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,936
    Perhaps because you gave different formatting instructions to
    .ToString()
    ?

    Go look at the formatting docs for ToString()... it's all laid out on Microsoft/MSDN.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,544
  4. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    They are similar but not duplicates. On that thread, I was asking how to convert from one format to another. On this one, I'm asking about the behavior of the slider's value and "n0" vs "F0". I didn't want to post this question on the previous thread and get someone tell me I'm off-topic and to post a new thread. But if I'm incorrect, then my apologies.

    It's got to be it, thanks. "F0" is getting the correct value, which is "999999799". I have to delve into how the "n0" format works since it doesn't work as I expected.

    Unfortunately, the "n0" format is not explained here:
    https://learn.microsoft.com/en-us/dotnet/api/system.object.tostring?view=net-7.0

    I'll try to find it somewhere else and report my findings. In the meanwhile, if somebody feels generous and wants to share, I'll appreciate it.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,936
    You can do all your own formatting too, it's not hard, dividing numbers and printing suffixes. I usually just do exactly what I want to get, such as:

    Code (csharp):
    1. string MyFormat( int x)
    2. {
    3.   if ( x >= 1000000)
    4.   {
    5.      x /= 1000000;
    6.      return x.ToString() + "m";
    7.   }
    8.   if ( x >= 1000)
    9.   {
    10.      x /= 1000;
    11.      return x.ToString() + "k";
    12.   }
    13.   return x.ToString();
    14. }
    etc. (I just hand-typed the above, it has never been compiled, so forgive typos)
     
    GuirieSanchez likes this.
  6. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    Sorry if the following question is off-topic (I don't want to create duplicates), now I want to focus on the slider:

    The slider max value is set to 999999799 (ended in 799). Then it translates into the "x.xxxe+xx" format, as seen in the inspector (in this case, "9.999998e+08"). if I convert this float value (9.999998e+08) to "long", I get 999999800, probably because it doesn't take into account small figures and goes by larger steps.

    The only way that occurs to me to get the correct "long" is to convert the "slider max value" to string (
    string stringValue = _mySlider.value.ToString("F0")
    ) and then get the "long" (
    long longValue= Convert.ToInt64(stringValue)
    ). This way, I get the expected long (999999799).

    However, this is not suitable since I cannot convert this long value into the slider value. If I wanted to subtract one from the long value (
    longValue--;
    (999999798)) and then set the slider value to the long value:
    _mySlider.value = (float) longValue
    , the value of the slider doesn't change, it keeps being the same: 9.999998e+08. When translating the number, it yields 999999799 instead of 999999798. (Same happens if I did
    _mySlider.value--;
    directly)

    I need the slider to handle large numbers too, is there a way to make the slider use a format that doesn't use "e+", or at least a workaround?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,936
    All the Slider source code is up on github, you can study and hack it however you like, within the limits of their EULA.

    https://github.com/Unity-Technologies/uGUI
     
  8. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
  9. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    Had no luck so far.

    I thought of overriding this cs but it seems too overkill, not to mention that probably Unity won't let me do it that easily. Tomorrow I'll have a look at another option that I have in mind. I'll report it if I succeed.
     
  10. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    Rather than overwriting the original slider rename the class into a new one and make the necessary changes? I don't see any internal stuff here so you should be good to go.
     
    GuirieSanchez likes this.
  11. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452
    upload_2022-12-6_11-42-40.png

    I can't access the SetPropertyUtility. Appart from that, everything seems fine.
     
  12. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    GuirieSanchez likes this.
  13. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    452