using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace Rewired.UI.ControlMapper { [AddComponentMenu("")] public class UIControlSet : MonoBehaviour { [SerializeField] private Text title; private Dictionary _controls; private Dictionary controls { get { return _controls ?? (_controls = new Dictionary()); } } public void SetTitle(string text) { if (!(title == null)) { title.text = text; } } public T GetControl(int uniqueId) where T : UIControl { UIControl value; controls.TryGetValue(uniqueId, out value); return value as T; } public UISliderControl CreateSlider(GameObject prefab, Sprite icon, float minValue, float maxValue, Action valueChangedCallback, Action cancelCallback) { GameObject gameObject = UnityEngine.Object.Instantiate(prefab); UISliderControl control = gameObject.GetComponent(); if (control == null) { UnityEngine.Object.Destroy(gameObject); Debug.LogError("Prefab missing UISliderControl component!"); return null; } gameObject.transform.SetParent(base.transform, false); if (control.iconImage != null) { control.iconImage.sprite = icon; } if (control.slider != null) { control.slider.minValue = minValue; control.slider.maxValue = maxValue; if (valueChangedCallback != null) { control.slider.onValueChanged.AddListener(delegate(float value) { valueChangedCallback(control.id, value); }); } if (cancelCallback != null) { control.SetCancelCallback(delegate { cancelCallback(control.id); }); } } controls.Add(control.id, control); return control; } } }