Introduction
This article covers the need for a combination of coding and design to create interactive experiences in Unity. The ability to open a panel with a single button click is one of Unity's features. In this article, we will see how to open a panel in Unity by clicking a button using the C# script.
Prerequisites
Unity Environment version 2018.3.0f2
Make a new Project and name it "Open Panel On Button click"
Create Button
Buttons are one of the most commonly used Ui components. They are very easy to customize and quick to configure to component any art style to match an application. Buttons feature publicly exposed On Click functions that allow interactions to be performed without any actual scripting involved. Buttons make calling on custom functionality simple as well.
- If a Canvas is already present in the hierarchy, right-click the Canvas It is created automatically. Then, Click the GameObject. Choose the UI and pick the Button.
Select the UI Button for the scene positioned in the bottom center of the screen.
We will now add the panel to our project.
- Let's right-click in our hierarchy and choose UI => Panel:
Open the Panel and after that, add a New Panel, resize it, and set the color to a black tone.
To add a basic Text element, right-click our Panel and Choose UI => Text from the menu.
Let's modify the Text by selecting it in the Hierarchy and Changing it. In the scene view, edit the Panel text.
Set the Background image in the scene view
Open this panel dynamically so that deactivate
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.
Create Folder
Create a C-sharp script. To open it means to activate it. So first I create a new folder. Right-click on Assets. Select Create => Folder.
Rename the folder as Scripts
Create Script
Add scripts and inside this folder, I add a new C-sharp Script, Right-click on Assets. Select Create => C# script.
The new script will be created in the scripts folder you have selected in the Project panel. Rename this script as OpenPanel.
Go to the mono development in Unity and write the coding like the following.
-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OpenPanel: MonoBehaviour {
public GameObject Panel;
public void PanelOpener() {
if (Panel != null) {
bool isActive = Panel.activeSelf;
Panel.SetActive(!isActive);
}
}
}
Save the Program
Go back to the Unity window. Drag and drop the OpenPanel script onto the button Open Panel on a Button click.
To make the OpenPanel script a component of the button, I just drag it into the inspector with the button selected. After that, I assigned the panel to the public panel variable that we added to the script. Now the open panel method should be called by clicking the button. I used the on-click event. You can see this here in the inspector, then I drag in the button and select the OpenPanel.
Start this game by clicking on the Play button.
The panel will be opened by clicking on the Toggle Panel button.
Summary
I hope you understand how to create an open panel feature on a button click using C# scripts.