GameObject.FindGameObjectWithTag()根据标签查找游戏物体并返回。 GameObject.FindGameObjectsWithTag()根据标签查找当前场景中所有这个标签的游戏物体并返回所有物体的数组。 将如下场景:除主摄像机~(Main Camera)外的所有游戏物体的标签(Tag)~都修改为Player,进行测试。 测试代码如
比如我们要在上图的场景里查找名为C的GameObject,以下三种Find方法中的name实参都是可以的: GameObject c0 = GameObject.Find("A/B/C"); GameObject c1 = GameObject.Find("B/C"); GameObject c2 = GameObject.Find("C"); 只要name所描述的层级确实存在即可。 最后就是name中用于对层级进行描述的特殊符号’...
4.3 FindGameObjectWithTag(string tag) 通过标签查找对象 public class Test : MonoBehaviour { void Start() { GameObject obj = GameObject.FindGameObjectWithTag("Tag1"); //GameObject.FindGameObjectWithTag() 等价于 GameObject.FindWithTag() if (obj != null) { print(obj.name); } else { print("...
GameObject[] tagObjs = GameObject.FindGameObjectsWithTag("Player");for(inti =0; i < tagObjs.Length; i++) { Debug.Log("根据标签查找游戏物体名称:"+ tagObjs[i].name); } } 测试结果: 查找不存在的标签测试: GameObject tagObj = GameObject.FindGameObjectWithTag("MainCamera1"); Debug.Log("...
FindUnityGameObject["name"] finds the Unity game object with the specified name. FindUnityGameObject[patt] finds the Unity game object with the name that matches the string pattern patt. FindUnityGameObject[RegularExpression["regex"]] finds the Unity game object with the name that matches th...
除非迫不得已,建议不要在每一帧中使用这个函数。可以在开始的时候用一个成员变量来缓存结果或者使用GameObject.FindWithTag函数。 注意:这里的参数name是严格区分大小写的。 varhand : GameObject;//This will return the game object named Hand in the scene.//这将返回名为Hand 的游戏物体hand = GameObject.Fi...
GameObject.Find---直接查找名字,需要保证名字是唯一的,返回找到的这个GameObject GameObject.Find(Canvas/Text").GetComponent(UI.Text).text GameObject.FindGameObjectsWithTag---通过标签查找某个游戏物体,并以数组形式返回所有找到的结果 GameObject go=GameObject.FindGameObjectWithTag("Player"); GameObject...
在Unity场景中查找GameObject可以通过多种方式实现,以下是一些常见的方法,并附有代码片段进行佐证: 1. 通过名称查找GameObject 使用GameObject.Find(string name)方法可以根据物体的名称查找物体。需要注意的是,该方法只能查找当前激活(未隐藏)的物体,并且性能开销较大,因为它会遍历场景中的所有激活物体。 csharp GameObjec...
FindWithTag 返回一个用tag做标识的活动的游戏物体.如果没有找到则为空. FindGameObjectsWithTag 返回一个用tag做标识的活动的游戏物体的列表.如果没有找到则为空. Find 找到并返回一个名字为name的游戏物体. 继承成员 继承变量 name 物体的名字 hideFlags 物体是否被隐藏、保存在场景中或被用户修改?
GameObject.Find---直接查找名字,需要保证名字是唯一的,返回找到的这个GameObject GameObject.Find(Canvas/Text").GetComponent(UI.Text).text GameObject.FindGameObjectsWithTag---通过标签查找某个游戏物体,并以数组形式返回所有找到的结果 GameObject go=GameObject.FindGameObjectWithTag("Player"); GameObject...