Unity之跑馬燈抽獎(jiǎng)效果單抽與連抽(附demo)
本文主要介紹了Unity之跑馬燈抽獎(jiǎng)效果單抽與連抽,分享給大家,具體如下:
效果圖
單次抽獎(jiǎng)效果

跳過(guò)動(dòng)畫(huà)抽獎(jiǎng)效果

三連抽抽獎(jiǎng)效果

設(shè)計(jì)思路 點(diǎn)擊按鈕 ,根據(jù)需求(概率)計(jì)算本次抽獎(jiǎng)獲得物品模擬轉(zhuǎn)動(dòng) (先加速后減速), 一段時(shí)間后停止連抽的情況下等所有獎(jiǎng)品動(dòng)畫(huà)都表演完成才結(jié)束跳過(guò)動(dòng)畫(huà)設(shè)計(jì),有跳過(guò)時(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)盤(pán)
/// </summary>
public class RotaryTablePanel : MonoBehaviour
{
//單次開(kāi)始抽獎(jiǎng)抽獎(jiǎng)結(jié)束的事件
private Action<bool> PlayingAction;
//三連抽開(kāi)始抽獎(jiǎng)抽獎(jiǎng)結(jié)束的事件
private Action<bool> PlayingThreeAction;
//是否是三連抽
bool isThreeDraw;
// 抽獎(jiǎng)按鈕,
public Button drawBtn;
//跳過(guò)抽獎(jiǎng)動(dòng)畫(huà)
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)盤(pá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("開(kāi)始抽獎(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("開(kāi)始抽獎(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>
/// 開(kāi)始抽獎(jiǎng)動(dòng)畫(huà)
/// 先快后慢 -- 根據(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)文章
unity實(shí)現(xiàn)鼠標(biāo)跟隨(ITween)
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)鼠標(biāo)跟隨,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C# 循環(huán)判斷會(huì)進(jìn)來(lái)幾次的實(shí)現(xiàn)代碼
這篇文章主要介紹了C# 循環(huán)判斷會(huì)進(jìn)來(lái)幾次的實(shí)現(xiàn)代碼,代碼中就一個(gè)循環(huán),循環(huán)的判斷是從一個(gè)函數(shù)獲取值,需要的朋友可以參考下2018-06-06
Unity實(shí)現(xiàn)角色受擊身體邊緣發(fā)光特效
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)角色受擊身體邊緣發(fā)光特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
C#調(diào)用QQ_Mail發(fā)送郵件實(shí)例代碼兩例
這篇文章介紹了C#調(diào)用QQ_Mail發(fā)送郵件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
Unity3D開(kāi)發(fā)教程:憤怒的小鳥(niǎo)
這篇文章詳細(xì)的講解了如何從0開(kāi)發(fā)出一個(gè)Unity3D的小游戲憤怒的小鳥(niǎo),本文包含大量的圖片與文字描述,也含有大量的源代碼,可以讓你快速入手,希望本篇文章對(duì)你有所幫助2021-06-06

