【Unity】实现轮盘抽奖

一、简介

转盘抽奖模块在游戏日常开发中经常用到,这里给大家分享一些实现方法,提供给大家学习,有兴趣的小伙伴可以关注、评论,一起交流学习。

二、实现示例

1、示例一(使用协程完成轮盘转动)

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

public class Lunpan : MonoBehaviour
{
    [Tooltip("轮盘节点")]
    public Transform Roulette;

    [Tooltip("轮盘旋转的圈数")]
    public int RotateNumber = 4;

    //[Tooltip("轮盘需要旋转的度数")]
    private float degrees = 0;

    [Tooltip("轮盘旋转初速度")]
    public int RotateSpeed = 500;

    //[Tooltip("当前轮盘旋转速度")]
    private float curRotateSpeed = 0;

    [Tooltip("减速度")]
    public float deceleration = 3;

    [Tooltip("最低速度")]
    public float minSpeed = 50;

    [Tooltip("轮盘剩余旋转多少时开始减速")]
    public float surRotate = 720;

    [Tooltip("轮盘中物体的数量")]
    public int All = 6;

    [Tooltip("旋转停止的位置")]
    public int selectedIndex = 1;

    [Tooltip("精准角度")]
    public Boolean enblePrecise = false;

    private void Start()
    {
    	//每秒的帧率改为60(不是必要)
        Application.targetFrameRate = 60;
    }

    /// <summary>
    /// 开始转动
    /// </summary>
    public void StartRoulette()
    {
        this.curRotateSpeed = this.RotateSpeed;
        StartCoroutine(RotateRoulette());
    }


    IEnumerator RotateRoulette()
    {
        //存储每帧需要旋转的角度
        float curRotate = 0;
        //计算轮盘总需要旋转的角度
        this.degrees = this.RotateNumber * 360 - ((this.selectedIndex - 1) * 360) / this.All - (360 - this.Roulette.eulerAngles.z);

        while (this.degrees > 0)
        {
            //计算此帧需要旋转的角度
            curRotate = Time.deltaTime * this.curRotateSpeed;

            //精准到指定角度
            if (this.enblePrecise && curRotate > this.degrees)
            {
                if (this.curRotateSpeed >= this.minSpeed)
                {
                    //速度减半
                    this.curRotateSpeed = this.minSpeed / 2;
                    curRotate = Time.deltaTime * this.curRotateSpeed;
                    if (curRotate > this.degrees)
                    {
                        curRotate = this.degrees;
                    }
                }
                else
                {
                    curRotate = this.degrees;
                }

            }

            //减速
            if (this.curRotateSpeed > this.minSpeed && this.degrees < this.surRotate)
            {
                this.curRotateSpeed -= deceleration;
            }

            //开始旋转
            this.Roulette.Rotate(Vector3.back * curRotate);
            this.degrees -= curRotate;
            yield return null;
        }

        yield return null;
    }


}


2、示例二(不使用协程实现轮盘转动)

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class LunpanController : MonoBehaviour
{
    [Tooltip("轮盘节点")]
    public Transform Roulette;

    [Tooltip("轮盘旋转的圈数")]
    public int RotateNumber = 4;

    //[Tooltip("轮盘需要旋转的度数")]
    private float degrees = 0;

    [Tooltip("轮盘旋转初速度")]
    public int RotateSpeed = 500;

    //[Tooltip("当前轮盘旋转速度")]
    private float curRotateSpeed = 0;

    [Tooltip("减速度")]
    public float deceleration = 3;

    [Tooltip("最低速度")]
    public float minSpeed = 50;

    [Tooltip("轮盘剩余旋转多少时开始减速")]
    public float surRotate = 720;

    [Tooltip("轮盘中物体的数量")]
    public int All = 6;

    [Tooltip("旋转停止的位置")]
    public int selectedIndex = 1;

    [Tooltip("精准角度")]
    public Boolean enblePrecise = false;

    //开启轮盘转动
    private Boolean enbleLunpan = false;

    private void Start()
    {
        //每秒的帧率改为60(不是必要)
        Application.targetFrameRate = 60;
    }

    private void Update()
    {

        if (this.enbleLunpan)
        {
            //存储每帧需要旋转的角度
            float curRotate = 0;

            if (this.degrees > 0)
            {
                //计算此帧需要旋转的角度
                curRotate = Time.deltaTime * this.curRotateSpeed;

                //精准到指定角度
                if (this.enblePrecise && curRotate > this.degrees)
                {
                    if (this.curRotateSpeed >= this.minSpeed)
                    {
                        //速度减半
                        this.curRotateSpeed = this.minSpeed / 2;
                        curRotate = Time.deltaTime * this.curRotateSpeed;
                        if (curRotate > this.degrees)
                        {
                            curRotate = this.degrees;
                        }
                    }
                    else
                    {
                        curRotate = this.degrees;
                    }

                }

                //减速
                if (this.curRotateSpeed > this.minSpeed && this.degrees < this.surRotate)
                {
                    this.curRotateSpeed -= deceleration;
                }

                //开始旋转
                this.Roulette.Rotate(Vector3.back * curRotate);
                this.degrees -= curRotate;

                if(this.degrees <= 0)
                {
                    this.enbleLunpan = false;
                }

            }
        }



    }

    /// <summary>
    /// 开始转动
    /// </summary>
    public void StartRoulette()
    {
        this.curRotateSpeed = this.RotateSpeed;

        //计算轮盘总需要旋转的角度
        this.degrees = this.RotateNumber * 360 - ((this.selectedIndex - 1) * 360) / this.All - (360 - this.Roulette.eulerAngles.z);
        this.enbleLunpan = true;
    }



}


演示效果:

Unity实现轮盘抽奖

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KeepJian

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值