• 【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;
        }
    
        /// 
        /// 开始转动
        /// 
        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;
                    }
    
                }
            }
    
    
    
        }
    
        /// 
        /// 开始转动
        /// 
        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实现轮盘抽奖

  • 相关阅读:
    si446使用记录(一):基本资料获取
    K8S 网络问题导致 ns 相关的服务不能互相访问
    高级语言讲义2018计专(仅高级语言部分)
    【教程 | IWR1642串口数据采集的烧写以及Bug复现】
    UEC++ day7
    391. 完美矩形 扫描线
    Django 静态自定义化配置
    win10下pycocotools安装及问题解决
    Leecode刷题 876. 链表的中间结点——链表
    Spring Boot 工程开发常见问题解决方案,日常开发全覆盖
  • 原文地址:https://blog.csdn.net/weixin_45961836/article/details/139260104