Create a Slider Show Screen Space On Button Click Using C# Script In Untiy

Introduction

 
This article demonstrates how to create a Slider Show Screen Show on button click using C# scripts in Unity.
 
Prerequisites
 
Unity Environment version 2018.4.19f1
 

Create a New Project

 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 

Create the Panel

 
if a canvas is already present in the Hierarchy, right click the canvas and select UI > Panel.
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Rename the Canvas as CanvasSliderShow in Scene View
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Rename the Panel as PanelBackGround in Scene View
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 

Create the Button

 
if a canvas is already present in the Hierarchy, right click the canvas and select UI > Button.
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Rename the Button as Preview & Next in Scene View
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 

Create the Image

 
If a canvas is already present in the Hierarchy, right click the canvas and select UI > Image.
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Rename the Image as ImageShow in scene View
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 

Create The Folder

 
Right-click on Assets. Select Create >> Folder.
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Rename the Folder as Script in Scene View
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 

Create the Script

 
Right-click on Assets. Select Create >> C# script
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Rename the Scripts as SliderManager in Scene View.
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Double click on the SliderManager script. Write the code as shown below, 
  1. using System.Collections.Generic;  
  2. using UnityEngine;  
  3. using UnityEngine.UI;  
  4. namespace IsmaelNascimento {  
  5.     public class SliderManager: MonoBehaviour {  
  6.         #region VARIABLES[SerializeField] private Button btnPrev;  
  7.         [SerializeField] private Image imgShow;  
  8.         [SerializeField] private Button btnNext;  
  9.         [Space(10)]  
  10.         [SerializeField] private List < Sprite > imagesForShow;  
  11.         private int indexImagesForShow;  
  12.         #endregion  
  13.         #region METHODS_MONOBEHAVIOUR  
  14.         private void Start() {  
  15.             btnPrev.onClick.AddListener(OnButtonPrevClicked);  
  16.             btnNext.onClick.AddListener(OnButtonNextClicked);  
  17.         }  
  18.         #endregion  
  19.         #region METHODS_PRIVATE  
  20.         private void OnButtonPrevClicked() {  
  21.             indexImagesForShow--;  
  22.             if (indexImagesForShow < 0) {  
  23.                 indexImagesForShow = imagesForShow.Count - 1;  
  24.             }  
  25.             imgShow.sprite = imagesForShow[indexImagesForShow];  
  26.         }  
  27.         private void OnButtonNextClicked() {  
  28.             indexImagesForShow++;  
  29.             if (indexImagesForShow > imagesForShow.Count - 1) {  
  30.                 indexImagesForShow = 0;  
  31.             }  
  32.             imgShow.sprite = imagesForShow[indexImagesForShow];  
  33.         }  
  34.         #endregion  
  35.     }  
  36. }  
Save the program
 
Drag and drop the SliderManager script onto the CanvasSliderShow
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Drag and Drop Button & Image to CanvasSliderShow
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Select Button Preview & Add Component Layout Element in Scene View
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Select Panel BackGround Add Component Hprizontal Layout Group in Scene View
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
On Button Click Start The Slider Show Screen Show Using C# Scripts In Unity
 
First Output 
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Second Output 2nd
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 
Third Output
 
Create The Slider Show Screen Space On Button Click Using C# Script In Untiy
 

Summary

 
I hope you understood how to create a Slider Show Screen Show on a button click using C# scripts in Unity.