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

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

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

PS:實際項目中可以根據(jù)需求來動態(tài)修改圖片顯示,以達到的控制每次抽獎獎品內(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;
// 進度
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};
// 自動暫停標識
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ù)維護
}
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;
}
}
到此這篇關(guān)于Unity實現(xiàn)老虎機滾動抽獎效果的示例代碼的文章就介紹到這了,更多相關(guān)Unity老虎機滾動抽獎內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# Socket 發(fā)送&接收&返回 簡單應(yīng)用實例
下面小編就為大家分享一篇C# Socket 發(fā)送&接收&返回 簡單應(yīng)用實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11
C# DataTable數(shù)據(jù)遍歷優(yōu)化詳解
這篇文章主要介紹了C# DataTable數(shù)據(jù)遍歷優(yōu)化詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

