您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
43 行
1.1 KiB
43 行
1.1 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace GameOptionsUtility
|
|
{
|
|
[RequireComponent(typeof(Slider))]
|
|
public class SliderTextBind : MonoBehaviour
|
|
{
|
|
[Tooltip("Text UI")]
|
|
public Text text;
|
|
[Tooltip("Text Prefix")]
|
|
public string Prefix = "";
|
|
[Tooltip("Text Suffix")]
|
|
public string Suffix = "";
|
|
[Tooltip("Multiplies value by 100")]
|
|
public bool AsPercentage = false;
|
|
[Tooltip("Numeric format as seen in C# Standard Numeric Format Strings")]
|
|
public string NumberFormat = "F2";
|
|
|
|
private void OnEnable()
|
|
{
|
|
var slider = GetComponent<Slider>();
|
|
slider.onValueChanged.AddListener(UpdateText);
|
|
UpdateText(slider.value);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
GetComponent<Slider>().onValueChanged.RemoveListener(UpdateText);
|
|
}
|
|
|
|
void UpdateText(float value)
|
|
{
|
|
value = AsPercentage ? value * 100 : value;
|
|
|
|
text.text = $"{Prefix}{value.ToString(NumberFormat)}{Suffix}";
|
|
}
|
|
}
|
|
|
|
}
|
|
|