一、演示效果
二、使用工具
Unity:2019.4.36
VS:2017
三、场景搭建
1、右键—>UI—>RawImage
(自己摆好位置并设置好大小,然后选择图片,挂载RawImageTest脚本。本案例需要创建6个RawImage,分别是一个背景图片、三列水果和两块遮板,背景图片必须放在最上面)
2、右键—>UI—>Button
(同样自己摆好位置并修改大小,然后修改一下自己需要显示的字。这两个按钮是用来控制游戏的开始和暂停的,具体逻辑是当没有按下开始按钮时,暂停按钮不允许被点击;当按下开始按钮后,暂停按钮就被激活,同时开始按钮就不允许被点击了;当按下暂停按钮,暂停按钮立即不允许被点击,开始按钮必须等老虎机停止才被激活)
3、创建一个空物体,用来挂载游戏控制脚本
4、总体预览
四、代码
1、RawImageTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RawImageTest : MonoBehaviour
{
static int totalNum; // 场景里该脚本的总数
static int stopNum; // 当前已经停止的数量
bool selfIsStop = true; // 自己是否停止
RawImage raw;
Rect r;
public float speed;
float[] a = new float[] { 0, 0.2f, 0.4f, 0.6f, 0.8f }; //每个图片的位置
int n;
float m;
public GameControl gameControl;
// Start is called before the first frame update
void Start()
{
totalNum++;
raw = GetComponent<RawImage>();
r = raw.uvRect;
n = Random.Range(0, 5);
r.y = a[n];
raw.uvRect = r;
gameControl = GameObject.Find("GameObject").GetComponent<GameControl>();
speed = Random.Range(0.5f, 2f);
m = Random.Range(0.001f, 0.004f);
}
// Update is called once per frame
void Update()
{
if (gameControl.state == GameState.Start) //当按下开始键
{
stopNum = 0; //重置停止的脚本数量
selfIsStop = false;
r.y += Time.deltaTime * speed;
raw.uvRect = r;
}
else if (gameControl.state == GameState.Pause)
{
if (!selfIsStop)
{
if (speed > 0)
{
speed -= m;
r.y += Time.deltaTime * speed;
raw.uvRect = r;
}
else
{
selfIsStop = true;
speed = Random.Range(0.5f, 2f);
m = Random.Range(0.001f, 0.004f);
r.y = a[GetPos(r.y)];
raw.uvRect = r;
stopNum++;
if (stopNum >= totalNum)
{
gameControl.state = GameState.Stop;
}
}
}
}
}
//获取将要停止的位置
private int GetPos(float num)
{
float t = num - Mathf.Floor(num); //获取小数部分
for (int i = 0; i < 4; i++)
{
if (t >= a[i] && t <= a[i + 1])
{
return i + 1;
}
}
return 0;
}
}
2、GameControl.cs文章来源:https://www.toymoban.com/news/detail-599267.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //引入命名空间
public enum GameState //用来记录三种状态
{
Start,
Pause,
Stop
}
public class GameControl : MonoBehaviour
{
public Button StartBtn; //通过Unity中拖拽获得
public Button PauseBtn;
public GameState state;
bool isStart = false;
// Start is called before the first frame update
void Start()
{
StartBtn.interactable = true;
PauseBtn.interactable = false;
StartBtn.onClick.AddListener(StartGame); //给按钮添加事件
PauseBtn.onClick.AddListener(PauseGame);
state = GameState.Stop;
}
// Update is called once per frame
void Update()
{
if (isStart && state == GameState.Stop)
{
StartBtn.interactable = true;
}
}
public void StartGame()
{
isStart = false;
StartBtn.interactable = false;
PauseBtn.interactable = true;
state = GameState.Start;
}
public void PauseGame()
{
isStart = true;
PauseBtn.interactable = false;
state = GameState.Pause;
}
}
五、项目链接
链接:https://pan.baidu.com/s/1tDwAbKd4zHEO0ySXXTY7_w
提取码:pjqm文章来源地址https://www.toymoban.com/news/detail-599267.html
到了这里,关于Unity简单实现老虎机抽奖效果的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!