網(wǎng)站首頁 編程語言 正文
一、前言
本博文標(biāo)題和內(nèi)容參考:基于原生JS實現(xiàn)H5轉(zhuǎn)盤游戲
博主將改編成Unity版本。
二、效果圖
三、案例制作
1.界面搭建
使用了9個圖片作為獎品欄,然后一個chooseBox作為蒙版,一個StartBtn開始按鈕放在中間
2.代碼編寫
新建腳本goLuckyDraw.cs
使用DoTween插件做動畫,沒有導(dǎo)入這個插件的下載導(dǎo)入一下
實現(xiàn)抽獎,主要有兩個方面,一個是概率的設(shè)置,一個是動畫
動畫
我使用一個蒙版用來表示當(dāng)前選中的獎品,然后不斷將蒙版移動到下一個獎品的位置,就這樣形成一個動畫的效果:
using System.Collections; using System.Collections.Generic; using DG.Tweening; using UnityEngine; using UnityEngine.UI; public class goLuckyDraw : MonoBehaviour { public Image transparentBox;//蒙版 public ListboxList = new List ();//所有的位置對象 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)到指定的位置的時候結(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); } } } }
然后將這個腳本掛載到一個游戲?qū)ο笊希?/p>
BoxList里面的對象,按照順序拖進去。
效果圖:
概率設(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; } }
這個將一個概率數(shù)組傳遞進去,就可以控制概率了:
int[] AA = { 10, 10, 10, 10, 10, 10, 10, 30 }; int _index = randomNum(AA); //獲得得獎的下標(biāo)數(shù)字 Debug.Log(_index);
算法理解:
然后代碼修改一下,解決兩個問題:
1、點擊頻率問題
2、下一次轉(zhuǎn)的時候不從當(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 ListboxList = new List ();//所有的位置對象 private Transform chooseBox;//蒙版要到達(dá)的位置 public Button button;//開始按鈕 private bool isRotate = false;//控制點擊頻率 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; //隨機概率可控制 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)到指定的位置的時候結(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.效果演示
四、后言
這是一個簡單的抽獎系統(tǒng),可以控制概率,也可以不傳遞概率數(shù)組,就會返回一個隨機值。
也可以設(shè)置一下概率,比如:
{10, 20, 0, 20, 20, 0, 20, 10 }
也就是:
反正加起來概率不要超過100就行。
原文鏈接:https://itmonon.blog.csdn.net/article/details/117767474
相關(guān)推薦
- 2022-06-23 muduo源碼分析之TcpServer模塊詳細(xì)介紹_Redis
- 2022-12-27 Android?Compose狀態(tài)改變動畫animateXxxAsState使用詳解_Android
- 2022-10-14 matlab非線性最小二乘擬合
- 2023-01-10 利用C#實現(xiàn)修改圖片透明度功能_C#教程
- 2022-07-13 Docker的數(shù)據(jù)管理
- 2022-07-15 go?GCM?gin中間件的加密解密文件流處理_Golang
- 2022-10-29 在Centos8中安裝配置Redis,實現(xiàn)遠(yuǎn)程訪問
- 2023-10-09 所有的引用類型都有自由可拓展性怎么理解
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支