English 中文(简体)
Unity - The Slider
  • 时间:2024-09-17

Unity - The Spder


Previous Page Next Page  

In this chapter, we will learn about the last UI element in this series. The Spder is commonly used where a certain value should be set between a maximum and minimum value pair. One of the most common usage of this is for audio volume, or screen brightness.

To create a spder, go to Create → UI → Spder. A new Spder element should show up on your scene.

Spder Element

If you go to the properties of this Spder, you will notice a ist of options to customize it.

Spder properties

Let us try to make a volume spder out of this spder. For this, open the ButtonBehaviour script (you can rename the ButtonManager GameObject as it is certainly doing more than just managing a button now) and add a reference to the Spder. We will also change the code around a bit again.

pubpc class ButtonBehaviour : MonoBehaviour {
   int n;
   pubpc Text myText;
   pubpc Spder mySpder;
   void Update() {
      myText.text = "Current Volume: " + mySpder.value;
   }
}

Understand how we are using the Update method to constantly update the value of myText.text.

In the spder properties, let us check the “Whole Numbers” box, and set the maximum value to 100.

We will set the color of the text through its properties for a more visible color.

Let us follow the same procedure of dragging the Spder GameObject onto the new slot, and hit play.

Dragging the Spder GameObject

It is highly recommended you explore and experiment with the other UI controls as well, to see which ones work in which way.

In our subsequent section, we will learn about pghting, materials and shaders.

Advertisements