软件编程
位置:首页>> 软件编程>> C#编程>> 基于Unity编写一个九宫格抽奖软件

基于Unity编写一个九宫格抽奖软件

作者:恬静的小魔龙  发布时间:2022-12-30 21:02:00 

标签:Unity,九宫格,抽奖

一、前言

本博文标题和内容参考:基于原生JS实现H5转盘游戏

博主将改编成Unity版本。

二、效果图

基于Unity编写一个九宫格抽奖软件

三、案例制作

1.界面搭建

基于Unity编写一个九宫格抽奖软件

使用了9个图片作为奖品栏,然后一个chooseBox作为蒙版,一个StartBtn开始按钮放在中间

基于Unity编写一个九宫格抽奖软件

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);
           }
       }
   }
}

然后将这个脚本挂载到一个游戏对象上:

基于Unity编写一个九宫格抽奖软件

BoxList里面的对象,按照顺序拖进去。

效果图:

基于Unity编写一个九宫格抽奖软件

概率设置

代码:

//控制概率
   //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);

算法理解:

基于Unity编写一个九宫格抽奖软件

然后代码修改一下,解决两个问题:

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.效果演示

基于Unity编写一个九宫格抽奖软件

四、后言

这是一个简单的抽奖系统,可以控制概率,也可以不传递概率数组,就会返回一个随机值。

也可以设置一下概率,比如:

{10, 20, 0, 20, 20, 0, 20, 10 }

也就是:

基于Unity编写一个九宫格抽奖软件

反正加起来概率不要超过100就行。

来源:https://itmonon.blog.csdn.net/article/details/117767474

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com