• 【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实现轮盘抽奖

  • 相关阅读:
    为何学linux及用处
    关于 Bash 脚本中 Shebang 的趣事
    C/C++家族族谱管理系统
    Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
    Python的打包神器—Nuitka
    汇编语言(第三版)第二章 寄存器 笔记
    【数据结构与算法分析】0基础带你学数据结构与算法分析07--二叉树
    【Web世界探险家】打开Web世界的大门
    document对象概念介绍、验证用户名是否有效、正则表达式对象、两种常见的验证提示效果
    智慧法院新征程,无纸化办公,护航智慧法院绿色庭审
  • 原文地址:https://blog.csdn.net/weixin_45961836/article/details/139260104