GameObject.FindGameObjectsWithTag
时间: 2023-11-24 16:06:05 浏览: 33
GameObject.FindGameObjectsWithTag 是一个在 Unity 游戏引擎中使用的方法,用于根据标签(Tag)查找场景中符合标签条件的所有 GameObject。
使用 GameObject.FindGameObjectsWithTag("标签名") 可以返回一个 GameObject 数组,包含了所有具有指定标签的游戏对象。标签是开发者在 Unity 编辑器中为 GameObject 分配的一个字符串标识,用于对游戏对象进行分类和组织。
这个方法可以帮助开发者快速找到场景中特定标签的游戏对象,方便进行后续的操作和逻辑处理。例如,可以使用它来查找所有敌人、道具或特定类型的物体,并对它们进行处理、修改属性或执行特定的行为。
相关问题
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 整理代码 功能不变 缩短
以下是优化后的代码:
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>());
}
}
这样,代码更加简洁易读,而且功能不变。
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; } } public void Scalse() { GameObject[] enemies = GameObject.FindGameObjectsWithTag("MOVEWIN"); foreach (GameObject Scale in enemies) { if (Scale.GetComponent<Scale>() == null) { img3.SetActive(true); Scale.AddComponent<Scale>(); img1.SetActive(false); MOve.GetComponent<MOVEWIN>().enabled = false; img2.SetActive(false); Rotate.GetComponent<Rotet>().enabled = false; unity 整理代码 功能不变 缩短
public GameObject img1, img2, img3, MOve, Rotate, 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);
DestroyExistingComponentsWithTag("MOVEWIN", typeof(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);
DestroyExistingComponentsWithTag("MOVEWIN", typeof(Scale));
}
else
{
img2.SetActive(false);
Rotate.GetComponent<Rotet>().enabled = false;
}
}
public void Scalse()
{
DestroyExistingComponentsWithTag("MOVEWIN", typeof(Scale));
if (Scale == null)
{
img3.SetActive(true);
Scale.AddComponent<Scale>();
img1.SetActive(false);
MOve.GetComponent<MOVEWIN>().enabled = false;
img2.SetActive(false);
Rotate.GetComponent<Rotet>().enabled = false;
}
}
void DestroyExistingComponentsWithTag(string tag, System.Type type)
{
GameObject[] existingObjects = GameObject.FindGameObjectsWithTag(tag);
foreach (GameObject obj in existingObjects)
{
Component component = obj.GetComponent(type);
if (component != null)
{
Destroy(component);
}
}
}