Unity實現老虎機滾動抽獎效果的示例代碼
更新時間:2021年04月26日 09:55:22 作者:佛系老陳
這篇文章主要介紹了Unity實現老虎機滾動抽獎效果的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
直接看下效果圖吧:
制作思路:
設計四張圖片,五個點,每個圖片同時向下一個點移動,到最后一個就回到0號點,以此循環(huán)。
場景搭建:
- 創(chuàng)建Image命名為Bg作為電視框背景;
- 創(chuàng)建Image命名Mask并添加Mask組件作為電視框內容顯示遮罩框;
- 創(chuàng)建四個Image作為滾動圖片;
- 創(chuàng)建開始抽獎按鈕;
PS:實際項目中可以根據需求來動態(tài)修改圖片顯示,以達到的控制每次抽獎獎品內容。
源碼分享:
using System.Collections; using UnityEngine; using UnityEngine.UI; public class ScollToDraw : MonoBehaviour { // 抽獎按鈕 public Button DrowBtn; // 獎勵圖片 public Image[] ArardImgArr; // 轉盤速度 public float AniMoveSpeed = 3f; // 進度 private float[] progress = new[] {0f, 1f, 2f, 3f, 4f}; // 轉動動畫位置 private Vector3[] AniPosV3 = new[] {Vector3.up * 240, Vector3.up * 120, Vector3.zero, Vector3.down * 120, Vector3.down * 240}; // 自動暫停標識 private bool isAutoStop; // 抽獎結束 停止刷新界面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) { //保留其小數部分,不能直接賦值為0 progress[i] -= index; index = 0; // 索引為2的到底了,索引為0的就在正中心 if (i == 2 && isAutoStop) { isStopUpdatePos = true; Debug.Log("展示獎勵界面..."); // todo...獲取獎勵數據維護 } return AniPosV3[index]; } else { return Vector3.Lerp(AniPosV3[index], AniPosV3[index + 1], progress[i] - index); } } /// <summary> /// 點擊抽獎 /// </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; } }
到此這篇關于Unity實現老虎機滾動抽獎效果的示例代碼的文章就介紹到這了,更多相關Unity老虎機滾動抽獎內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!