实现效果如下:
实现效果为:外部传入数值,正常显示 lhj抽取效果
用户可自定义行为:1,抽取的显示;2,抽取的最低时间
实现代码如下,可自行改写:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
/// <summary>
/// 老虎机
/// </summary>
public class SlotMachine : MonoBehaviour
{
//private float startPosY;
private int[] moveAdd = new int[3] { 0, 0, 0 };
private bool[] playArray = new bool[3] { false, false, false };
private int[] showArray = new int[3] { 0, 1, 2 };
public float MinTime = 3f;
private float minTime = 0f;
private float moveFx = 0.2f;//移动系数,需要能被1整除:0.1;0.2;0.25;0.5
private float moveLen;
private int moveNum = 5;
private Action callBackAc;
//public float UpdateSp = 0.05f;
/ <summary>
/ 传入类型
/ </summary>
//public void Born()
//{
// var _messT = transform.Find("1").GetChild(1);
// moveLen = _messT.GetComponent<RectTransform>().rect.height * moveFx;
// Debug.LogError(moveNum + " " + moveLen + " " + (1 / moveFx) + " "/*+startPosY*/);
//}
public void ClickPlay(int _one, int _two, int _three,Action _callAc)
{
playArray[0] = true;
playArray[1] = true;
playArray[2] = true;
showArray[0] = _one;
showArray[1] = _two;
showArray[2] = _three;
minTime = MinTime + Time.time;
callBackAc = null;
callBackAc = _callAc;
}
// Start is called before the first frame update
void Start()
{
//Born();
var _messT = transform.Find("1").GetChild(1);
moveLen = _messT.GetComponent<RectTransform>().rect.height * moveFx;
}
//private float updateTime = 0f;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A)) ClickPlay(1, 2, 2,delegate { Debug.Log("打印"); });
for (var _i = 0; _i < 3; _i++)
{
if (playArray[_i])
{
string _name = "";
bool _isUpdate = UpdateDown(_i, out _name);
if (Time.time > minTime && _isUpdate && _name == showArray[_i].ToString())
{
switch (_i)
{
case 0: playArray[_i] = false; break;
case 1: if (!playArray[0]) playArray[_i] = false; break;
case 2: if (!playArray[1] && playArray[_i]) { playArray[_i] = false;/* Debug.LogError("执行");*/ callBackAc?.Invoke(); } break;
}
//Debug.Log(_name + " ~~~~~~~ " + _i);
}
}
}
}
/// <summary>
/// 模拟不断下落的过程
/// </summary>
private bool UpdateDown(int _i, out string _imageName)
{
var _tran = transform.Find(_i.ToString());
_imageName = "";
var _down = moveLen * Vector3.down;
foreach (Transform idx in _tran)
idx.GetComponent<RectTransform>().localPosition += _down;
moveAdd[_i] += 1;
//Debug.Log(moveAdd[_i] + " " + moveNum);
var _isR = moveAdd[_i] == moveNum;
if (_isR)
{
_tran.GetChild(_tran.childCount - 1).SetSiblingIndex(0);
_imageName = _tran.GetChild(2).name;
//Debug.Log(_i + " " + _imageName);
}
_tran.GetComponent<ContentSizeFitter>().enabled = _isR;
_tran.GetComponent<VerticalLayoutGroup>().enabled = _isR;
if (_isR) moveAdd[_i] = 0;
return _isR;
}
}
demo下载地址:简单实用的抽奖显示插件资源-CSDN文库