Monster shadow

VIDEO

Unity Slider in 4 Minutes - [Unity Tutorial]

Description:
The Unity slider component is a great way to handle value ranges, health-bars, progress bars, and more. I'll show you how it works, how to change its appearance, and how to hook into the OnChangedEvent.
ADD A COMMENT

M. Scott Veach
great tutorial but one question: what's a slidau?
Tarodev
They're only available in 'straya
JamesXXXYZ
When i change slider text (TextMeshPro) change too. How can i save/load or update this text when i start the game again? Text is display as default. I save slider.value as PlayerPrefs.
JamesXXXYZ
@Tarodev I tried different things, but If it didn't work (example v).
Tarodev
Yup so you could save it in player prefs, then in your start Functuon set the slider value to the retrieved player prefs
rampaga79
THANK YOU!! I'VE BEEN SEARCHING FOR THE FILL INSTEAD OF STRETCH SOLUTION!
Brandon savanco
Quick question, how can i make it so the slider.value resets to 0? i'm making an idle game and using a slider to show how much time it'll take to spawn things, and that's working fine...the first time around, then the value either goes back to 0 and DOESNT start the process of filling up again, or it just gets stuck in the max value and i cant get it to work
Tarodev
Do you mean once the player lifts their finger you'd like it to go back to 0? The slider itself doesn't have a mouse up event specifically, but you could just hook into your average Input.OnMouseUp event and if the slider has a value you could just do _slider.value = 0; Hopefully I understood your question correctly :)
Gavin Guerette
Can you explain the code please? I don't understand this syntax very well and can't apply this knowledge to my game. What is serialized field? How does this work being that the information is only called on the start method? (at the beginning frame). I see you made a reference but you don't really explain how it's updated and it seems very strange to code with curly brackets within functions.
Tarodev
The serializedfield attribute allows the variable to be visible in the inspector while also remaining private. It works in start as we are subscribing to an event. You only need to subscribe once and can listen to it forever after that. The curly braces in the function is because the function is expecting a parameter of type Action, which is a function. Basically we are saying whenever this slider value changes, please run this code.
Gabriel
very nice tutorial. but how do i get the exact sliders value from another class? is it something like SliderScript.v?
Tarodev
In SliderScript add this: public float Value => _slider.value; In the other class add a reference to SliderScript and just grab Value. Did this on the phone so hopefully it's correct.
Nguyen Ho
short and clear, love it !
Qwerty Ram
I remember I used the slider to make a 'health bar' on the UI, by just removing the knob and setting the slider's value to whatever I wanted it to be
Tarodev
@Qwerty Ram good luck 😉
Qwerty Ram
@Tarodev I might do something like that for the enemies, but frankly, I'll finish my Python project first before getting into a strict typed language again...
Tarodev
Yup, that's a very nice and easy way to set up a health bar. You can also attach it to the actual player object by making the canvas 'world space' and parenting it to the player
Nils Müller-Cleve
Thanks for the hint with the broken call in the inspector. I just pulled up an old project and was wondering why all my sliders didn't work. Your solution fixed it.
Tarodev
It's lucky I used a version of unity which was broken for the video 😂
Eldan Ridley
great video man. really helpful and straight to the point! also you explain really well :)
Guardabarranco Estudio
my slider does not work if I have it in my secondary canvas display 2
Tarodev
@Guardabarranco Estudio good luck my guy!
Guardabarranco Estudio
@Tarodev I only have a canvas on screen 2, on screen 1 there is only one level. I have read a lot of document and can apparently work around it using a Rayscaster chart. What concerns me is that this information is from 2014. I don't know if this component is already included automatically when I add a canvas. Tomorrow I can check it. If I am very lucky and it does not add when doing it, it will solve it otherwise I will have to put buttons to add and subtract the values ​​since the buttons do work Thank you very much
Tarodev
Is it not receiving input? Could it be blocked by an invisible canvas element on the primary canvas?
srisrid multi purpose channel
Exactly 4 minutes gooooood.
Tarodev
I'd never lie to you ;)
SEE ALL COMMENTS


Transcript:

so i'm going to show you about the unity ui slider component to create a slider
right click on your canvas down to ui and slider slider is made up is of four
pieces you've got your main slider logic uh the slider specific logic being down
the bottom here as the direction left to right right to
left up to up up to down min max values
if you want to snap it to whole numbers so if you've got for example six it will
snap one two three four five six instead of going smoothly between the
the float values and then you've got a manual value
slider here next we've got the background there
we've got the fill area which is comprised of the parent which is the
actual area that the fill will be going as well as the actual
sprite there and in the same way we've got our
handle area and then our actual handle sprite
okay now if you think it's a bit ugly and you want to change it
which i do uh you'll need for uh three sprites you'll need a slider background
foreground and point so select your background drag in your
slider background select your foreground put in your
foreground and then select your handle and put in
your handle now you'll notice a few things
i'll just scale this up so we can see it a bit better
you'll notice a few things firstly the foreground stretches which doesn't look
too good so to fix that go to your foreground
change it from simple to filled and then horizontal and as
we're going from left to right left is perfectly fine so now you'll see
that it fills instead of stretches which looks
much better next you'll see that our handle kind of
doesn't go all the way to the edges there
so to fix that i'll put my slider down to there
and i'll do my get my handle slide area and i'll just move it just so it fills
there and then i'll pull my slider to the edge
so i can see where that goes and i will pull it to there so now
our slider goes all the way to the edges and that looks pretty good
now we'll hook it up to update that text on value change
now one little disclaimer uh in unity 2019 and 2020 i found
that this on value changed trigger is always broken i can never get it to work
there's issues in the forum about it as well but it just doesn't seem to ever
get fixed so what you can do is create a script
let's call it a slider script and we'll open that up
and we'll need some references here we'll get a reference to
uh slider and then we'll get our reference to
uh text slider text next in our start method
let's attach our event listener so slider
on value changed and we will add a listener
now in here we will it will give us a new value so let's call that v and
let's say every time that the value changes
we're going to take our value slider text
equals a new value to string and let's format it just so there's two
decimal places because it's probably going to be massive
now on our slider let's attach our scripts
let's pull in our slider value text and let's try that out there you go
that should be just about everything you need to know about the slider uh
if you learned something thumbs up subscribe and i'll see you next time