public GameObject img1; public GameObject img2; public GameObject img3; public GameObject MOve; public GameObject Rotate; public GameObject Scale; public void Move() { if(MOve.GetComponent<MOVEWIN>().enabled == false) { img1.SetActive(true); MOve.GetComponent<MOVEWIN>().enabled = true; img2.SetActive(false); Rotate.GetComponent<Rotet>().enabled = false; img3.SetActive(false); GameObject[] enemies = GameObject.FindGameObjectsWithTag("MOVEWIN"); foreach (GameObject Scale in enemies) { Destroy(Scale.GetComponent<Scale>()); } } else { img1.SetActive(false); MOve.GetComponent<MOVEWIN>().enabled = false; } } public void Rota() { if (Rotate.GetComponent<Rotet>().enabled == false) { img2.SetActive(true); Rotate.GetComponent<Rotet>().enabled = true; img1.SetActive(false); MOve.GetComponent<MOVEWIN>().enabled = false; img3.SetActive(false); GameObject[] enemies = GameObject.FindGameObjectsWithTag("MOVEWIN"); foreach (GameObject Scale in enemies) { Destroy(Scale.GetComponent<Scale>()); } } else { img2.SetActive(false); Rotate.GetComponent<Rotet>().enabled = false; } unity 整理代码 功能不变 缩短
时间: 2024-02-10 16:20:55 浏览: 70
Unity3D如何获得两GameObject间距1
以下是优化后的代码:
public GameObject img1;
public GameObject img2;
public GameObject img3;
public GameObject MOve;
public GameObject Rotate;
public GameObject Scale;
public void Move()
{
bool moveEnabled = MOve.GetComponent<MOVEWIN>().enabled;
img1.SetActive(!moveEnabled);
MOve.GetComponent<MOVEWIN>().enabled = !moveEnabled;
img2.SetActive(false);
Rotate.GetComponent<Rotet>().enabled = false;
img3.SetActive(false);
DestroyAllObjectsWithTag("MOVEWIN");
}
public void Rota()
{
bool rotateEnabled = Rotate.GetComponent<Rotet>().enabled;
img2.SetActive(!rotateEnabled);
Rotate.GetComponent<Rotet>().enabled = !rotateEnabled;
img1.SetActive(false);
MOve.GetComponent<MOVEWIN>().enabled = false;
img3.SetActive(false);
DestroyAllObjectsWithTag("MOVEWIN");
}
private void DestroyAllObjectsWithTag(string tag)
{
GameObject[] objects = GameObject.FindGameObjectsWithTag(tag);
foreach (GameObject obj in objects)
{
Destroy(obj.GetComponent<Scale>());
}
}
这样,代码更加简洁易读,而且功能不变。
阅读全文