Search Unity

How to set a sliders value without the callback being triggered?

Discussion in 'UGUI & TextMesh Pro' started by antx, Dec 8, 2015.

  1. antx

    antx

    Joined:
    Feb 16, 2012
    Posts:
    28
    Since protected void Set(float input, bool sendCallback); is protected I need to find a way to init my sliders without the callback function to be called.
    The callback function should be called when a user changes the slider value via UI usage, but not when I set the value via script.

    Any hints on how I could do this?
     
    Toomblercazz likes this.
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    Just manage a static flag somewhere which denotes the game is starting and don't perform the logic in your callback if it is true :D
     
    Happy-Zomby likes this.
  3. antx

    antx

    Joined:
    Feb 16, 2012
    Posts:
    28
    Thanks for your reply. A flag is what I came down to as well. Feels somewhat like a workaround hack but is apparently still the cleanest way to do it. I'm initializing this slider not only at start but also during runtime. This is why it feels so dirty.

    Oh and thanks for your signature as well. I'm going to check those UI Extensions out now. :)
     
    SimonDarksideJ likes this.
  4. dreasgrech

    dreasgrech

    Joined:
    Feb 9, 2013
    Posts:
    205
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    The UI framework is open source so you could just download it and change it so it is not protected.
    https://bitbucket.org/Unity-Technologies/ui

    Or inherit from Slider and provide access in your inherited class.
     
    SimonDarksideJ and dreasgrech like this.
  6. antx

    antx

    Joined:
    Feb 16, 2012
    Posts:
    28
    Yeah okay, I could do that, but then I would have to maintain those sources as well.
    Is there a reason why it is protected? This function is handy for everyone. Is it planed to expose it in the future?
    In the meantime you should remove the public entry for this function in the scripting reference. I was so happy when I found it there just to bite into my keyboard when I realized it was not actually public.

    dreasgrech: Thanks for the link. Although I settled for a flag workaround for this time, this post is quite interesting since it's not just for my slider issue.
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Its not that much code to maintain.
    Here I did it for you ;)

    SliderEx.cs
    Code (csharp):
    1. public class SliderEx : UnityEngine.UI.Slider
    2. {
    3.     public void SetValue(float val, bool sendEvent)
    4.     {
    5.         Set(val, sendEvent);
    6.     }
    7. }
    Editor/SliderExEditor.cs
    Code (csharp):
    1. using UnityEditor;
    2.  
    3. [CustomEditor(typeof(SliderExEditor), true)]
    4. [CanEditMultipleObjects]
    5. public class SliderExEditor : UnityEditor.UI.SliderEditor {}
    The function is handy though so I see no reason not to expose it. Can you create a feature request for it?
    http://issuetracker.unity3d.com/

    It should not be in the scripting references, I think this was a bug with the doc generator.
     
  8. antx

    antx

    Joined:
    Feb 16, 2012
    Posts:
    28
    Thanks for the code.

    On the issuetracker page you pointed me to is no option to create a feature request. It's just a list of bugs that can be browsed. (yes I'm logged in there)

    And btw. yesterday I was checking out the new dropdown UI element and noticed there are also a whole bunch of function that are listed under public as well as under protected in the scripting reference. Seems the doc generator has a general problem there. All those function look like they where supposed to be public since they provide customization.
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Sorry wrong link! https://feedback.unity3d.com/

    I think the doc generator did have an issue and picked up some protected stuff by mistake. I have discussed some of the restricted api with the UI team in the past, I believe we will be opening up some of it in the future. If you use the feedback to suggest the parts you need it will help to ensure they don't get missed
     
  10. mowax74

    mowax74

    Joined:
    Mar 3, 2015
    Posts:
    97
    It's still not public! There must be a clean solution to set these elements to a given value without triggering an event.
    I also came up with a dirty flag for that, since years. Just wondered if there is a cleaner solution than that, but there is still not.
    Inheriting my own class from each and every unity class just to have some basic features is also far from a perfect solution. Why do you not make this thing public?
     
  11. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    This change has already been made, its just not hit a public release yet. Expect to see it in the future.
     
    makaka-org likes this.
  12. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,087
    Code (CSharp):
    1. slider.SetValueWithoutNotify(value)
     
    codxr, Toomblercazz, RENchyk and 11 others like this.
  13. chilton

    chilton

    Joined:
    May 6, 2008
    Posts:
    564
    Was this ever implemented? I just ran into a case where it would've been useful today (in 2019).
     
  14. Blightbuster

    Blightbuster

    Joined:
    Feb 14, 2017
    Posts:
    4
    Its implemented as of Unity 2019
     
    mowax74 likes this.
  15. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    nice, thanks. i was debugging a problem that setting the slider value in the onclick callback was putting unity into an infinite loop and the only way to get out was kill it with the task manager.
    slider.SetValueWithoutNotify(value) vs. slider.value = value works perfect
     
  16. harti177

    harti177

    Joined:
    Nov 5, 2018
    Posts:
    12
    Actually, setting minValue and maxValue also triggers the callback
     
    JakesFable and VirtusH like this.
  17. VirtusH

    VirtusH

    Joined:
    Aug 18, 2015
    Posts:
    95
    Unfortunately, using a flag to skip the extra callback is the only solution I see. :(
     
  18. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,087
    U can cache all listeners, remove from callback, set value, set listeners again.
     
  19. Yiming075

    Yiming075

    Joined:
    Mar 24, 2017
    Posts:
    33
    This works in Unity 2020
    toggle.SetIsOnWithoutNotify(value);
    slider.SetValueWithoutNotify(value);
     
    Destriarch, xucian and deekpyro like this.
  20. chibiskuld2

    chibiskuld2

    Joined:
    Nov 11, 2022
    Posts:
    10
    This really needs to have an OnMouseUp or OnRelease event. Most of the time I'd rather it behave this way than onChange.
     
    Last edited: Aug 7, 2023