Showing posts with label User-Interface. Show all posts
Showing posts with label User-Interface. Show all posts

Binding Slider's Value to a Text Component | Using onValueChanged event | Unity Game Engine


SliderValueText.cs
using UnityEngine;
using UnityEngine.UI;

public class SliderValueText : MonoBehaviour
{
    private Slider slider;
    private Text textComp;

    void Awake()
    {
        slider = GetComponentInParent<Slider>();
        textComp = GetComponent<Text>();
    }

    void Start()
    {
        UpdateText(slider.value);
        slider.onValueChanged.AddListener(UpdateText);
    }

    void UpdateText(float val)
    {
        //textComp.text = slider.value.ToString();
        textComp.text = val.ToString();
    }
}

Changing Dropdown options at Runtime | Unity Game Engine


DropdownOperations.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DropdownOperations : MonoBehaviour
{
    private Dropdown dropdown;

    void Awake()
    {
        dropdown = GetComponent<Dropdown>();
    }

    public void LoadFruitOptions()
    {
        dropdown.ClearOptions();
        var option1 = new Dropdown.OptionData("Apple");
        dropdown.options.Add(option1);
        var option2 = new Dropdown.OptionData("Mango");
        dropdown.options.Add(option2);
        dropdown.RefreshShownValue();
    }
    
    public void LoadDrinkOptions()
    {
        dropdown.ClearOptions();
        var option1 = new Dropdown.OptionData("Tea");
        dropdown.options.Add(option1);
        var option2 = new Dropdown.OptionData("Coffee");
        dropdown.options.Add(option2);
        dropdown.RefreshShownValue();
    }
}

Using 'UI Mask' component in Unity Game Engine

Create Radio Buttons Behavior Using Toggle Component


RadioButtonSystem.cs
using UnityEngine;
using UnityEngine.UI;
using System.Linq;

public class RadioButtonSystem : MonoBehaviour
{
    ToggleGroup toggleGroup;

    void Start()
    {
        toggleGroup = GetComponent<ToggleGroup>();
    }

    public void Submit()
    {
        Toggle toggle = toggleGroup.ActiveToggles().FirstOrDefault();
        Debug.Log(toggle.name + " _ " + toggle.GetComponentInChildren<Text>().text);
    }
}