基于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
0
投稿
猜你喜欢
- 在springboot中,默认继承好了一套完好的redis包,可以直接使用,但是如果使用中出了错不容易找到错误的原因,因此这里使用自己配置的
- Interceptor讲到Interceptor,相信熟悉struts2的童鞋肯定不会陌生了,struts2可以自定义 * 进行自己想要的一
- 1、redis的几种常见客户端:Jedis:是Redis的Java实现客户端,提供了比较全面的Redis命令的支持;Redisson:实现了
- java web 实现分页功能,分享给大家,具体如下:使用框架:ssm数据库:oracle话说 oracle 的分页查询比 mysql 复杂
- 下截JNative组件jnative.sourceforge.net/ 到这里下载JNative开源项目,我下载的是1.3.2解压JNati
- 对象POJO和JSON互转public class JsonUtil { /** * JSON 转 POJO &n
- 前言使用SpringBoot来开发项目相对于传统模式,要快速优雅许多,相信目前国内绝大部分web项目的开发还没有使用SpringBoot来做
- 1.为什么要 token自动续期token中一般会包含用户的基本信息,为了保证token的安全性,一般会将token的过期时间设置的比较短,
- 代码很简单,直接上代码,大家参考使用吧package com.it.login.service;import java.io.Buffere
- 这篇文章主要介绍了Java String的intern用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 问题描述本来开发工具安装的是IDEA2018,有天用着用着突然崩溃了,重启后死活用不了。心血来潮下载了2019版本,顺利安装完,但是点击快捷
- yml 文件规则yml文件的好处,天然的树状结构,一目了然,实质上跟properties是差不多的。不支持tab缩进可以使用 "-
- 在生产环境中,需要实时或定期监控服务的可用性。spring-boot 的actuator(监控)功能提供了很多监控所需的接口。简单的配置和使
- 通过Class对象获取对象的方式是通过class.newInstance()方式获取,通过调用默认构造参数实例化一个对象。/** * Cre
- 基于SMTP发送一个简单的邮件首先,需要一个认证器:package No001_基于SMTP的文本邮件;import javax.mail.
- iText介绍和说明因为项目需要生成PDF文件,所以去找了一下能够生成PDF的Java工具,看到了iText可以说好评如潮。如果你想通过ja
- android线程消息机制主要由Handler,Looper,Message和MessageQuene四个部分组成。平常在开发中,我们常用来
- 本文实例讲述了C#数字图像处理之图像缩放的方法。分享给大家供大家参考。具体如下://定义图像缩放函数private static Bitma
- 前言:想象一下,有一个服务提供个多个客户端调用,但不是所有客户端都需要全部的返回参数:比如商品列表服务返回商品的所有信息,而订单服务调用商品
- using System;using System.Collections.Generic;using System.IO;using Sy