Note
This material focuses on working with basic UI elements through scripts in Unity. You will learn how to handle button clicks, read slider values, track toggle states, and control scrolling. These skills are essential for creating menus, settings panels, inventories, and any user interfaces.
The Button is the primary interaction element. The .onClick event allows you to execute your code when the user presses and releases the button.
- Get a reference to the
Buttoncomponent. - Add a listener using
onClick.AddListener(MethodName). - Write the method that will be called.
using UnityEngine;
using UnityEngine.UI;
public class MenuController : MonoBehaviour
{
public Button startButton;
void Start()
{
startButton.onClick.AddListener(StartGame);
}
void StartGame()
{
Debug.Log("Game started!");
// Load a scene, start the game, etc.
}
}In the Button's Inspector, there's an OnClick() list. Drag an object there and select its method (e.g., SceneManager.LoadScene). Useful for quick prototypes.
An OptionsButton opens the settings panel. On click – the panel becomes active (SetActive(true)).
A Slider allows the user to select a value from a range (e.g., volume, mouse sensitivity, font size).
- Get a reference to the
Slider. - Read its
.valueproperty. - React to changes using
onValueChanged.AddListener.
using UnityEngine;
using UnityEngine.UI;
public class VolumeControl : MonoBehaviour
{
public Slider volumeSlider;
public AudioSource backgroundMusic;
void Start()
{
volumeSlider.value = PlayerPrefs.GetFloat("Volume", 0.5f);
volumeSlider.onValueChanged.AddListener(OnVolumeChanged);
}
void OnVolumeChanged(float value)
{
backgroundMusic.volume = value;
PlayerPrefs.SetFloat("Volume", value);
}
}Min Value/Max Value– the minimum and maximum values.Whole Numbers– if enabled, the value will be an integer (e.g., 1, 2, 3).Value– the current value (can be set manually).
A volume slider from 0 to 1. As the user drags the handle, the music volume changes in real time, and the value is saved to PlayerPrefs.
A Toggle is a checkbox that can be true (checked) or false (unchecked). Used for on/off settings: fullscreen mode, UI sounds, camera inversion.
- Get a reference to the
Toggle. - Read its
.isOnproperty. - Subscribe to the
onValueChangedevent.
using UnityEngine;
using UnityEngine.UI;
public class SettingsController : MonoBehaviour
{
public Toggle fullscreenToggle;
void Start()
{
fullscreenToggle.isOn = Screen.fullScreen;
fullscreenToggle.onValueChanged.AddListener(OnFullscreenToggled);
}
void OnFullscreenToggled(bool isOn)
{
Screen.fullScreen = isOn;
Debug.Log("Fullscreen mode: " + isOn);
}
}A Toggle can be part of a Toggle Group, where enabling one automatically disables others. Useful for selecting one option from several (e.g., difficulty: Easy / Medium / Hard).
A checkbox "Disable music" – when checked, music volume becomes 0; when unchecked, it returns to the saved value.
A ScrollRect is used for lists, inventories, long texts, or any content that doesn't fit on the screen.
Although ScrollRect is often configured in the Inspector, scripts allow you to control scrolling programmatically:
using UnityEngine;
using UnityEngine.UI;
public class ScrollController : MonoBehaviour
{
public ScrollRect scrollRect;
public Button scrollToTopButton;
void Start()
{
scrollToTopButton.onClick.AddListener(ScrollToTop);
}
void ScrollToTop()
{
scrollRect.verticalNormalizedPosition = 1f; // 1 = top, 0 = bottom
}
public float GetScrollPosition()
{
return scrollRect.verticalNormalizedPosition;
}
}Content– the object withRectTransformthat holds the list items. Its size determines the scrollable area.Viewport– the "window" through which the content is visible.Horizontal/Vertical– allow horizontal/vertical scrolling.Scroll Sensitivity– mouse wheel sensitivity.
An inventory with 20 items. You dynamically create 20 buttons as children of Content, and ScrollRect automatically adds a scroll bar – the player can scroll through the entire inventory.
- Slider changes a
Textvalue (displaying current volume as a percentage). - Toggle enables/disables a GameObject (e.g., an advanced settings panel).
- Button saves all settings or closes the menu.
- ScrollRect contains a list of levels, and clicking a button in the list loads that level.
public class OptionsMenu : MonoBehaviour
{
public Slider volumeSlider;
public Toggle musicToggle;
public Text volumePercentText;
public Button saveButton;
void Start()
{
volumeSlider.onValueChanged.AddListener(v => volumePercentText.text = Mathf.Round(v * 100) + "%");
musicToggle.onValueChanged.AddListener(OnMusicToggled);
saveButton.onClick.AddListener(SaveSettings);
}
void OnMusicToggled(bool isOn) { /* Turn background music off/on */ }
void SaveSettings() { /* Save everything to PlayerPrefs */ }
}