C# Using Dropdown Menu To Change Background Color With Switch Statement

Introduction

 
In this article, we discuss using a dropdown menu in C# to change the background color with a switch statement in Unity.
 
Prerequisites
 
Unity environment version 2018.4.19f1
 
Create the Project
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
Click on the main camera. The camera Inspector displays Click on Clear Flags Option Select the Solid Color in the scene view.
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
First, you have to open the unity3D project. The unity window is like this. Select the GameObject menu in the menu bar. Select UI and pick the Dropdown option.
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
Select the canvas and click on the dropdown. The inspector displays Select the Option and Add the colors name in Unity.
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
Create the Scripts
 
Right-click on Assets. Select Create >> C# script
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
Rename the scripts as ChangeBackgroundColors
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
Double click on the ChangeBackgroundColors script. Write the code like shown below:
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. using UnityEngine.UI;  
  5. public class ChangeBackgroundColors: MonoBehaviour {  
  6.     public Camera cam;  
  7.     public Dropdown mydropdown;  
  8.     // Update is called once per frame  
  9.     void Update() {  
  10.         switch (mydropdown.value) {  
  11.             case 1:  
  12.                 cam.backgroundColor = Color.red;  
  13.                 break;  
  14.             case 2:  
  15.                 cam.backgroundColor = Color.blue;  
  16.                 break;  
  17.             case 3:  
  18.                 cam.backgroundColor = Color.green;  
  19.                 break;  
  20.         }  
  21.     }  
  22. }   
Save the Program.
 
Create the Empty GameObject
 
Select the Game Object >> Create Empty GameObject.
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
Rename the Empty Game Object as DropdownManager
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
Drag and drop the ChangeBackgroundColors script onto the Dropdown Manager.
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
Click on the Play button. Select a color mode in run time.
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
For runtime, select the color red as the background.
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
Runtime select the color as blue for the background.
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 
For runtime, select green as the background.
 
C# Using Dropdown Menu To Change Background Color With Switch Statement
 

Summary 

 
I hope that you understood how to use a C# Dropdown menu to change the background color with a switch Statement in Unity.


Similar Articles