基于Unity编写一个九宫格抽奖软件
作者:恬静的小魔龙 发布时间:2022-12-30 21:02:00
标签:Unity,九宫格,抽奖
一、前言
本博文标题和内容参考:基于原生JS实现H5转盘游戏
博主将改编成Unity版本。
二、效果图
三、案例制作
1.界面搭建
使用了9个图片作为奖品栏,然后一个chooseBox作为蒙版,一个StartBtn开始按钮放在中间
2.代码编写
新建脚本goLuckyDraw.cs
使用DoTween插件做动画,没有导入这个插件的下载导入一下
实现抽奖,主要有两个方面,一个是概率的设置,一个是动画
动画
我使用一个蒙版用来表示当前选中的奖品,然后不断将蒙版移动到下一个奖品的位置,就这样形成一个动画的效果:
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
public class goLuckyDraw : MonoBehaviour
{
public Image transparentBox;//蒙版
public List<Transform> boxList = new List<Transform>();//所有的位置对象
private Transform chooseBox;//蒙版要到达的位置
public Button button;//开始按钮
void Start()
{
transparentBox.gameObject.SetActive(false);
//获取需要监听的按钮对象
button.onClick.AddListener(() =>
{
StartLuckyDraw();
});
}
private void StartLuckyDraw()
{
chooseBox = boxList[_index];
transparentBox.gameObject.SetActive(true);
StartCoroutine(Move());
}
IEnumerator Move()
{
float time = 0.2f;
//下次开始旋转的位置等于上次旋转到的位置
for (int i = 0; i < boxList.Count; i++)
{
transparentBox.transform.DOLocalMove(boxList[i].localPosition, time);
yield return new WaitForSeconds(time);
}
//旋转两圈
for (int i = 0; i < boxList.Count; i++)
{
transparentBox.transform.DOLocalMove(boxList[i].localPosition, time);
yield return new WaitForSeconds(time);
}
for (int i = 0; i < boxList.Count; i++)
{
transparentBox.transform.DOLocalMove(boxList[i].localPosition, time);
yield return new WaitForSeconds(time);
}
//当旋转到指定的位置的时候结束
for (int i = 0; i < boxList.Count; i++)
{
if (transparentBox.transform.localPosition == chooseBox.localPosition)
{
transparentBox.transform.DOLocalMove(chooseBox.localPosition, time);
continue;
}
else
{
transparentBox.transform.DOLocalMove(boxList[i].localPosition, time);
yield return new WaitForSeconds(time);
}
}
}
}
然后将这个脚本挂载到一个游戏对象上:
BoxList里面的对象,按照顺序拖进去。
效果图:
概率设置
代码:
//控制概率
//rate:几率数组(%), total:几率总和(100%)
private int randomNum(int[] rate, int total=100)
{
if (rate == null)
{
int r = Random.Range(1, 7);
return r;
}
else
{
int r = Random.Range(1, total + 1);
int t = 0;
for (int i = 0; i < rate.Length; i++)
{
t += rate[i];
if (r < t)
{
return i;
}
}
return 0;
}
}
这个将一个概率数组传递进去,就可以控制概率了:
int[] AA = { 10, 10, 10, 10, 10, 10, 10, 30 };
int _index = randomNum(AA);
//获得得奖的下标数字
Debug.Log(_index);
算法理解:
然后代码修改一下,解决两个问题:
1、点击频率问题
2、下一次转的时候不从当前位置转的问题
完整代码如下:
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;
public class goLuckyDraw : MonoBehaviour
{
public Image transparentBox;//蒙版
public List<Transform> boxList = new List<Transform>();//所有的位置对象
private Transform chooseBox;//蒙版要到达的位置
public Button button;//开始按钮
private bool isRotate = false;//控制点击频率
int index = 0;//转盘转到的位置记录
void Start()
{
transparentBox.gameObject.SetActive(false);
//获取需要监听的按钮对象
button.onClick.AddListener(() =>
{
if (!isRotate)
{
StartLuckyDraw();
}
});
}
private void StartLuckyDraw()
{
isRotate = true;
//随机概率可控制
int[] AA = { 10, 10, 10, 10, 10, 10, 10, 30 };
int _index = randomNum(AA);
Debug.Log(_index);
chooseBox = boxList[_index];
transparentBox.gameObject.SetActive(true);
StartCoroutine(Move(_index));
}
//控制概率
//rate:几率数组(%), total:几率总和(100%)
private int randomNum(int[] rate, int total=100)
{
if (rate == null)
{
int r = Random.Range(0, 7);
return r;
}
else
{
int r = Random.Range(1, total + 1);
int t = 0;
for (int i = 0; i < rate.Length; i++)
{
t += rate[i];
if (r < t)
{
return i;
}
}
return 0;
}
}
IEnumerator Move(int _index)
{
float time = 0.2f;
//下次开始旋转的位置等于上次旋转到的位置
for (int i = index; i < boxList.Count; i++)
{
transparentBox.transform.DOLocalMove(boxList[i].localPosition, time);
yield return new WaitForSeconds(time);
}
index = _index;
//旋转两圈
for (int i = 0; i < boxList.Count; i++)
{
transparentBox.transform.DOLocalMove(boxList[i].localPosition, time);
yield return new WaitForSeconds(time);
}
for (int i = 0; i < boxList.Count; i++)
{
transparentBox.transform.DOLocalMove(boxList[i].localPosition, time);
yield return new WaitForSeconds(time);
}
//当旋转到指定的位置的时候结束
for (int i = 0; i < boxList.Count; i++)
{
if (transparentBox.transform.localPosition == chooseBox.localPosition)
{
transparentBox.transform.DOLocalMove(chooseBox.localPosition, time);
continue;
}
else
{
transparentBox.transform.DOLocalMove(boxList[i].localPosition, time);
yield return new WaitForSeconds(time);
}
}
isRotate = false;
}
}
3.效果演示
四、后言
这是一个简单的抽奖系统,可以控制概率,也可以不传递概率数组,就会返回一个随机值。
也可以设置一下概率,比如:
{10, 20, 0, 20, 20, 0, 20, 10 }
也就是:
反正加起来概率不要超过100就行。
来源:https://itmonon.blog.csdn.net/article/details/117767474


猜你喜欢
- 用过新版本android 360手机助手都人都对 360中只在桌面显示一个小小悬浮窗口羡慕不已吧? 其实实现这种功能,主要有两步: 1.判断
- 废话不多说了,直接给大家贴java代码了。 import java.io.IOException;import sun.net.Telnet
- 简介本次五子棋使用的是光标控制移动,通过按空格键(键值32)来落子,实现游戏的。我们额外用到的头文件有:#include<getch.
- 前言Spring Boot中在yaml中编写的自定义变量、数组、对象等,在代码中读取该yaml配置文件中内容的三种方式。实现在代码中运用配置
- “深入浅出,人人都是程序员”开发过android手机的同学都知道在eclipse中可以直接查找到SHA1值,但是使用intellij ide
- 本文以C#代码为例介绍如何实现将彩色PDF文件转为灰度(黑白)的PDF文件,即 将PDF文档里面的彩色图片或者文字等通过调用PdfGrayC
- 环境:springcloud Hoxton.SR11本节主要了解系统中的谓词与配置的路由信息是如何进行初始化关联生成路由对象的。每个谓词工厂
- 前言各位精通CRUD的老司机,相信大家在工作中mybatis或者mybatisplus使用的肯定是比较多的,那么大家或多或少都应该对下面的行
- 在开发中,我们经常会使用IO操作,例如创建,删除文件等操作。在项目中这样的需求也较多,我们也会经常对这些操作进行编
- 一、日志的分类1、名字分类log4j :log for java (因为for和4读音差不多,所以交log4j)logBack 日志说明注意
- 前言通常在DAL层我们都需要把DataTable转换为List<T>让调用者尽可能的好用,尽量的不用关心数据库的字段等,所以我们
- 本文实例为大家分享了Spring boot多线程配置的具体代码,供大家参考,具体内容如下1、配置线程配置类package test;impo
- Sentinel流控模式Sentinel流量控制主要有以下几种模式:直接失败模式:在达到流量控制阈值后,直接拒绝请求,返回错误信息。关联模式
- 目录引言API介绍1、Optional(),empty(),of(),ofNullable()2、orElse(),orElseGet()和
- 开发 Web 应用的思路实现一个简单的 JSP/Servlet。搭建创建 Web 应用工程的环境。创建 Web 应用工程。Web 应用工程的
- Android的控件有很多种,其中就有一个Spinner的控件,这个控件其实就是一个下拉显示列表。Spinner是位于 andr
- Android Studio从3.0版本新增了许多功能,当然首当其冲就是从3.0版本新增了对 Kotlin 开发语言的支持,除此之外还有其他
- 一、什么叫做匿名类?匿名类就是没有名字的类。匿名类不能被引用,只能再创建的时候用new语句来声明。二、匿名类的优势以及应用场景;1、匿名类型
- JavaWeb项目部署到服务器详细步骤本地准备在eclipse中将项目打成war文件:鼠标右键要部署到服务器上的项目导出项目数据库文件MyS
- 在java开发的实际场景中,我们经常要对时间进行格式化处理,但是每次获取开发中自己需要的格式都要重新写一个方法,这样的代码看起来是非常的笨重