Unity實(shí)現(xiàn)老虎機(jī)滾動抽獎效果的示例代碼
直接看下效果圖吧:
制作思路:
設(shè)計(jì)四張圖片,五個點(diǎn),每個圖片同時向下一個點(diǎn)移動,到最后一個就回到0號點(diǎn),以此循環(huán)。
場景搭建:
- 創(chuàng)建Image命名為Bg作為電視框背景;
- 創(chuàng)建Image命名Mask并添加Mask組件作為電視框內(nèi)容顯示遮罩框;
- 創(chuàng)建四個Image作為滾動圖片;
- 創(chuàng)建開始抽獎按鈕;
PS:實(shí)際項(xiàng)目中可以根據(jù)需求來動態(tài)修改圖片顯示,以達(dá)到的控制每次抽獎獎品內(nèi)容。
源碼分享:
using System.Collections; using UnityEngine; using UnityEngine.UI; public class ScollToDraw : MonoBehaviour { // 抽獎按鈕 public Button DrowBtn; // 獎勵圖片 public Image[] ArardImgArr; // 轉(zhuǎn)盤速度 public float AniMoveSpeed = 3f; // 進(jìn)度 private float[] progress = new[] {0f, 1f, 2f, 3f, 4f}; // 轉(zhuǎn)動動畫位置 private Vector3[] AniPosV3 = new[] {Vector3.up * 240, Vector3.up * 120, Vector3.zero, Vector3.down * 120, Vector3.down * 240}; // 自動暫停標(biāo)識 private bool isAutoStop; // 抽獎結(jié)束 停止刷新界面UI private bool isStopUpdatePos; void Start() { DrowBtn.onClick.AddListener(DrawFun); isAutoStop = false; isStopUpdatePos = false; } void Update() { if (isStopUpdatePos) return; float t = Time.deltaTime * AniMoveSpeed; for (int i = 0; i < ArardImgArr.Length; i++) { progress[i] += t; ArardImgArr[i].transform.localPosition = MovePosition(i); } } // 獲取下一個移動到的位置 Vector3 MovePosition(int i) { int index = Mathf.FloorToInt(progress[i]); if (index > AniPosV3.Length - 2) { //保留其小數(shù)部分,不能直接賦值為0 progress[i] -= index; index = 0; // 索引為2的到底了,索引為0的就在正中心 if (i == 2 && isAutoStop) { isStopUpdatePos = true; Debug.Log("展示獎勵界面..."); // todo...獲取獎勵數(shù)據(jù)維護(hù) } return AniPosV3[index]; } else { return Vector3.Lerp(AniPosV3[index], AniPosV3[index + 1], progress[i] - index); } } /// <summary> /// 點(diǎn)擊抽獎 /// </summary> void DrawFun() { isAutoStop = false; isStopUpdatePos = false; StartCoroutine(SetMoveSpeed(2)); // DoTween 按鈕下拉動畫 // Transform tran = DrowBtn.transform; //tran.DOLocalMoveY(-60, 0.2f).OnComplete(() => //{ // tran.DOLocalMoveY(50, 0.2f); // //}); } // 抽獎動畫速度控制 IEnumerator SetMoveSpeed(int time) { AniMoveSpeed = 10; yield return new WaitForSeconds(time); AniMoveSpeed = 1; yield return new WaitForSeconds(time); isAutoStop = true; } }
到此這篇關(guān)于Unity實(shí)現(xiàn)老虎機(jī)滾動抽獎效果的示例代碼的文章就介紹到這了,更多相關(guān)Unity老虎機(jī)滾動抽獎內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# Socket 發(fā)送&接收&返回 簡單應(yīng)用實(shí)例
下面小編就為大家分享一篇C# Socket 發(fā)送&接收&返回 簡單應(yīng)用實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11解析C#設(shè)計(jì)模式編程中備忘錄模式的運(yùn)用
這篇文章主要介紹了C#設(shè)計(jì)模式編程中備忘錄模式的運(yùn)用,備忘錄模式用來保存與對象有關(guān)的數(shù)據(jù)用以在將來對對象進(jìn)行復(fù)原,需要的朋友可以參考下2016-02-02C# DataTable數(shù)據(jù)遍歷優(yōu)化詳解
這篇文章主要介紹了C# DataTable數(shù)據(jù)遍歷優(yōu)化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01