100个 Unity小游戏系列三 -Unity 抽奖游戏专题三老虎机游戏

2024-05-28 12:52

本文主要是介绍100个 Unity小游戏系列三 -Unity 抽奖游戏专题三老虎机游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、演示效果

二、知识点讲解

2.1 布局

public void CreateItems(SlotsData[] slotsData){isInited = false;slotsPrizeList = new List<SlotsData>();for (int i = 0; i < slotsData.Length; i++){var item = slotsData[i];slotsPrizeList.Add(item);}float bottomY = -itemPadding * frontItemCount;slotEdge = new Vector2(bottomY + itemPadding * slotsPrizeList.Count, bottomY);slotWheelsIds = new int[slotWheels.Length][];slotsStopIdx = new int[slotWheels.Length];for (int i = 0; i < slotWheelsIds.Length; i++){slotWheelsIds[i] = new int[slotsPrizeList.Count];for (int j = 0; j < slotWheelsIds[i].Length; j++){slotWheelsIds[i][j] = slotsPrizeList[j].id;}int halfLen = slotsPrizeList.Count / 2;for (int j = 0; j < halfLen; j++){int randSwapIdx = UnityEngine.Random.Range(0, halfLen + 1);int swapIdx2 = slotsPrizeList.Count - randSwapIdx - 1;int tmpPrizeId = slotWheelsIds[i][swapIdx2];slotWheelsIds[i][swapIdx2] = slotWheelsIds[i][randSwapIdx];slotWheelsIds[i][randSwapIdx] = tmpPrizeId;}}for (int i = 0; i < slotWheels.Length; i++){var wheel = slotWheels[i];for (int j = 0; j < slotWheelsIds[i].Length; j++){var prizeId = slotWheelsIds[i][j];var reward_data = LuckyManager.Instance.GetDataById(rewardDatas, prizeId, out int index);var item = Instantiate(slot_Item, wheel);var pos = item.transform.localPosition;pos.y = slotEdge.y + j * itemPadding;item.transform.localPosition = pos;item.transform.localScale = Vector3.one * itemSize;var reward_img = item.GetComponentInChildren<Image>();reward_img.sprite = itemImgs[reward_data.type - 1];reward_img.SetNativeSize();item.GetComponentInChildren<TextMeshProUGUI>().text = string.Format("{0}", reward_data.amount);item.SetActive(true);}}isInited = true;}

2.2、转动逻辑

 private void OnClickSpin(){if (!isInited || IsRolling){return;}GetSelectIndex();RollSlots(SpinComplete);}public void RollSlots(Action<bool, SlotsData> onSpinCompleted){IsRolling = true;int rewardId = LuckyManager.Instance.CalculateRewardId(rewardDatas);var reward_data = LuckyManager.Instance.GetDataById(rewardDatas, rewardId, out int slot_index);bool isMatched = CalculateSlotStopIndex(reward_data, out slotsStopIdx);int slotWheelsCount = slotWheels.Length;int roundNum = 5;for (int i = 0; i < slotWheels.Length; i++){var wheel = slotWheels[i];var stopIdx = slotsStopIdx[i];float rollDistance = roundNum * (slotEdge.x - slotEdge.y);var offsetY = wheel.GetChild(stopIdx).localPosition.y;rollDistance += offsetY < 0 ? Mathf.Abs(offsetY - slotEdge.y) + slotEdge.x : offsetY;float preframePosY = 0f;float curPosY = 0f;float rollTime = 2.5f;if (rollStyle2){rollTime = 2.5f + i * 0.5f;}else if (rollStyle3){rollTime = 4.5f - i * 0.5f;}else if (rollStyle4){rollTime = 2.5f + i * 2.5f;}var rollAnim = DOTween.To(() => curPosY, (x) => curPosY = x, rollDistance, rollTime);rollAnim.SetEase(Ease.OutQuart);rollAnim.onUpdate = () =>{float deltaY = curPosY - preframePosY;for (int j = 0; j < wheel.childCount; j++){var item = wheel.GetChild(j);float nextPosY = item.transform.localPosition.y - deltaY;var localPosition = item.transform.localPosition;localPosition.y = nextPosY < slotEdge.y ? slotEdge.x - (slotEdge.y - nextPosY) : nextPosY;item.transform.localPosition = localPosition;}preframePosY = curPosY;};rollAnim.onComplete = () =>{if (--slotWheelsCount <= 0){IsRolling = false;onSpinCompleted.Invoke(isMatched, reward_data);}};}}

三、代码完整链接

https://github.com/lixianjun0903/luckydraw-master.git

这篇关于100个 Unity小游戏系列三 -Unity 抽奖游戏专题三老虎机游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1010626

相关文章

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

Python38个游戏开发库整理汇总

《Python38个游戏开发库整理汇总》文章介绍了多种Python游戏开发库,涵盖2D/3D游戏开发、多人游戏框架及视觉小说引擎,适合不同需求的开发者入门,强调跨平台支持与易用性,并鼓励读者交流反馈以... 目录PyGameCocos2dPySoyPyOgrepygletPanda3DBlenderFife

游戏闪退弹窗提示找不到storm.dll文件怎么办? Stormdll文件损坏修复技巧

《游戏闪退弹窗提示找不到storm.dll文件怎么办?Stormdll文件损坏修复技巧》DLL文件丢失或损坏会导致软件无法正常运行,例如我们在电脑上运行软件或游戏时会得到以下提示:storm.dll... 很多玩家在打开游戏时,突然弹出“找不到storm.dll文件”的提示框,随后游戏直接闪退,这通常是由于

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python开发围棋游戏的实例代码(实现全部功能)

《Python开发围棋游戏的实例代码(实现全部功能)》围棋是一种古老而复杂的策略棋类游戏,起源于中国,已有超过2500年的历史,本文介绍了如何用Python开发一个简单的围棋游戏,实例代码涵盖了游戏的... 目录1. 围棋游戏概述1.1 游戏规则1.2 游戏设计思路2. 环境准备3. 创建棋盘3.1 棋盘类

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

【专题】2024飞行汽车技术全景报告合集PDF分享(附原数据表)

原文链接: https://tecdat.cn/?p=37628 6月16日,小鹏汇天旅航者X2在北京大兴国际机场临空经济区完成首飞,这也是小鹏汇天的产品在京津冀地区进行的首次飞行。小鹏汇天方面还表示,公司准备量产,并计划今年四季度开启预售小鹏汇天分体式飞行汽车,探索分体式飞行汽车城际通勤。阅读原文,获取专题报告合集全文,解锁文末271份飞行汽车相关行业研究报告。 据悉,业内人士对飞行汽车行业