在此对老虎机进行相对简单的模拟,只是实现无限循环、缓停、最后归位功能,代码未进行重构,仅此记录一个思路。
代码:
/************************************************************
FileName: PositionController.cs
Author:末零 Version :1.0 Date: 2018-7-17
Description:老虎机模拟
************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PositionController: MonoBehaviour {
public List<Transform> sprites = new List<Transform>();
private Transform lastSprite;
private int indexResetPos;
private int indexMove;
public float speed = 0;
private void Awake()
{
GetAll();
}
private void Update ()
{
StartMave();
Move();
ResetPos();
StopMove();
}
/// <summary>
/// 初始化
/// </summary>
private void GetAll()
{
for (int i = 0; i < transform.childCount; i++)
{
sprites.Add(transform.GetChild(i));
}
lastSprite = sprites[sprites.Count - 1];
}
/// <summary>
/// 楷书滚动
/// </summary>
private void StartMave()
{
if (Input.GetMouseButtonDown(0))
{
speed = 10;
}
}
/// <summary>
/// 滚动
/// </summary>
private void Move()
{
for (indexMove = 0; indexMove < sprites.Count; indexMove++)
{
sprites[indexMove].transform.localPosition = new Vector3(sprites[indexMove].transform.localPosition.x, sprites[indexMove].transform.localPosition.y, sprites[indexMove].transform.localPosition.z) + new Vector3(0, 1, 0)*speed;
}
}
/// <summary>
/// 重置坐标
/// </summary>
private void ResetPos()
{
if (sprites[indexResetPos].localPosition.y > 100)
{
sprites[indexResetPos].localPosition = new Vector3(lastSprite.localPosition.x, lastSprite.localPosition.y - 100, lastSprite.localPosition.z);
lastSprite = sprites[indexResetPos];
indexResetPos++;
if (indexResetPos >= sprites.Count)
indexResetPos = 0;
}
}
/// <summary>
/// 停止滚动
/// </summary>
private void StopMove()
{
if(Input.GetKeyDown(KeyCode.Space))
{
if (speed < 10)
return;
StartCoroutine(SlowDown(0.5f));
}
}
/// <summary>
/// 减速
/// </summary>
/// <param name="del"></param>
/// <returns></returns>
IEnumerator SlowDown(float del)
{
while (speed > 0)
{
yield return new WaitForSeconds(0.1f);
speed -= del;
if (speed <= 0)
{
StartCoroutine(ReturnZero());
}
}
}
/// <summary>
/// 位置归零
/// </summary>
/// <returns></returns>
IEnumerator ReturnZero()
{
float dis = sprites[0].localPosition.y;
int id = 0;
for (int j = 1; j < sprites.Count; j++)
{
if (Mathf.Abs(sprites[j].localPosition.y) < Mathf.Abs(dis))
{
dis = sprites[j].localPosition.y;
id = j;
}
}
while (Mathf.Abs(sprites[id].localPosition.y) > 0.5f)
{
yield return new WaitForSeconds(0.001f);
for (int k = 0; k < sprites.Count; k++)
{
sprites[k].localPosition -= new Vector3(0, dis / 100, 0);
}
}
#region 此处代码不够精简,但上面代码还不够完善,故未删除
//if (dis < 0)
//{
// while (sprites[id].localPosition.y < 0)
// {
// yield return new WaitForSeconds(0.001f);
// for (int k = 0; k < sprites.Count; k++)
// {
// sprites[k].localPosition -= new Vector3(0, dis / 100, 0);
// }
// }
//}
//else
//{
// while (sprites[id].localPosition.y > 0)
// {
// yield return new WaitForSeconds(0.001f);
// for (int k = 0; k < sprites.Count; k++)
// {
// sprites[k].localPosition -= new Vector3(0, dis / 100, 0);
// }
// }
//}
#endregion
}
}
效果图: