Unity tutorial: Scroll View controlled with buttons

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome today i want to show you how to scroll the scroll view with buttons the basic unity engine doesn't allow us to do this it's allowing us only to drag and move it or just use the middle mouse button let's start i will create a new folder called scroll view and then two scripts one will be controlling the middle part and the second one will be controlling buttons so for the middle one i will call it scroll use system for the buttons it will be called scroll button then we will drag and drop it on the scroll view and scroll button on arrow up clock button for arrow down and let's start with scroll button don't need this we don't need this so we need even the handlers and we need to know when the button is down and when it's up for this we will need to use you need the engine dot even systems and we want eye pointer down handler and i pointer up handler so for the ipod the downhandler and up handler you need to implement two methods first one is on pointer down public point on pointer down which accepts one parameter which is pointer even data i'll call it even data and the second one is almost the same it's just on point one point there up same parameter pointer even data event data and when the button is down or up we want to change one boolean value which will be publicly accessible so public bull is down is equal to false when it's down we will change it to true when it's up we'll change it back to false and that's it for the button for the scroll view it will need a little bit of more so we will need a reference to the scroll view so private scroll view scroll view for the scroll rect right scroll correct scroll right to actually use these we need the namespace for unity engine ui so using unity engine.ui let's get the reference we can get it from the component because it's on this component scroll rect then in update method we will we will scroll when the button is down to get the buttons we will make a serialized field which will be accessible from unity editor and it will be a reference to to a button scroll button f button forget to say private and as we want to have it on all four sides we will have four buttons so left right top and bottom and this way we can use it on different scroll like scroll rects anywhere in the ui bottom and top and then we want serialize field for actual speed of the scroll private float scroll speed we will set it default 0.01 let's start with the left button so if left button is set so isn't equal to null we will check if it's down so left button is down and if yes we will just scroll left we will do the same for the remaining three buttons so i will just duplicate the code three times and do it for the right button copy right button and bottom button top button for right we will call it scroll right for bottom scroll bottom and for top scroll top scroll top let's create functions private void scroll left the only thing we need to do here is change the horizontal normalized position of our scroll rectangle so first of all we will check if scroll rect isn't null then check if the scroll wrecked horizontal normalized position is more than zero because it's scrolling from zero to one so it should be either zero or one and or anything in between so to move it we will set the horizontal normalized position minus scroll speed okay and we will duplicate the function three times for the remaining three buttons so scroll left scroll right scroll right has to be always less or equal to one and we are actually incrementing scroll top for this we will use the vertical normalized position and where when we are scrolling top we want to be below or equal to one and for scrolling to bottom scroll bottom [Music] i want to be it has to be more than zero all right that's it now we will reference the buttons in the inside unity editor so we have only the bottom and top so we will use these two arrow down for the bottom and arrow up for the top button and that's it let's try it so when i click the bottom button it's going down when i hold it it's scrolling top okay and that's it thank you for watching
Info
Channel: Jumagama - Game Development
Views: 11,980
Rating: undefined out of 5
Keywords: unity, development, tutorial
Id: WhwHBqjWelk
Channel Id: undefined
Length: 8min 21sec (501 seconds)
Published: Wed Feb 03 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.