找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
游戏黄埔已经开课啦,大家速速报名赶快上车
查看: 2261|回复: 1

《not bad lucky coffee》(《还不赖咖啡》)抽奖素材

[复制链接]

162

主题

33

回帖

891

积分

管理员

积分
891
发表于 2025-1-5 23:49:52 | 显示全部楼层 |阅读模式
难点:
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;

  5. public class GameManager : MonoBehaviour
  6. {
  7.     public GameObject[] item;
  8.     public GameObject maskRed;
  9.     public Button Go;
  10.     public int roundCount;//转的圈数(元素一共22个)
  11.     public int steps=-1;
  12.     public float onceTime=0.2f;
  13.     public int index=0;
  14.     public int maskRedIndex = -1;
  15.     public void StartGo()
  16.     {
  17.         RandomSteps();
  18.         StartCoroutine("RunMarquee");
  19.     }

  20.     //随机多少步
  21.     public void RandomSteps()
  22.     {
  23.         roundCount = Random.Range(1,6);
  24.         steps = Random.Range(0,21);
  25.         steps = steps + roundCount*item.Length;
  26.         print(steps);
  27.     }
  28.     IEnumerator RunMarquee()
  29.     {
  30.         for (int i = 0; i < steps; i++)
  31.         {
  32.             index = (index + 1) % item.Length; // 更新当前图片索引
  33.             maskRed.transform.position = item[index].transform.position; // 更新红纸位置
  34.             yield return new WaitForSeconds(onceTime); // 等待一段时间
  35.         }
  36.     }


  37. }
复制代码

代码分析
index: 当前图片的索引。item.Length: 图片对象数组的长度,即数组中图片的数量。%: 取模运算符,用于计算两个数相除后的余数。
详细解释
  • index + 1: 每次执行这行代码时,index 的值会增加 1。这意味着每次循环都会指向数组中的下一个元素。
  • % item.Length: 取模运算确保 index 的值始终在 0 到 item.Length - 1 之间。当 index 达到 item.Length 时,取模运算会将其重置为 0,从而实现循环效果。
示例
假设 item.Length 为 5,即数组中有 5 张图片。初始时 index 为 0。
  • 第一次循环:index = (0 + 1) % 5 = 1
  • 第二次循环:index = (1 + 1) % 5 = 2
  • 第三次循环:index = (2 + 1) % 5 = 3
  • 第四次循环:index = (3 + 1) % 5 = 4
  • 第五次循环:index = (4 + 1) % 5 = 0
通过这种方式,index 的值会在 0 到 4 之间循环,确保不会超出数组的边界。
结论
这行代码的作用是实现一个循环索引,使得 index 在数组 item 的范围内不断循环递增,从而实现走马灯效果。每次循环时,maskRed 的位置会更新为当前图片的位置,形成连续的动画效果。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

162

主题

33

回帖

891

积分

管理员

积分
891
 楼主| 发表于 2025-1-14 13:02:53 | 显示全部楼层
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;

  5. public class GameManager : MonoBehaviour
  6. {
  7.     public GameObject[] item;
  8.     public GameObject maskRed;
  9.     public Button Go;
  10.     public int roundCount;//转的圈数(元素一共22个)
  11.     public int steps=-1;
  12.     public float onceTime=0.2f;
  13.     public int index=0;
  14.     public int maskRedIndex = -1;
  15.     public void StartGo()
  16.     {
  17.         RandomSteps();
  18.         StartCoroutine("RunMarquee");
  19.     }

  20.     //随机多少步
  21.     public void RandomSteps()
  22.     {
  23.         roundCount = Random.Range(1,6);
  24.         steps = Random.Range(0,21);
  25.         steps = steps + roundCount*item.Length;
  26.         print(steps);
  27.     }
  28.     IEnumerator RunMarquee()
  29.     {
  30.         for (int i = 0; i < steps; i++)
  31.         {
  32.             index = (index + 1) % item.Length; // 更新当前图片索引
  33.             maskRed.transform.position = item[index].transform.position; // 更新红纸位置
  34.             yield return new WaitForSeconds(onceTime); // 等待一段时间
  35.         }
  36.     }


  37. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|平顶山市图灵科技 ( 豫ICP备2024088136号-1| 豫公网安备41040202000275号 )

GMT+8, 2025-5-23 03:48 , Processed in 0.047328 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表