Open Panel On Button Click Using C# Script In Unity

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"

Open Panel On Button Click Using C# Script in Unity

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.

Open Panel On Button Click Using C# Script In Unity

Select the  UI Button for the scene positioned in the bottom center of the screen.

Open Panel On Button Click Using C# Script in Unity

We will now add the panel to our project.

  • Let's right-click in our hierarchy and choose UI => Panel:

Open Panel On Button Click Using C# Script in Unity

Open the Panel and after that, add a New Panel, resize it, and set the color to a black tone.

Open Panel On Button Click Using C# Script in Unity

To add a basic Text element, right-click our Panel and Choose UI => Text from the menu.

Open Panel On Button Click Using C# Script in Unity

Let's modify the Text by selecting it in the Hierarchy and Changing it. In the scene view, edit the Panel text.

Open Panel On Button Click Using C# Script in Unity

Set the Background image in the scene view 

Open Panel On Button Click Using C# Script in Unity

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.

Open Panel On Button Click Using C# Script in Unity

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.

Open Panel On Button Click Using C# Script in Unity

Rename the folder as Scripts

Open Panel On Button Click Using C# Script in Unity

Create Script

Add scripts and inside this folder, I add a new C-sharp Script, Right-click on Assets. Select Create => C# script.

Open Panel On Button Click Using C# Script in Unity

The new script will be created in the scripts folder you have selected in the Project panel. Rename this script as OpenPanel.

Open Panel On Button Click Using C# Script in Unity

Go to the mono development in Unity and write the coding like the following.

  1. 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.

Open Panel On Button Click Using C# Script In Unity

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.

Open Panel On Button Click Using C# Script In Unity

Start this game by clicking on the Play button.

Open Panel On Button Click Using C# Script in Unity

The panel will be opened by clicking on the Toggle Panel button.

Open Panel On Button Click Using C# Script in Unity

Summary

I hope you understand how to create an open panel feature on a button click using C# scripts.


Similar Articles