要实现一个slider控制text内容变化,需要一个组件onValueChanged回调自定义方法
功能:滑块滑动时,文本框内string随之变化
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class TextChang : MonoBehaviour {
public Slider slider;
public Text text;
public Toggle tog;
//UnityAction<float> onSlider;
// Use this for initialization
void Awake()
{
slider = GetComponent<Slider>();
text = transform.Find("txtUseNum").GetComponent<Text>();
}
void Start () {
slider.onValueChanged.AddListener(onSlider); //当slider数值变化时,回调onSlider方法
}
// Update is called once per frame
void Update () {
}
void onSlider(float value)
{
text.text = value.ToString();
text.text = text.text + "/20";
}
}