服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - 基于Unity编写一个九宫格抽奖软件

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

2023-02-21 15:46恬静的小魔龙 C#

这篇文章主要为大家介绍了如何利用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就行。

以上就是基于Unity编写一个九宫格抽奖软件的详细内容,更多关于Unity抽奖的资料请关注服务器之家其它相关文章!

原文链接:https://itmonon.blog.csdn.net/article/details/117767474

延伸 · 阅读

精彩推荐
  • C#C#中的DateTime是值类型还是引用类型

    C#中的DateTime是值类型还是引用类型

    近期遇到了DateTime到底是值类型还是引用类型的疑惑,顺势较深入地了解一下DateTime相关的内容,大家有需要的朋友可以参考下...

    zhixin90018412021-12-31
  • C#C#提取网页中超链接link和text部分的方法

    C#提取网页中超链接link和text部分的方法

    这篇文章主要介绍了C#提取网页中超链接link和text部分的方法,涉及C#正则表达式及字符串操作相关技巧,需要的朋友可以参考下...

    礼拜一3722021-11-11
  • C#C#编程实现动态改变配置文件信息的方法

    C#编程实现动态改变配置文件信息的方法

    这篇文章主要介绍了C#编程实现动态改变配置文件信息的方法,涉及C#针对xml格式文件的相关操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    aparche3842021-11-24
  • C#C#常用数据结构和算法总结

    C#常用数据结构和算法总结

    这篇文章主要介绍了C#常用数据结构和算法,这里我们总结了一些知识点,可以帮助大家理解这些概念。...

    贺臣12272021-11-29
  • C#C#如何通过T4自动生成代码详解

    C#如何通过T4自动生成代码详解

    这篇文章主要给大家介绍了关于C#如何通过T4自动生成代码的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值...

    FreeTimeWorker9642022-03-02
  • C#C#实现截图工具小项目

    C#实现截图工具小项目

    这篇文章主要为大家详细介绍了C#实现截图工具小项目,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    *八步赶蝉*4012022-11-27
  • C#C#获取日期的星期名称实例代码

    C#获取日期的星期名称实例代码

    本文通过实例代码给大家介绍了基于c#获取日期的星期名称,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧...

    _York7602022-02-28
  • C#C#对多个集合和数组的操作方法(合并,去重,判断)

    C#对多个集合和数组的操作方法(合并,去重,判断)

    下面小编就为大家带来一篇C#对多个集合和数组的操作方法(合并,去重,判断)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过...

    C#教程网11182021-12-14