Unity Tutorial: Open Panel on Button Click

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is janam with an answer to a user request on how to open a panel in unity by clicking a button okay this is unity 2018 I created a new project and first to add a UI button if there is no canvas it is created automatically and the text of the button are set to let's say open panel and after that I add a panel resize it and set the color to a black tone and also add a text to this panel in a white color and the text will be hello unbootable so this was the UI part very fast I know but the request was about how to open this panel dynamically so deactivate the panel by default in the inspector so that it is not visible another create a c-sharp script to open it this means to activate it so first I create a new folder called add scripts and inside of this folder I add a new c-sharp script other gives this script a name for example panel opener and then I double click it and it will be opened in Visual Studio 2017 which is my programming IDE for unity ok here's the script with a class called panel opener and the first thing that I add is a public variable of type game object to that I will assign the panel that I'm going to open I don't need to start an update method serval deleted but I add a new public method that is called open panel inside of that method I check if the panel is assigned if it is not null and in this case I will call set active for this game object ok the next step is to assign this panel opener script as a component to the button I just drag it into the inspector with the button selected and after that I assigned the panel to the public panel variable that we added to the script okay now the open panel method should be called on clicking the button so I used the on click event you can see this here in the inspector then I drag in the button to the slot and select panel opener open panel and that's it now we can go ahead and start this game press the button and the panel will be opened very simple okay I know this already answers the request but let's assume that we want to toggle the visibility of this panel so I renamed the text of the button to toggle panel then I opened the script again and first I want to know if the panel is active I can get this information by using the active self property of the game object now store this in a local bool variable and to toggle the active state of the panel I just called the set active method with the inverted state of the is active variable so use the exclamation mark which is the not operator to always toggle the active state of the panel when this method is called okay so let's try start the game press the button and the panel is visible I press it again to toggle it so guys I hope this answers the request and the tutorial was interesting and useful for you and if this is the case then please don't forget to subscribe to this channel to be notified when the next one is online and think about being my patron this would really help this channel grow so thanks for your support thanks for watching and I'll see you on Janna
Info
Channel: Jayanam
Views: 163,994
Rating: undefined out of 5
Keywords: unity panel open, unity open panel click, unity button click c#, c# script button panel, c# unity activate panel, unity activate panel, unity activeSelf, Unity c# Scripting, unity tutorial button, Unity tutorial panel, Unity UI tutorial, How to open a panel with Unity, unity button click, unity button onclick, unity button script, unity button click event
Id: LziIlLB2Kt4
Channel Id: undefined
Length: 3min 39sec (219 seconds)
Published: Wed Sep 26 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.