Unity3D圣典3.3版本-中文版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
GameObject.FindGameObjectsWithTag 查找标签的游戏物体列表
# [GameObject](GameObject.html).FindGameObjectsWithTag 查找标签的游戏物体列表 static function *FindGameObjectsWithTag* (*tag* : string) : GameObject[] *Description* 描述 Returns a list of active GameObjects tagged tag. Returns null if no GameObject was found. Tags must be declared in the tag manager before using them. 返回一个用 tag 做标识的活动的游戏物体的列表.如果没有找到则为空。 标签必须在使用之前到标签管理器里面声明。 * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { public GameObject respawnPrefab; public GameObject[] respawns = GameObject.FindGameObjectsWithTag("Respawn"); public void Awake() { foreach (GameObject respawn in respawns) { Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation); } } } ``` ``` // Instantiates respawnPrefab at the location // of all game objects tagged "Respawn". //在标签为"Respawn"的全部游戏物体自身坐标实例化respawnPrefab var respawnPrefab : GameObject; var respawns = GameObject.FindGameObjectsWithTag ("Respawn"); for (var respawn in respawns) Instantiate (respawnPrefab, respawn.transform.position, respawn.transform.rotation); ``` 另一个例子: * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { GameObject FindClosestEnemy() { GameObject[] gos; gos = GameObject.FindGameObjectsWithTag("Enemy"); GameObject closest; float distance = Mathf.Infinity; Vector3 position = transform.position; foreach (GameObject go in gos) { Vector3 diff = go.transform.position - position; float curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest; } public void Awake() { print(FindClosestEnemy().name); } } ``` ``` // Print the name of the closest enemy //打印最靠近敌人的名字 print(FindClosestEnemy().name); // Find the name of the closest enemy //查找最靠近敌人的名字 function FindClosestEnemy () : GameObject { // Find all game objects with tag Enemy //查找标签为Enemy的全部游戏物体 var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Enemy"); var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one //遍历他们找到最接近的一个 for (var go : GameObject in gos) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } } return closest; } ```
da
2022年5月20日 22:55
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码