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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Slider.Set is 'protected'?

Discussion in 'Scripting' started by samboyer276, Jan 28, 2017.

  1. samboyer276

    samboyer276

    Joined:
    Jan 15, 2016
    Posts:
    6
    Hi,
    While working on a project, I've noticed a little issue with the UI.Slider class. I'm setting the value programmatically every frame like a progress bar, but also keeping it interactive. Because of this, I'm using OnValueChanged to manage the user's input (which in this case, sets the progress time based on the value), but this method is also being invoked every frame when I'm setting Slider.value.

    Looking at the Scripting API, there's a procedure called Slider.Set, which allows a bool to decide whether or not the callback should be invoked. However, I can't access it because of the protection level (the API itself marks it as 'protected', which seems strange since it would be so useful to access.

    I'm only using Unity Personal so I can't access the source in attempt to make it public, so is there another way I can access this method? Or is this an issue with Unity, and the method simply needs to be exposed?

    Thank you for any response :)

    (P.S. I'm currently using a workaround based on Input.GetMouseButton, but I feel like a proper solution would be better.)
     
  2. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    Well as the slider class is not sealed you could just inherit from it and expose the Set Method this way?!

    Code (CSharp):
    1. public class mySlider : Slider {
    2.  
    3.         public void MySet(float value, bool callback){
    4.             Set (value, callback);
    5.         }
    6.     }
     
  3. samboyer276

    samboyer276

    Joined:
    Jan 15, 2016
    Posts:
    6
    Ah, that seems to work. Apologies for what seems like a silly post, I'm still quite a beginner at Unity/C# and didn't consider using inheritance. Thank you :)
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    For the record, the source for UI is public, and it has nothing to do with your lisence type.

    I've broken into it occasionally to do some weird things.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    Link to the UI.

    But, yes, if you want to do something fancy with a UI component, it's often better to inherit from it, as you'll need to change public or protected methods to do something else.
     
    FMTriguero likes this.
  6. samboyer276

    samboyer276

    Joined:
    Jan 15, 2016
    Posts:
    6
    Ah, thank you very much! I was assuming that the 'Source Code Access' row on the plan features table meant that all of it was accessible, but I'm guessing that just refers to the core of the software? Anyway that will be very useful to look into.

    Yes, I understand why inheriting from components can be a valuable option and I'll remember to use it for things like this in the future. Thanks again!