173 lines
4.6 KiB
C#
173 lines
4.6 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 数字选择器
|
|
/// </summary>
|
|
public class NumPicker : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
|
{
|
|
public int maxNum;
|
|
public int minNum;
|
|
public int _itemNum = 5;
|
|
[HideInInspector]
|
|
/// <summary>
|
|
/// 更新选择的目标值
|
|
/// </summary>
|
|
public float _updateLength;
|
|
/// <summary>
|
|
/// 子节点预制体
|
|
/// </summary>
|
|
public GameObject _itemObj;
|
|
/// <summary>
|
|
/// 子节点容器对象
|
|
/// </summary>
|
|
public Transform _itemParent;
|
|
public Color normalColor;
|
|
public int value;
|
|
public Action<int> onValueChange;
|
|
void Awake()
|
|
{
|
|
_itemObj.SetActive(false);
|
|
_updateLength = _itemObj.GetComponent<RectTransform>().sizeDelta.y/2;
|
|
}
|
|
private void Start()
|
|
{
|
|
// Init(10);
|
|
}
|
|
/// <summary>
|
|
/// 初始化数字选择器
|
|
/// </summary>
|
|
public void Init(int num, Action<int> onValueChange)
|
|
{
|
|
this.onValueChange = onValueChange;
|
|
value = num;
|
|
for (int i = 0; i < _itemNum; i++)
|
|
{
|
|
//计算真实位置
|
|
int real_i = -(_itemNum / 2) + i;
|
|
SpawnItem(real_i);
|
|
}
|
|
RefreshDateList();
|
|
}
|
|
/// <summary>
|
|
/// 生成子对象
|
|
/// </summary>
|
|
/// <param name="real_i">真实位置</param>
|
|
/// <returns></returns>
|
|
GameObject SpawnItem(float real_i)
|
|
{
|
|
GameObject _item = Instantiate(_itemObj);
|
|
_item.SetActive(true);
|
|
_item.transform.SetParent(_itemParent);
|
|
_item.transform.localScale = Vector3.one;
|
|
_item.transform.localEulerAngles = Vector3.zero;
|
|
_item.transform.localPosition = Vector3.zero;
|
|
if (real_i != 0)
|
|
{
|
|
_item.GetComponent<Text>().color = normalColor;
|
|
// _item.transform.eulerAngles += new Vector3(20 * Mathf.Abs(real_i), 0, 0);
|
|
_item.GetComponent<RectTransform>().sizeDelta -= new Vector2(0, 80);
|
|
// _item.GetComponent<Text>().fontSize -= 30;
|
|
//_item.GetComponent<Text>().fontStyle = FontStyle.Normal;
|
|
}
|
|
return _item;
|
|
}
|
|
|
|
Vector3 oldDragPos;
|
|
/// <summary>
|
|
/// 当开始拖拽
|
|
/// </summary>
|
|
/// <param name="eventData"></param>
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
oldDragPos = eventData.position;
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
UpdateSelectDate(eventData);
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
_itemParent.localPosition = Vector3.zero;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新选择的数字
|
|
/// </summary>
|
|
/// <param name="eventData"></param>
|
|
void UpdateSelectDate(PointerEventData eventData)
|
|
{
|
|
//判断拖拽的长度是否大于目标值
|
|
if (Mathf.Abs(eventData.position.y - oldDragPos.y) >= _updateLength)
|
|
{
|
|
int num;
|
|
//判断加减
|
|
if (eventData.position.y > oldDragPos.y)
|
|
{
|
|
//+
|
|
num = (int)(Mathf.Abs(eventData.position.y - oldDragPos.y) / _updateLength);
|
|
}
|
|
else
|
|
{
|
|
//-
|
|
num = -(int)(Mathf.Abs(eventData.position.y - oldDragPos.y) / _updateLength);
|
|
}
|
|
value += num;
|
|
|
|
//判断是否属于时间段
|
|
if (IsInRange(value))
|
|
{
|
|
oldDragPos = eventData.position;
|
|
RefreshDateList();
|
|
}
|
|
else
|
|
{
|
|
value -= num;
|
|
RefreshDateList();
|
|
}
|
|
onValueChange?.Invoke(value);
|
|
_itemParent.localPosition = Vector3.zero;
|
|
|
|
}
|
|
else
|
|
{
|
|
_itemParent.localPosition = new Vector3(0, (eventData.position.y - oldDragPos.y), 0);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 刷新时间列表
|
|
/// </summary>
|
|
public void RefreshDateList()
|
|
{
|
|
for (int i = 0; i < _itemNum; i++)
|
|
{
|
|
//计算真实位置
|
|
int real_i = -(_itemNum / 2) + i;
|
|
|
|
string str = "";
|
|
if (IsInRange(value+ real_i))
|
|
{
|
|
int nowvalue=value+real_i;
|
|
if(nowvalue<10)
|
|
str ="0"+nowvalue;
|
|
else
|
|
str=nowvalue.ToString();
|
|
|
|
}
|
|
_itemParent.GetChild(i).GetComponent<Text>().text = str;
|
|
}
|
|
|
|
}
|
|
public bool IsInRange(int value)
|
|
{
|
|
return value>=minNum &&value<=maxNum ;
|
|
}
|
|
} |