这篇文章主要为大家介绍了如何利用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就行。
以上就是基于Unity编写一个九宫格抽奖软件的详细内容,更多关于Unity抽奖的资料请关注得得之家其它相关文章!
本文标题为:基于Unity编写一个九宫格抽奖软件
				
        
 
            
        基础教程推荐
- C#使用Chart绘制曲线 2023-05-22
 - C#实现归并排序 2023-05-31
 - C#中参数的传递方式详解 2023-06-27
 - C#执行EXE文件与输出消息的提取操作 2023-04-14
 - C# TreeView从数据库绑定数据的示例 2023-04-09
 - 如何用C#创建用户自定义异常浅析 2023-04-21
 - C#使用NPOI将excel导入到list的方法 2023-05-22
 - Unity虚拟摇杆的实现方法 2023-02-16
 - 浅谈C# 构造方法(函数) 2023-03-03
 - C#使用SQL DataAdapter数据适配代码实例 2023-01-06
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				