欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Unity編寫一個(gè)九宮格抽獎(jiǎng)軟件

 更新時(shí)間:2022年03月10日 15:47:22   作者:恬靜的小魔龍  
這篇文章主要為大家介紹了如何利用Unity編寫一個(gè)年會抽獎(jiǎng)軟件,還能設(shè)置中獎(jiǎng)概率。文中的示例代碼講解詳細(xì),感興趣的可以了解一下

一、前言

本博文標(biāo)題和內(nèi)容參考:基于原生JS實(shí)現(xiàn)H5轉(zhuǎn)盤游戲

博主將改編成Unity版本。

二、效果圖

三、案例制作

1.界面搭建

使用了9個(gè)圖片作為獎(jiǎng)品欄,然后一個(gè)chooseBox作為蒙版,一個(gè)StartBtn開始按鈕放在中間

2.代碼編寫

新建腳本goLuckyDraw.cs

使用DoTween插件做動畫,沒有導(dǎo)入這個(gè)插件的下載導(dǎo)入一下

實(shí)現(xiàn)抽獎(jiǎng),主要有兩個(gè)方面,一個(gè)是概率的設(shè)置,一個(gè)是動畫

動畫

我使用一個(gè)蒙版用來表示當(dāng)前選中的獎(jiǎng)品,然后不斷將蒙版移動到下一個(gè)獎(jiǎng)品的位置,就這樣形成一個(gè)動畫的效果:

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;//蒙版要到達(dá)的位置
    public Button button;//開始按鈕

    void Start()
    {
        transparentBox.gameObject.SetActive(false);
        //獲取需要監(jiān)聽的按鈕對象
        button.onClick.AddListener(() =>
        {
                StartLuckyDraw();
        });
    }

    private void StartLuckyDraw()
    {
        chooseBox = boxList[_index];

        transparentBox.gameObject.SetActive(true);
        StartCoroutine(Move());
    }

    IEnumerator Move()
    {
        float time = 0.2f;
        //下次開始旋轉(zhuǎn)的位置等于上次旋轉(zhuǎn)到的位置
        for (int i = 0; i < boxList.Count; i++)
        {
            transparentBox.transform.DOLocalMove(boxList[i].localPosition, time);
            yield return new WaitForSeconds(time);
        }
        //旋轉(zhuǎn)兩圈
        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);
        }
        //當(dāng)旋轉(zhuǎn)到指定的位置的時(shí)候結(jié)束
        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);
            }
        }
    }
}

然后將這個(gè)腳本掛載到一個(gè)游戲?qū)ο笊希?/p>

BoxList里面的對象,按照順序拖進(jìn)去。

效果圖:

概率設(shè)置

代碼:

    //控制概率
    //rate:幾率數(shù)組(%),  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;
        }
    }

這個(gè)將一個(gè)概率數(shù)組傳遞進(jìn)去,就可以控制概率了:

int[] AA = { 10, 10, 10, 10, 10, 10, 10, 30 };
int _index = randomNum(AA);
//獲得得獎(jiǎng)的下標(biāo)數(shù)字
Debug.Log(_index);

算法理解:

然后代碼修改一下,解決兩個(gè)問題:

1、點(diǎn)擊頻率問題

2、下一次轉(zhuǎn)的時(shí)候不從當(dāng)前位置轉(zhuǎn)的問題

完整代碼如下:

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;//蒙版要到達(dá)的位置
    public Button button;//開始按鈕
    private bool isRotate = false;//控制點(diǎn)擊頻率
    int index = 0;//轉(zhuǎn)盤轉(zhuǎn)到的位置記錄

    void Start()
    {
        transparentBox.gameObject.SetActive(false);
        //獲取需要監(jiān)聽的按鈕對象
        button.onClick.AddListener(() =>
        {
            if (!isRotate)
            {
                StartLuckyDraw();
            }
        });
    }

    private void StartLuckyDraw()
    {
        isRotate = true;
        //隨機(jī)概率可控制
        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:幾率數(shù)組(%),  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;
        //下次開始旋轉(zhuǎn)的位置等于上次旋轉(zhuǎn)到的位置
        for (int i = index; i < boxList.Count; i++)
        {
            transparentBox.transform.DOLocalMove(boxList[i].localPosition, time);
            yield return new WaitForSeconds(time);
        }
        index = _index;
        //旋轉(zhuǎn)兩圈
        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);
        }
        //當(dāng)旋轉(zhuǎn)到指定的位置的時(shí)候結(jié)束
        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.效果演示

四、后言

這是一個(gè)簡單的抽獎(jiǎng)系統(tǒng),可以控制概率,也可以不傳遞概率數(shù)組,就會返回一個(gè)隨機(jī)值。

也可以設(shè)置一下概率,比如:

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

也就是:

反正加起來概率不要超過100就行。

以上就是基于Unity編寫一個(gè)九宮格抽獎(jiǎng)軟件的詳細(xì)內(nèi)容,更多關(guān)于Unity抽獎(jiǎng)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • c# 在Emit代碼中如何await一個(gè)異步方法

    c# 在Emit代碼中如何await一個(gè)異步方法

    這篇文章主要介紹了c# 在Emit代碼中如何await一個(gè)異步方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C#設(shè)置子窗體在主窗體中居中顯示解決方案

    C#設(shè)置子窗體在主窗體中居中顯示解決方案

    接下來將介紹C#如何設(shè)置子窗體在主窗體中居中顯示,本文提供詳細(xì)的操作步驟,需要的朋友可以參考下
    2012-12-12
  • VsCode使用EmmyLua插件調(diào)試Unity工程Lua代碼的詳細(xì)步驟

    VsCode使用EmmyLua插件調(diào)試Unity工程Lua代碼的詳細(xì)步驟

    這篇文章主要介紹了VsCode使用EmmyLua插件調(diào)試Unity工程Lua代碼,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • C#讀取配置文件的方法匯總

    C#讀取配置文件的方法匯總

    本文給大家介紹的是使用C#讀取配置文件的方法,個(gè)人給大家總結(jié)了6種,余下的以后再更新,有需要的小伙伴可以參考下。
    2015-06-06
  • C#實(shí)現(xiàn)Word轉(zhuǎn)為PDF的方法

    C#實(shí)現(xiàn)Word轉(zhuǎn)為PDF的方法

    今天小編就為大家分享一篇關(guān)于C#實(shí)現(xiàn)Word轉(zhuǎn)為PDF的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • C#實(shí)現(xiàn)單例模式的6種方法小結(jié)

    C#實(shí)現(xiàn)單例模式的6種方法小結(jié)

    這篇文章主要介紹了C#實(shí)現(xiàn)單例模式的6種方法,C#中實(shí)現(xiàn)單例有很多種方法,本文將按順序介紹非線程安全、完全懶漢式、線程安全和低/高性能集中版本,需要的朋友可以參考下
    2022-09-09
  • C# 打開電子郵件軟件的具體方法

    C# 打開電子郵件軟件的具體方法

    這篇文章介紹了C# 打開電子郵件軟件的具體方法,有需要的朋友可以參考一下
    2013-11-11
  • 詳解C#通過反射獲取對象的幾種方式比較

    詳解C#通過反射獲取對象的幾種方式比較

    本文主要介紹了C#通過反射獲取對象的幾種方式比較,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • C#程序(含多個(gè)Dll)合并成一個(gè)Exe的簡單方法

    C#程序(含多個(gè)Dll)合并成一個(gè)Exe的簡單方法

    這篇文章主要為大家詳細(xì)介紹了C#程序(含多個(gè)Dll)合并成一個(gè)Exe的簡單方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • c# 接口使用實(shí)例

    c# 接口使用實(shí)例

    這篇文章主要介紹了c#接口使用的實(shí)例,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07

最新評論