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

  • 相关阅读:
    网工知识角|华为网络工程师,华为、华三、思科设备三层交换机如何使用三层接口?命令敲起来
    【字符串函数内功修炼】strcpy + strcat + strcmp(一)
    【PyQt】多窗口使用信号-槽传递数据
    【C语言】求一个整数的二进制序列中1的个数的三种方法
    二叉树正传 - 二叉树的基本操作及构建
    (41)Verilog实现递归功能
    我是真没想到,这个面试题居然从11年前就开始讨论了,而官方今年才表态。
    C/C++ 课程设计 | 银行管理系统
    rmq nameserver
    打造无证服务化:这个政务服务平台有点不一样
  • 原文地址:https://blog.csdn.net/weixin_45961836/article/details/139260104