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.
❤️ Become a Tarobro on Patreon: https://www.patreon.com/tarodev
🔔 SUBSCRIBE: https://bit.ly/3eqG1Z6
🗨️ DISCORD: https://discord.gg/GqeHHnhHpz
✅ MORE TUTORIALS: https://www.youtube.com/tarodev
Develop video games like a pro!
Whether you're a beginner or a seasoned game developer, you'll find lots of useful tips and tricks to boost your development career. Visit the channel: https://www.youtube.com/channel/UCAg2py6olXgod71Ix06HnUQ
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