滑动选择时间效果
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Test1 : MonoBehaviour,IBeginDragHandler,IDragHandler
{
/// <summary>
/// 根据手指滑动距离得出一个比率来计算方法执行次数
/// </summary>
public Text text1, text2, text3;
private int count1,count2,count3;
private Vector2 oldPos,newPos;
public void OnBeginDrag(PointerEventData eventData)
{
oldPos = eventData.position;
}
public void OnDrag(PointerEventData eventData)
{
newPos = eventData.position;
if ((newPos - oldPos).magnitude >= 20)
{
//DoLoop1(newPos.y - oldPos.y);
DoNotLoop1(newPos.y - oldPos.y);
oldPos = newPos;
}
}
void Start()
{
count1 = 0;
count2 = 1;
count3 = 2;
}
void Update()
{
}
public void DoNotLoop()
{
//未实现数字循环
if (text1.text != "")//设置临界点
{
if (Input.GetKeyDown(KeyCode.A) && int.Parse(text1.text) >= 0)
{
if (int.Parse(text1.text) == 0)
{
text1.text = "";
text2.text = (int.Parse(text2.text) - 1).ToString();
text3.text = (int.Parse(text3.text) - 1).ToString();
}
else
{
text1.text = (int.Parse(text1.text) - 1).ToString();
text2.text = (int.Parse(text2.text) - 1).ToString();
if (text3.text == "")//变为空串后需要赋值字符串
{
text3.text = "23";
}
else
{
text3.text = (int.Parse(text3.text) - 1).ToString();
}
}
}
}
if (text3.text != "")//设置临界点
{
if (Input.GetKeyDown(KeyCode.B) && int.Parse(text3.text) <= 23)
{
if (int.Parse(text3.text) == 23)
{
text1.text = (int.Parse(text1.text) + 1).ToString();
text2.text = (int.Parse(text2.text) + 1).ToString();
text3.text = "";
}
else
{
if (text1.text == "")//变为空串后需要赋值字符串
{
text1.text = "0";
}
else
{
text1.text = (int.Parse(text1.text) + 1).ToString();
}
text2.text = (int.Parse(text2.text) + 1).ToString();
text3.text = (int.Parse(text3.text) + 1).ToString();
}
}
}
}
public void DoNotLoop1(float y)
{
//未实现数字循环
if (y < 0)
{
if (text1.text != "")//设置临界点
{
if (int.Parse(text1.text) >= 0)
{
if (int.Parse(text1.text) == 0)
{
text1.text = "";
text2.text = (int.Parse(text2.text) - 1).ToString();
text3.text = (int.Parse(text3.text) - 1).ToString();
}
else
{
text1.text = (int.Parse(text1.text) - 1).ToString();
text2.text = (int.Parse(text2.text) - 1).ToString();
if (text3.text == "")//变为空串后需要赋值字符串
{
text3.text = "23";
}
else
{
text3.text = (int.Parse(text3.text) - 1).ToString();
}
}
}
}
}
if (y > 0)
{
if (text3.text != "")//设置临界点
{
if (int.Parse(text3.text) <= 23)
{
if (int.Parse(text3.text) == 23)
{
text1.text = (int.Parse(text1.text) + 1).ToString();
text2.text = (int.Parse(text2.text) + 1).ToString();
text3.text = "";
}
else
{
if (text1.text == "")//变为空串后需要赋值字符串
{
text1.text = "0";
}
else
{
text1.text = (int.Parse(text1.text) + 1).ToString();
}
text2.text = (int.Parse(text2.text) + 1).ToString();
text3.text = (int.Parse(text3.text) + 1).ToString();
}
}
}
}
}
public void DoLoop()
{
//实现数字循环
if (Input.GetKeyDown(KeyCode.A))
{
if (--count1 != -1 && count1 >= 0)
{
++count1;
count1 -= 1;
text1.text = count1.ToString();
}
else
{
count1 = 23;
text1.text = count1.ToString();
}
if (--count2 != -1 && count2 >= 0)
{
++count2;
count2 -= 1;
text2.text = count2.ToString();
}
else
{
count2 = 23;
text2.text = count2.ToString();
}
if (--count3 != -1 && count3 >= 0)
{
++count3;
count3 -= 1;
text3.text = count3.ToString();
}
else
{
count3 = 23;
text3.text = count3.ToString();
}
}
if (Input.GetKeyDown(KeyCode.B))
{
if (++count1 != 24 && count1 <= 23)
{
--count1;
count1 += 1;
text1.text = count1.ToString();
}
else
{
count1 = 0;
text1.text = count1.ToString();
}
if (++count2 != 24 && count2 <= 23)
{
--count2;
count2 += 1;
text2.text = count2.ToString();
}
else
{
count2 = 0;
text2.text = count2.ToString();
}
if (++count3 != 24 && count3 <= 23)
{
--count3;
count3 += 1;
text3.text = count3.ToString();
}
else
{
count3 = 0;
text3.text = count3.ToString();
}
}
}
public void DoLoop1(float y)
{
//实现数字循环
if (y < 0)
{
if (--count1 != -1 && count1 >= 0)
{
++count1;
count1 -= 1;
text1.text = count1.ToString();
}
else
{
count1 = 23;
text1.text = count1.ToString();
}
if (--count2 != -1 && count2 >= 0)
{
++count2;
count2 -= 1;
text2.text = count2.ToString();
}
else
{
count2 = 23;
text2.text = count2.ToString();
}
if (--count3 != -1 && count3 >= 0)
{
++count3;
count3 -= 1;
text3.text = count3.ToString();
}
else
{
count3 = 23;
text3.text = count3.ToString();
}
}
if (y > 0)
{
if (++count1 != 24 && count1 <= 23)
{
--count1;
count1 += 1;
text1.text = count1.ToString();
}
else
{
count1 = 0;
text1.text = count1.ToString();
}
if (++count2 != 24 && count2 <= 23)
{
--count2;
count2 += 1;
text2.text = count2.ToString();
}
else
{
count2 = 0;
text2.text = count2.ToString();
}
if (++count3 != 24 && count3 <= 23)
{
--count3;
count3 += 1;
text3.text = count3.ToString();
}
else
{
count3 = 0;
text3.text = count3.ToString();
}
}
}
}
``
1. 需要三个Text,第一个跟最后一个的Text文本颜色跟中间Text的文本颜色不同,效果就类似列表滑动
