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

《生化危机2RE》便携保险箱课程案例素材

[复制链接]

162

主题

33

回帖

891

积分

管理员

积分
891
发表于 2024-12-21 22:12:57 | 显示全部楼层 |阅读模式
素材在附件压缩包内内

本帖子中包含更多资源

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

×
回复

使用道具 举报

162

主题

33

回帖

891

积分

管理员

积分
891
 楼主| 发表于 2024-12-23 22:40:09 | 显示全部楼层
要将一个长度为8的C#数组中下标为3及以后的元素依次放在新数组的前部,然后将原来下标为3之前的元素放在新数组的后部,可以按照以下步骤进行操作:
  1. using System;

  2. class Program
  3. {
  4.     static void Main()
  5.     {
  6.         // 原始数组
  7.         int[] originalArray = { 0, 1, 2, 3, 4, 5, 6, 7 };
  8.         
  9.         // 新数组
  10.         int[] newArray = new int[originalArray.Length];
  11.         
  12.         // 将原数组中下标为3及以后的元素复制到新数组的前部
  13.         for (int i = 3; i < originalArray.Length; i++)
  14.         {
  15.             newArray[i - 3] = originalArray[i];
  16.         }
  17.         
  18.         // 将原数组中下标为0到2的元素复制到新数组的后部
  19.         for (int i = 0; i < 3; i++)
  20.         {
  21.             newArray[originalArray.Length - 3 + i] = originalArray[i];
  22.         }
  23.         
  24.         // 输出新数组
  25.         Console.WriteLine("新数组: " + string.Join(", ", newArray));
  26.     }
  27. }
复制代码
回复

使用道具 举报

162

主题

33

回帖

891

积分

管理员

积分
891
 楼主| 发表于 2024-12-24 21:23:37 | 显示全部楼层
为了在Unity中设计一个游戏,实现8个按钮对应8个灯,按某个按钮某个灯亮,并且灯需要按逆时针方向被点亮,如果按某个按钮不是逆时针连续则8个灯全灭,我们可以按照以下步骤进行设计:

1. 创建游戏对象
创建8个灯对象(可以是简单的立方体或自定义的灯模型)。
创建8个按钮对象(可以使用Unity的UI按钮或自定义的按钮模型)。
2. 设置灯光和按钮
为每个灯对象添加一个Light组件或自定义的发光效果。
为每个按钮对象添加一个Button组件,并设置其点击事件。
3. 编写脚本
编写一个C#脚本来控制灯的亮灭逻辑。以下是一个示例脚本:
  1. using UnityEngine;
  2. using UnityEngine.UI;

  3. public class LightController : MonoBehaviour
  4. {
  5.     public Light[] lights; // 灯对象数组
  6.     public Button[] buttons; // 按钮对象数组

  7.     private int currentLightIndex = -1; // 当前点亮的灯的索引

  8.     void Start()
  9.     {
  10.         // 初始化所有灯为熄灭状态
  11.         foreach (Light light in lights)
  12.         {
  13.             light.enabled = false;
  14.         }

  15.         // 为每个按钮添加点击事件
  16.         for (int i = 0; i < buttons.Length; i++)
  17.         {
  18.             int index = i;
  19.             buttons[i].onClick.AddListener(() => OnButtonClick(index));
  20.         }
  21.     }

  22.     void OnButtonClick(int buttonIndex)
  23.     {
  24.         // 如果是第一个按钮按下,直接点亮
  25.         if (currentLightIndex == -1)
  26.         {
  27.             lights[buttonIndex].enabled = true;
  28.             currentLightIndex = buttonIndex;
  29.             return;
  30.         }

  31.         // 计算逆时针下一个灯的索引
  32.         int nextIndex = (currentLightIndex - 1 + lights.Length) % lights.Length;

  33.         // 如果按下的是逆时针下一个灯,则点亮该灯
  34.         if (buttonIndex == nextIndex)
  35.         {
  36.             lights[currentLightIndex].enabled = false;
  37.             lights[buttonIndex].enabled = true;
  38.             currentLightIndex = buttonIndex;
  39.         }
  40.         else
  41.         {
  42.             // 否则,所有灯熄灭
  43.             foreach (Light light in lights)
  44.             {
  45.                 light.enabled = false;
  46.             }
  47.             currentLightIndex = -1;
  48.         }
  49.     }
  50. }
复制代码

4. 将脚本附加到游戏对象
创建一个空的游戏对象,命名为LightController。
将上述脚本附加到LightController对象上。
在Inspector面板中,将8个灯对象拖动到Lights数组中。
将8个按钮对象拖动到Buttons数组中。
5. 测试游戏
进入Play模式,测试按钮是否能按逆时针顺序点亮灯,如果不是逆时针顺序则所有灯熄灭。
回复

使用道具 举报

162

主题

33

回帖

891

积分

管理员

积分
891
 楼主| 发表于 2024-12-29 21:02:28 | 显示全部楼层
代码最终为:
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;

  5. public class GameManager : MonoBehaviour
  6. {
  7.     public Image[] lights; // 灯对象数组
  8.     public Button[] buttons; // 按钮对象数组
  9.     public Sprite green;//绿灯
  10.     public Sprite black;//黑灯
  11.     public Image bigLight;
  12.     public int greenLightingCount;

  13.     private int currentLightIndex = -1; // 当前点亮的灯的索引

  14.     void Start()
  15.     {
  16.         // 初始化所有灯为熄灭状态
  17.         foreach (Image light in lights)
  18.         {
  19.             Debug.Log(light.GetComponent<Image>());
  20.             light.GetComponent<Image>().sprite = black;
  21.         }
  22.         foreach (Button btn in buttons)
  23.         {
  24.             Debug.Log(btn.GetComponent<Image>());
  25.             btn.GetComponent<Image>().sprite = black;
  26.         }

  27.         // 为每个按钮添加点击事件
  28.         for (int i = 0; i < buttons.Length; i++)
  29.         {
  30.             int index = i;
  31.             buttons[i].onClick.AddListener(() => OnButtonClick(index));
  32.         }
  33.     }

  34.     void OnButtonClick(int buttonIndex)
  35.     {
  36.         // 如果是第一个按钮按下,直接点亮
  37.         if (currentLightIndex == -1)
  38.         {
  39.             lights[buttonIndex].GetComponent<Image>().sprite = green;
  40.             buttons[buttonIndex].GetComponent<Image>().sprite = green;
  41.             currentLightIndex = buttonIndex;
  42.             return;
  43.         }
  44.         else
  45.         {
  46.             lights[buttonIndex].GetComponent<Image>().sprite = green;
  47.             buttons[buttonIndex].GetComponent<Image>().sprite = green;
  48.             StartCoroutine(DelayCalculate(buttonIndex));
  49.         }
  50.         
  51.         
  52.         //计算逆时针下一个灯的索引和逆时针下一个灯就点亮该灯,如果不是则所有灯熄灭
  53.         
  54.     }
  55.     IEnumerator DelayCalculate(int buttonIndex)
  56.     {
  57.         yield return new WaitForSeconds(0.5f);

  58.         // 计算逆时针下一个灯的索引
  59.         int nextIndex = (currentLightIndex + 1 + lights.Length) % lights.Length;

  60.         // 如果按下的是逆时针下一个灯,则点亮该灯
  61.         if (buttonIndex == nextIndex)
  62.         {
  63.             greenLightingCount++;
  64.             lights[buttonIndex].GetComponent<Image>().sprite = green;
  65.             currentLightIndex = buttonIndex;
  66.             if (greenLightingCount==lights.Length-1)
  67.             {
  68.                 bigLight.sprite = green;
  69.                 Debug.Log("胜利");
  70.             }
  71.         }
  72.         else
  73.         {
  74.             // 否则,所有灯熄灭
  75.             foreach (Image light in lights)
  76.             {
  77.                 light.GetComponent<Image>().sprite = black;
  78.             }
  79.             foreach (Button btn in buttons)
  80.             {
  81.                 btn.GetComponent<Image>().sprite = black;
  82.             }
  83.             currentLightIndex = -1;
  84.             greenLightingCount = 0;
  85.         }
  86.     }
  87.    
  88.    
  89. }
复制代码
回复

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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