public GameObject Pan1; public GameObject Imag1; public GameObject Pan2; public GameObject Imag2; public GameObject Pan3; public GameObject Imag3; public GameObject Pan4; public GameObject Imag4; public GameObject Pan5; public GameObject Imag5; public void bt1() { if (Pan1.activeSelf == false) { Pan1.SetActive(true); Imag1.SetActive(true); Pan2.SetActive(false); Imag2.SetActive(false); Pan3.SetActive(false); Imag3.SetActive(false); Pan4.SetActive(false); Imag4.SetActive(false); Pan5.SetActive(false); Imag5.SetActive(false); } else { Pan1.SetActive(false); Imag1.SetActive(false); } } public void bt2() { if (Pan2.activeSelf == false) { Pan2.SetActive(true); Imag2.SetActive(true); Pan1.SetActive(false); Imag1.SetActive(false); Pan3.SetActive(false); Imag3.SetActive(false); Pan4.SetActive(false); Imag4.SetActive(false); Pan5.SetActive(false); Imag5.SetActive(false); } else { Pan2.SetActive(false); Imag2.SetActive(false); } } public void bt3() { if (Pan3.activeSelf == false) { Pan3.SetActive(true); Imag3.SetActive(true); Pan1.SetActive(false); Imag1.SetActive(false); Pan2.SetActive(false); Imag2.SetActive(false); Pan4.SetActive(false); Imag4.SetActive(false); Pan5.SetActive(false); Imag5.SetActive(false); } else { Pan3.SetActive(false); Imag3.SetActive(false); } } unity 用更好的方法整理并减少这段代码
时间: 2024-02-14 20:10:41 浏览: 23
可以使用数组和循环来简化代码,例如:
```
public GameObject[] panels;
public GameObject[] images;
public void ShowPanel(int index)
{
for (int i = 0; i < panels.Length; i++)
{
panels[i].SetActive(i == index);
images[i].SetActive(i == index);
}
}
```
然后在按钮的点击事件中调用 `ShowPanel` 方法并传入对应的索引值,例如:
```
public void bt1()
{
ShowPanel(0);
}
public void bt2()
{
ShowPanel(1);
}
public void bt3()
{
ShowPanel(2);
}
```
这样就可以通过一个方法来控制所有的面板和图片的显示了,代码量也大大减少了。
相关问题
public GameObject monster1; // 怪物1的预制体
这是一段 C# 代码,声明了一个名为 "monster1" 的公共变量,类型为 GameObject。这个变量存储的是怪物1的预制体(Prefab)。
在 Unity 中,预制体是一种可重复使用的对象,可以在场景中多次实例化。通过在代码中声明一个名为 "monster1" 的变量,并将其类型设置为 GameObject,可以通过代码动态地实例化这个预制体,并在游戏中使用它。
例如,在代码中使用 "monster1" 变量实例化一个怪物,并将其放置在游戏场景中:
```
GameObject newMonster = Instantiate(monster1, transform.position, Quaternion.identity);
```
这个代码将实例化 "monster1" 预制体,并将其放置在当前对象的位置上。可以通过修改这个代码,调整怪物的位置、旋转角度等属性,来满足游戏的需求。
public GameObject Pan1; public GameObject Imag1; public GameObject Pan2; public GameObject Imag2; public GameObject Pan3; public GameObject Imag3; public GameObject Pan4; public GameObject Imag4; public GameObject Pan5; public GameObject Imag5; public void bt1() { if (Pan1.activeSelf == false) { Pan1.SetActive(true); Imag1.SetActive(true); Pan2.SetActive(false); Imag2.SetActive(false); Pan3.SetActive(false); Imag3.SetActive(false); Pan4.SetActive(false); Imag4.SetActive(false); Pan5.SetActive(false); Imag5.SetActive(false); } else { Pan1.SetActive(false); Imag1.SetActive(false); } } public void bt2() { if (Pan2.activeSelf == false) { Pan2.SetActive(true); Imag2.SetActive(true); Pan1.SetActive(false); Imag1.SetActive(false); Pan3.SetActive(false); Imag3.SetActive(false); Pan4.SetActive(false); Imag4.SetActive(false); Pan5.SetActive(false); Imag5.SetActive(false); } else { Pan2.SetActive(false); Imag2.SetActive(false); } } unity 这段代码功能不变 用更好的方法整理减少
可以使用数组和循环来简化代码,如下所示:
```
public GameObject[] panels;
public GameObject[] images;
public void ShowPanel(int index) {
for (int i = 0; i < panels.Length; i++) {
if (i == index) {
panels[i].SetActive(true);
images[i].SetActive(true);
} else {
panels[i].SetActive(false);
images[i].SetActive(false);
}
}
}
```
然后在按钮的 OnClick 事件里分别调用 ShowPanel 方法,传入对应的索引即可。这样可以减少重复的代码,也更加灵活。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)