This project demonstrates simple scene transitions in Unity using UI buttons. It includes two scenes, and users can switch from the first scene ("Scene1") to the second scene ("Scene2") by clicking a button.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
What things you need to install the software and how to install them:
- Unity
- TextMesh Pro (Optional, only if you are using TMP components)
A step-by-step series of examples that tell you how to get a development environment running:
- Clone the repository to your local machine or download the ZIP file and extract it.
- Open Unity Hub and add the project by selecting the project folder.
- Once the project is opened in Unity, go to
File > Build Settings
and make sure both scenes ("Scene1" and "Scene2") are added in the Scenes in Build. - Hit
Play
to start the scene in Unity Editor.
To set up the button for scene transitions, follow these steps:
-
Ensure you have a UI Canvas in "Scene1" with a Button named "Button".
-
Create a C# script named
SceneLoader.cs
. -
Open
SceneLoader.cs
and write the following code to add an event listener to the button:using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class SceneLoader : MonoBehaviour { void Start() { Button btn = GameObject.Find("Canvas/Button").GetComponent<Button>(); btn.onClick.AddListener(OnButtonClick); } void OnButtonClick() { SceneManager.LoadScene("Scene2"); } }
- In the Unity Editor, create an empty GameObject in "Scene1". Name it "Scene Manager".
- Drag the
SceneLoader
script onto the "Scene Manager" GameObject in the inspector to attach it.
- Start the game either by playing the scene in Unity Editor or building the game.
- In "Scene1", press the button labeled "Button" to transition to "Scene2".
- Unity - The game development platform used
This project is licensed under the MIT License - see the LICENSE.md file for details