131 lines
3.2 KiB
C#
131 lines
3.2 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using Module.Log;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
public class RollNum : MonoBehaviour
|
|||
|
{
|
|||
|
// 需配置部分
|
|||
|
public float maxSpeed; // 转动最大速度
|
|||
|
public float deltaSpeed; // 加速度
|
|||
|
public float height; // 10个数字的总长度(一周高度)
|
|||
|
public float childHeight; // 每个数字高度
|
|||
|
// ----------
|
|||
|
|
|||
|
private float curSpeed; // 当前速度
|
|||
|
private RectTransform rt; // 当前物体Rt组件,控制位移
|
|||
|
private float deltaSlowSpeed; // 减速加速度
|
|||
|
private float roundLenght; // 转动一圈的路程
|
|||
|
private float totalLength; // 总路程
|
|||
|
private float tempLength; // 补足长度
|
|||
|
private int des; // 需要的转动结果
|
|||
|
|
|||
|
public delegate void voidNoneDel();
|
|||
|
public event voidNoneDel FinishEvent; // 展示结束事件
|
|||
|
|
|||
|
private void OnEnable()
|
|||
|
{
|
|||
|
// 归位
|
|||
|
rt = transform as RectTransform;
|
|||
|
rt.anchoredPosition = new Vector2(rt.anchoredPosition.x, 0.0f);
|
|||
|
roundLenght = 10 * childHeight;
|
|||
|
curSpeed = 0.0f;
|
|||
|
}
|
|||
|
|
|||
|
private void Update()
|
|||
|
{
|
|||
|
rt.anchoredPosition += Vector2.up * curSpeed * Time.deltaTime;
|
|||
|
|
|||
|
if (rt.anchoredPosition.y > height)
|
|||
|
{
|
|||
|
float newBegin = rt.anchoredPosition.y - height;
|
|||
|
rt.anchoredPosition = new Vector2(rt.anchoredPosition.x, newBegin);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void SetAndBegin(int des)
|
|||
|
{
|
|||
|
if(des >= 0 && des <= 9)
|
|||
|
{
|
|||
|
this.des = des;
|
|||
|
StopAllCoroutines();
|
|||
|
StartCoroutine("SpeedUp");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
LogModule.ErrorLog("Des is wrong, please reset it");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private IEnumerator SpeedUp()
|
|||
|
{
|
|||
|
curSpeed = 0.0f;
|
|||
|
while (true)
|
|||
|
{
|
|||
|
curSpeed += Time.deltaTime * deltaSpeed;
|
|||
|
if (curSpeed > maxSpeed)
|
|||
|
{
|
|||
|
curSpeed = maxSpeed;
|
|||
|
BeginSlow();
|
|||
|
yield break;
|
|||
|
}
|
|||
|
|
|||
|
yield return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void BeginSlow()
|
|||
|
{
|
|||
|
StopAllCoroutines();
|
|||
|
|
|||
|
if(des < 0 || des > 9)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (des * childHeight >= rt.anchoredPosition.y)
|
|||
|
{
|
|||
|
tempLength = des * childHeight - rt.anchoredPosition.y;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
tempLength = des * childHeight + roundLenght - rt.anchoredPosition.y;
|
|||
|
}
|
|||
|
|
|||
|
totalLength = tempLength + 3 * roundLenght;
|
|||
|
|
|||
|
StartCoroutine("SlowDown");
|
|||
|
}
|
|||
|
|
|||
|
private IEnumerator SlowDown()
|
|||
|
{
|
|||
|
yield return 0;
|
|||
|
|
|||
|
deltaSlowSpeed = maxSpeed * maxSpeed / (2.0f * totalLength);
|
|||
|
while (true)
|
|||
|
{
|
|||
|
curSpeed -= deltaSlowSpeed * Time.deltaTime;
|
|||
|
if (curSpeed < 0.0f)
|
|||
|
{
|
|||
|
curSpeed = 0.0f;
|
|||
|
|
|||
|
if(FinishEvent != null)
|
|||
|
{
|
|||
|
FinishEvent.Invoke();
|
|||
|
}
|
|||
|
|
|||
|
yield break;
|
|||
|
}
|
|||
|
|
|||
|
yield return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//// 结束后的修正过程
|
|||
|
//private IEnumerator FinallyCorrect()
|
|||
|
//{
|
|||
|
|
|||
|
//}
|
|||
|
}
|