Unity之跑馬燈抽獎(jiǎng)效果單抽與連抽(附demo)
本文主要介紹了Unity之跑馬燈抽獎(jiǎng)效果單抽與連抽,分享給大家,具體如下:
效果圖
單次抽獎(jiǎng)效果
跳過動(dòng)畫抽獎(jiǎng)效果
三連抽抽獎(jiǎng)效果
設(shè)計(jì)思路 點(diǎn)擊按鈕 ,根據(jù)需求(概率)計(jì)算本次抽獎(jiǎng)獲得物品模擬轉(zhuǎn)動(dòng) (先加速后減速), 一段時(shí)間后停止連抽的情況下等所有獎(jiǎng)品動(dòng)畫都表演完成才結(jié)束跳過動(dòng)畫設(shè)計(jì),有跳過時(shí)抽獎(jiǎng)速度直接到最大,并進(jìn)入可中獎(jiǎng) 場(chǎng)景搭建
一個(gè)按鈕,一個(gè)組獎(jiǎng)品放到一個(gè)父物體上。
獎(jiǎng)品元素,有兩種狀態(tài),一種旋轉(zhuǎn)狀態(tài),一種中獎(jiǎng)狀態(tài)。
代碼
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// <summary> /// 跑馬燈轉(zhuǎn)盤 /// </summary> public class RotaryTablePanel : MonoBehaviour { //單次開始抽獎(jiǎng)抽獎(jiǎng)結(jié)束的事件 private Action<bool> PlayingAction; //三連抽開始抽獎(jiǎng)抽獎(jiǎng)結(jié)束的事件 private Action<bool> PlayingThreeAction; //是否是三連抽 bool isThreeDraw; // 抽獎(jiǎng)按鈕, public Button drawBtn; //跳過抽獎(jiǎng)動(dòng)畫 public Toggle jumpTgl; // 抽獎(jiǎng)圖片父物體 public Transform rewardImgTran; //轉(zhuǎn)動(dòng)特效 public Transform eff_TurnFrame; //中獎(jiǎng)特效 public Transform eff_SelectFrame; // 抽獎(jiǎng)圖片 private Transform[] rewardTransArr; private RotaryCell[] rewardCellArr; // 默認(rèn)展示狀態(tài) private bool isInitState; // 抽獎(jiǎng)結(jié)束 -- 結(jié)束狀態(tài),光環(huán)不轉(zhuǎn) private bool drawEnd; // 中獎(jiǎng) private bool drawWinning; [Header("展示狀態(tài)時(shí)間 --> 控制光環(huán)轉(zhuǎn)動(dòng)初始速度")] public float setrewardTime = 1f; private float rewardTime; private float rewardTiming = 0; // 當(dāng)前光環(huán)所在獎(jiǎng)勵(lì)的索引 private int haloIndex = 0; // 本次中獎(jiǎng)ID private int rewardIndex = 0; // 點(diǎn)了抽獎(jiǎng)按鈕正在抽獎(jiǎng) private bool isOnClickPlaying; public bool IsOnClickPlaying { get => isOnClickPlaying; set { isOnClickPlaying = value; if (eff_TurnFrame != null) { eff_TurnFrame.gameObject.SetActive(isOnClickPlaying); } } } public bool DrawWinning { get => drawWinning; set => drawWinning = value; } public bool DrawEnd { get => drawEnd; set { drawEnd = value; if (eff_SelectFrame != null) { eff_SelectFrame.gameObject.SetActive(drawEnd); } } } /// <summary> /// 注冊(cè)轉(zhuǎn)盤抽獎(jiǎng)事件 /// </summary> /// <param name="playingAction"></param> public void SetPlayingAction(Action<bool> playingAction, Action<bool> playingThreeAction) { PlayingAction = playingAction; PlayingThreeAction = playingThreeAction; } public void Start() { Init(); } public void Init() { drawBtn.onClick.AddListener(OnClickDrawFun); rewardTransArr = new Transform[rewardImgTran.childCount]; rewardCellArr = new RotaryCell[rewardImgTran.childCount]; for (int i = 0; i < rewardImgTran.childCount; i++) { rewardTransArr[i] = rewardImgTran.GetChild(i); rewardCellArr[i] = rewardTransArr[i].GetComponent<RotaryCell>(); } // 默認(rèn)展示時(shí)間 rewardTime = setrewardTime; rewardTiming = 0; DrawEnd = false; DrawWinning = false; IsOnClickPlaying = false; } public void RePrepare() { if (IsOnClickPlaying) { return; } rewardTime = setrewardTime; rewardTiming = 0; DrawEnd = false; DrawWinning = false; IsOnClickPlaying = false; if (true) { for (int i = 0; i < rewardCellArr.Length; i++) { rewardCellArr[i].ShowEff(RotaryCell.EffType.all, false); } } } /// <summary> /// 從中獎(jiǎng)狀態(tài)恢復(fù)到默認(rèn)狀態(tài) /// </summary> /// <param name="index"></param> public void RestoreDefault(int index = 0) { index--; rewardCellArr[index].ShowEff(RotaryCell.EffType.all, false); } void Update() { if (Input.GetKeyDown(KeyCode.F)) { RePrepare(); } if (Input.GetKeyDown(KeyCode.H)) { OnClickDrawFunThree(); } if (DrawEnd || rewardCellArr == null) return; if (!IsOnClickPlaying) { return; } // 抽獎(jiǎng)?wù)故? rewardTiming += Time.deltaTime; if (rewardTiming >= rewardTime) { rewardTiming = 0; haloIndex++; if (haloIndex >= rewardCellArr.Length) { haloIndex = 0; } if (isThreeDraw) SetHaloThreePos(haloIndex); else SetHaloPos(haloIndex); } } // 設(shè)置光環(huán)顯示位置 void SetHaloPos(int index) { rewardCellArr[index - 1 < 0 ? rewardCellArr.Length - 1 : index - 1].ShowEff(RotaryCell.EffType.turn, false); rewardCellArr[index].ShowEff(RotaryCell.EffType.turn, true); // 中獎(jiǎng) && 此ID == 中獎(jiǎng)ID if (DrawWinning && index == rewardIndex) { rewardCellArr[index].ShowEff(RotaryCell.EffType.select, true); rewardCellArr[index].ShowEff(RotaryCell.EffType.turn, false); IsOnClickPlaying = false; DrawEnd = true; if (PlayingAction != null) { PlayingAction(false); } //todo...展示中獎(jiǎng)物品,維護(hù)數(shù)據(jù) --> 注意: index是索引 Debug.Log("恭喜您中獎(jiǎng),中獎(jiǎng)物品索引是:" + index + "號(hào)"); } } void SetHaloThreePos(int index) { rewardCellArr[index - 1 < 0 ? rewardCellArr.Length - 1 : index - 1].ShowEff(RotaryCell.EffType.turn, false); rewardCellArr[index].ShowEff(RotaryCell.EffType.turn, true); // 中獎(jiǎng) && 此ID == 中獎(jiǎng)ID if (DrawWinning && index == indexList.Peek()) { rewardCellArr[index].GetComponent<RotaryCell>().ShowEff(RotaryCell.EffType.select, true); rewardCellArr[index].GetComponent<RotaryCell>().ShowEff(RotaryCell.EffType.turn, false); indexList.Dequeue(); //todo...展示中獎(jiǎng)物品,維護(hù)數(shù)據(jù) --> 注意: index是索引 Debug.Log("恭喜您三連抽中獎(jiǎng),中獎(jiǎng)物品索引是:" + index + "號(hào)"); if (indexList.Count == 0) { if (PlayingThreeAction != null) { PlayingThreeAction(false); } IsOnClickPlaying = false; DrawEnd = true; isThreeDraw = false; return; } if (jumpTgl != null && jumpTgl.isOn) { rewardTime = 0.02f; DrawWinning = true; } else { rewardTime = setrewardTime; rewardTiming = 0; DrawWinning = false; StartCoroutine(StartDrawAni()); } } } // 點(diǎn)擊抽獎(jiǎng)按鈕 void OnClickDrawFun() { if (!IsOnClickPlaying) { haloIndex = -1; RePrepare(); // 隨機(jī)抽中ID rewardIndex = UnityEngine.Random.Range(0, rewardCellArr.Length); Debug.Log("開始抽獎(jiǎng),本次抽獎(jiǎng)隨機(jī)到的ID是:" + rewardIndex); IsOnClickPlaying = true; DrawEnd = false; DrawWinning = false; if (PlayingAction != null) { PlayingAction(true); } if (jumpTgl != null && jumpTgl.isOn) { rewardTime = 0.02f; DrawWinning = true; } else StartCoroutine(StartDrawAni()); } } // 點(diǎn)擊抽獎(jiǎng)按鈕 public void OnClickDrawFun(int index) { haloIndex = -1; isThreeDraw = false; rewardIndex = index - 1;//給lua提供方法,減1 if (!IsOnClickPlaying) { RePrepare(); Debug.Log("開始抽獎(jiǎng),本次抽獎(jiǎng)到的ID是:" + rewardIndex); IsOnClickPlaying = true; DrawEnd = false; DrawWinning = false; if (PlayingAction != null) { PlayingAction(true); } if (jumpTgl != null && jumpTgl.isOn) { rewardTime = 0.02f; DrawWinning = true; } else StartCoroutine(StartDrawAni()); } } Queue<int> indexList = new Queue<int>(); public void OnClickDrawFunThree(Queue<int> _table) { haloIndex = -1; isThreeDraw = true; if (!IsOnClickPlaying) { RePrepare(); IsOnClickPlaying = true; DrawEnd = false; DrawWinning = false; if (PlayingThreeAction != null) { PlayingThreeAction(true); } if (jumpTgl != null && jumpTgl.isOn) { rewardTime = 0.02f; DrawWinning = true; } else StartCoroutine(StartDrawAni()); } } public void OnClickDrawFunThree() { haloIndex = -1; isThreeDraw = true; indexList.Enqueue(3); indexList.Enqueue(7); indexList.Enqueue(5); //rewardIndex = indexList.Peek(); if (!IsOnClickPlaying) { RePrepare(); IsOnClickPlaying = true; DrawEnd = false; DrawWinning = false; if (PlayingThreeAction != null) { PlayingThreeAction(true); } if (jumpTgl != null && jumpTgl.isOn) { rewardTime = 0.02f; DrawWinning = true; } else StartCoroutine(StartDrawAni()); } } /// <summary> /// 開始抽獎(jiǎng)動(dòng)畫 /// 先快后慢 -- 根據(jù)需求調(diào)整時(shí)間 /// </summary> /// <returns></returns> IEnumerator StartDrawAni() { rewardTime = setrewardTime; // 加速 for (int i = 0; i < setrewardTime / 0.05f - 1; i++) { yield return new WaitForSeconds(0.05f); rewardTime -= 0.05f; } yield return new WaitForSeconds(2f); // 減速 for (int i = 0; i < setrewardTime / 0.05f - 4; i++) { yield return new WaitForSeconds(0.05f); rewardTime += 0.02f; } yield return new WaitForSeconds(0.5f); DrawWinning = true; } public void OnDestroy() { Debug.Log("C#的關(guān)閉"); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RotaryCell : MonoBehaviour { public Transform[] turnEff; public Transform[] seletEff; public enum EffType { turn, select, all, } public void ShowEff(EffType efftype, bool isShow) { switch (efftype) { case EffType.turn: for (int i = 0; i < turnEff.Length; i++) { turnEff[i].gameObject.SetActive(isShow); } break; case EffType.select: for (int i = 0; i < turnEff.Length; i++) { seletEff[i].gameObject.SetActive(isShow); } break; case EffType.all: for (int i = 0; i < turnEff.Length; i++) { turnEff[i].gameObject.SetActive(isShow); seletEff[i].gameObject.SetActive(isShow); } break; default: break; } } public void HideAllEff() { for (int i = 0; i < turnEff.Length; i++) { turnEff[i].gameObject.SetActive(false); } for (int i = 0; i < turnEff.Length; i++) { seletEff[i].gameObject.SetActive(false); } } IEnumerator HideEffAni() { yield return new WaitForSeconds(0.1f); for (int i = 0; i < turnEff.Length; i++) { turnEff[i].gameObject.SetActive(false); } } }
工程項(xiàng)目
到此這篇關(guān)于Unity之跑馬燈抽獎(jiǎng)效果單抽與連抽(附demo)的文章就介紹到這了,更多相關(guān)Unity 跑馬燈抽獎(jiǎng) 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中比較常用的DateTime結(jié)構(gòu)的使用方法
這篇文章主要介紹了C#中比較常用的DateTime結(jié)構(gòu)的使用方法,需要的朋友可以參考下2015-11-11C#調(diào)用Python程序傳參數(shù)獲得返回值
C# 調(diào)用 Python 程序有多種方式,本文主要介紹了4種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02C#中數(shù)組Array,ArrayList,泛型List詳細(xì)對(duì)比
關(guān)于數(shù)組Array,ArrayList,泛型List,簡(jiǎn)單的說(shuō)數(shù)組就是值對(duì)象,它存儲(chǔ)數(shù)據(jù)元素類型的值的一系列位置.Arraylist和list可以提供添加,刪除,等操作的數(shù)據(jù). 具體如何進(jìn)行選擇使用呢,我們來(lái)詳細(xì)探討下2016-06-06c#使用htmlagilitypack解析html格式字符串
這篇文章主要介紹了c#使用htmlagilitypack解析html格式字符串的示例,需要的朋友可以參考下2014-03-03C#實(shí)現(xiàn)一鍵換IP、重置DNS、網(wǎng)關(guān)及掩碼的方法
這篇文章主要介紹了C#實(shí)現(xiàn)一鍵換IP、重置DNS、網(wǎng)關(guān)及掩碼的方法,很實(shí)用的功能,需要的朋友可以參考下2014-07-07C# DoubleClick與MouseDoubleClick區(qū)別,雙擊事件引發(fā)順序
從邏輯上來(lái)說(shuō),由于比MouseDoubleClick 描述更抽象,DoubleClick 事件是控件的更高級(jí)別的事件2009-09-09