UISliderEx 封装方法

xiaoxiao2021-02-27  389

using UnityEngine; using System.Collections; using System.Collections.Generic; using CommonEnum; public class UISliderEx : MonoBehaviour { UIButton _AddBtn; UIButton _SubBtn; UISlider _UISlider; UILabel _Lab; string _MaxTips = "";// 满了增加了 string _MinTips = "";// 最少了不能减了 int _SelectCount = 0;// 当前选中num int _AllCount = 0; // 总数 int _MaxCount = 0; // 最多可选择多少num int _MinCount = 0; // 最少可选择多少num CommonDelegate.IntDelegate _Func = null;// changed事件 bool _NotCanDrag = false;// 默认可正常拖拽 string _Tips = ""; bool _IsInit = false; void Init() { if (_IsInit) return; _IsInit = true; _AddBtn = ObjectCommon.GetChildComponent<UIButton>(gameObject, "addbtn"); _SubBtn = ObjectCommon.GetChildComponent<UIButton>(gameObject, "subbtn"); _UISlider = ObjectCommon.GetChildComponent<UISlider>(gameObject, "slider"); _Lab = ObjectCommon.GetChildComponent<UILabel>(gameObject, "lab"); UITools.AddOnclick(_AddBtn,OnAddBtnClick); UITools.AddOnclick(_SubBtn,OnSubBtnClick); if (_UISlider != null) { _UISlider.onDragProcess = OnDragProcess; _UISlider.onDragFinished = OnDragFinished; } // if (string.IsNullOrEmpty(_MaxTips)) // _MaxTips = "满了,不能选择了"; // // if (string.IsNullOrEmpty(_MinTips)) // _MinTips = "必须选择一个,不能再删除了"; } public void Init(CommonDelegate.IntDelegate func,string max = "",string min = "") { _MaxTips = max; _MinTips = min; _Func = func; } void OnClear() { _SelectCount = 0; _AllCount = 0; _MaxCount = 0; _NotCanDrag = false; _Tips = ""; } public void SetData(int all,int cur = 1,int min = 0,int max = -1) { Init(); OnClear(); _SelectCount = cur; _AllCount = all; _MinCount = min == 0 ? 0 : min; _MaxCount = max == 0 ? _AllCount : max; UpdataCount(false); } void UpdataCount(bool isOperationChange = true) { UITools.SetLabText(_Lab,string.Format("{0}/{1}",_SelectCount,_AllCount)); UITools.SetSlider(_UISlider,(float)_SelectCount/(float)_AllCount); if (isOperationChange) { if (_Func != null) _Func(_SelectCount); } } // + btn void OnAddBtnClick() { if (_SelectCount + 1 > _MaxCount) { UITemplate.ShowInfoTips(_MaxTips); return; } _SelectCount++; UpdataCount(); } // - btn void OnSubBtnClick() { if (_SelectCount - 1 <= 0) { UITemplate.ShowInfoTips(_MinTips); return; } _SelectCount--; UpdataCount(); } bool IsToMin() { int num = Mathf.CeilToInt(UITools.GetSlider(_UISlider)*_AllCount); if (num < _SelectCount) return true; return false; } void OnDragProcess() { _NotCanDrag = false; _Tips = ""; int num = Mathf.CeilToInt(UITools.GetSlider(_UISlider) * _AllCount); if (num < _MinCount) { num = _MinCount; _NotCanDrag = true;// 最少要选择一个 _Tips = _MinTips; } else if (num > _MaxCount) { num = _MaxCount;// 最多要全选择 _NotCanDrag = true;// 最少要选择一个 _Tips = _MaxTips; } _SelectCount = num; UpdataCount(); } void OnDragFinished() { if (_NotCanDrag)// 不能拖拽提示 { if (string.IsNullOrEmpty(_Tips) == false) UITemplate.ShowInfoTips(_Tips); } _NotCanDrag = false; _Tips = ""; } }

转载请注明原文地址: https://www.6miu.com/read-4073.html

最新回复(0)