欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity實(shí)現(xiàn)老虎機(jī)滾動抽獎效果的示例代碼

 更新時間:2021年04月26日 09:55:22   作者:佛系老陳  
這篇文章主要介紹了Unity實(shí)現(xiàn)老虎機(jī)滾動抽獎效果的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

直接看下效果圖吧:

choujiang

制作思路:

設(shè)計(jì)四張圖片,五個點(diǎn),每個圖片同時向下一個點(diǎn)移動,到最后一個就回到0號點(diǎn),以此循環(huán)。

1.2

場景搭建:

  • 創(chuàng)建Image命名為Bg作為電視框背景;
  • 創(chuàng)建Image命名Mask并添加Mask組件作為電視框內(nèi)容顯示遮罩框;
  • 創(chuàng)建四個Image作為滾動圖片;
  • 創(chuàng)建開始抽獎按鈕;

1.3

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)文章

最新評論