Unity之跑馬燈抽獎效果單抽與連抽

2021-05-10 21:00:17

Unity之跑馬燈抽獎效果單抽與連抽

效果圖

  • 單次抽獎效果
    在這裡插入圖片描述

  • 跳過動畫抽獎效果
    在這裡插入圖片描述

  • 三連抽抽獎效果
    在這裡插入圖片描述

設計思路

  1. 點選按鈕 ,根據需求(概率)計算本次抽獎獲得物品
  2. 模擬轉動 (先加速後減速), 一段時間後停止
  3. 連抽的情況下等所有獎品動畫都表演完成才結束
  4. 跳過動畫設計,有跳過時抽獎速度直接到最大,並進入可中獎

場景搭建

在這裡插入圖片描述
一個按鈕,一個組獎品放到一個父物體上。
在這裡插入圖片描述
獎品元素,有兩種狀態,一種旋轉狀態,一種中獎狀態。

程式碼


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 跑馬燈轉盤
/// </summary>
public class RotaryTablePanel : MonoBehaviour
{
    //單次開始抽獎抽獎結束的事件
    private Action<bool> PlayingAction;
    //三連抽開始抽獎抽獎結束的事件
    private Action<bool> PlayingThreeAction;
    //是否是三連抽
    bool isThreeDraw;
    // 抽獎按鈕,
    public Button drawBtn;
    //跳過抽獎動畫
    public Toggle jumpTgl;
    // 抽獎圖片父物體
    public Transform rewardImgTran;
 
    //轉動特效
    public Transform eff_TurnFrame;
    //中獎特效
    public Transform eff_SelectFrame;
    // 抽獎圖片
    private Transform[] rewardTransArr;
    private RotaryCell[] rewardCellArr;

    // 預設展示狀態
    private bool isInitState;
    // 抽獎結束 -- 結束狀態,光環不轉
    private bool drawEnd;
    // 中獎
    private bool drawWinning;

    [Header("展示狀態時間 --> 控制光環轉動初始速度")]
    public float setrewardTime = 1f;

    private float rewardTime;
    private float rewardTiming = 0;

    // 當前光環所在獎勵的索引
    private int haloIndex = 0;
    // 本次中獎ID
    private int rewardIndex = 0;

    // 點了抽獎按鈕正在抽獎
    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>
    /// 註冊轉盤抽獎事件
    /// </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>();
        }

        // 預設展示時間
        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>
    /// 從中獎狀態恢復到預設狀態
    /// </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;
        }

        // 抽獎展示
        rewardTiming += Time.deltaTime;
        if (rewardTiming >= rewardTime)
        {

            rewardTiming = 0;

            haloIndex++;
            if (haloIndex >= rewardCellArr.Length)
            {
                haloIndex = 0;
            }
            if (isThreeDraw)
                SetHaloThreePos(haloIndex);
            else
                SetHaloPos(haloIndex);
        }
    }

    // 設定光環顯示位置
    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);

        // 中獎 && 此ID == 中獎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...展示中獎物品,維護資料 --> 注意: index是索引
            Debug.Log("恭喜您中獎,中獎物品索引是:" + index + "號");
        }
    }

    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);

        // 中獎 && 此ID == 中獎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...展示中獎物品,維護資料 --> 注意: index是索引
            Debug.Log("恭喜您三連抽中獎,中獎物品索引是:" + index + "號");
            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());
            }


        }
    }


    // 點選抽獎按鈕
    void OnClickDrawFun()
    {
        if (!IsOnClickPlaying)
        {
            haloIndex = -1;
            RePrepare();

            // 隨機抽中ID
            rewardIndex = UnityEngine.Random.Range(0, rewardCellArr.Length);
            Debug.Log("開始抽獎,本次抽獎隨機到的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());
        }
    }

    // 點選抽獎按鈕
    public void OnClickDrawFun(int index)
    {
        haloIndex = -1;
        isThreeDraw = false;
        rewardIndex = index - 1;//給lua提供方法,減1
        if (!IsOnClickPlaying)
        {
            RePrepare();
            Debug.Log("開始抽獎,本次抽獎到的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>
    /// 開始抽獎動畫
    /// 先快後慢 -- 根據需求調整時間
    /// </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#的關閉");
    }

}


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);
        }
    }
}

工程專案

連結:https://pan.baidu.com/s/1zGRGBXIfcTsRU8pS-_E6mQ
提取碼:0br5