在本文,你将学到如何将UGUI Slider的onValueChanged事件进行统一管理。
代码块:
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class UIManager : MonoBehaviour {
public Slider[] Sliders; //滑块集合
private void Start()
{
foreach (Slider item in Sliders) //为各个Slider注册事件;
{
//其实Value可以不用传过去,因为这个值可以从item中获取;
item.onValueChanged.AddListener((float value)=>OnSliderValueChange(value,item));
}
}
// 同统一管理Slider,被拖动时,会执行各自的command
private void OnSliderValueChange(float value,Slider EventSender)
{
switch (EventSender.name)
{
case "01":
Debug.Log("Slider01"+":"+value.ToString("f2"));
break;
case "02":
Debug.Log("Slider02"+":"+value.ToString("f2"));
break;
case "03":
Debug.Log("Slider03"+":"+value.ToString("f2"));
break;
}
}
}
Debug输出:
标签:Unity3D、UGUI、Slider、EventSystem、OnSliderValueChange、AddListener
快捷访问: