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

 更新时间:2022年03月10日 15:47:22   作者:恬静的小魔龙  
这篇文章主要为大家介绍了如何利用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抽奖的资料请关注脚本之家其它相关文章!

相关文章

  • c# 在Emit代码中如何await一个异步方法

    c# 在Emit代码中如何await一个异步方法

    这篇文章主要介绍了c# 在Emit代码中如何await一个异步方法,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • C#设置子窗体在主窗体中居中显示解决方案

    C#设置子窗体在主窗体中居中显示解决方案

    接下来将介绍C#如何设置子窗体在主窗体中居中显示,本文提供详细的操作步骤,需要的朋友可以参考下
    2012-12-12
  • VsCode使用EmmyLua插件调试Unity工程Lua代码的详细步骤

    VsCode使用EmmyLua插件调试Unity工程Lua代码的详细步骤

    这篇文章主要介绍了VsCode使用EmmyLua插件调试Unity工程Lua代码,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • C#读取配置文件的方法汇总

    C#读取配置文件的方法汇总

    本文给大家介绍的是使用C#读取配置文件的方法,个人给大家总结了6种,余下的以后再更新,有需要的小伙伴可以参考下。
    2015-06-06
  • C#实现Word转为PDF的方法

    C#实现Word转为PDF的方法

    今天小编就为大家分享一篇关于C#实现Word转为PDF的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • C#实现单例模式的6种方法小结

    C#实现单例模式的6种方法小结

    这篇文章主要介绍了C#实现单例模式的6种方法,C#中实现单例有很多种方法,本文将按顺序介绍非线程安全、完全懒汉式、线程安全和低/高性能集中版本,需要的朋友可以参考下
    2022-09-09
  • C# 打开电子邮件软件的具体方法

    C# 打开电子邮件软件的具体方法

    这篇文章介绍了C# 打开电子邮件软件的具体方法,有需要的朋友可以参考一下
    2013-11-11
  • 详解C#通过反射获取对象的几种方式比较

    详解C#通过反射获取对象的几种方式比较

    本文主要介绍了C#通过反射获取对象的几种方式比较,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • C#程序(含多个Dll)合并成一个Exe的简单方法

    C#程序(含多个Dll)合并成一个Exe的简单方法

    这篇文章主要为大家详细介绍了C#程序(含多个Dll)合并成一个Exe的简单方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • c# 接口使用实例

    c# 接口使用实例

    这篇文章主要介绍了c#接口使用的实例,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07

最新评论