标签 (Tag) 是可分配给一个或多个__游戏对象__的参考词。例如,可为玩家控制的角色定义“Player”标签,并为非玩家控制的角色定义“Enemy”标签。还可以使用“Collectable”标签定义玩家可在场景中收集的物品。
标签有助于识别游戏对象以便于编写脚本。通过使用标签,不需要使用拖放方式手动将游戏对象添加到脚本的公开属性,因此可以节省在多个游戏对象中使用相同脚本代码的时间。
标签对碰撞体控制脚本中的触发器很有用;例如,需要通过标签确定玩家是否与敌人、道具或可收集物进行交互。
通过设置 GameObject.FindWithTag() 函数,可以查找包含所需标签的游戏对象。以下示例便使用了 GameObject.FindWithTag()。该函数在具有“Respawn”标签的游戏对象位置实例化 respawnPrefab
:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public GameObject respawnPrefab;
public GameObject respawn;
void Start() {
if (respawn == null)
respawn = GameObject.FindWithTag("Respawn");
Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation) as GameObject;
}
}
在 Inspector 中,游戏对象的名称下面会显示 Tag 和 Layer 下拉菜单。
要创建新标签,请选择 Add Tag…。随即在 Inspector 中打开标签和层管理器 (Tag and Layer Manager)。请注意,一旦命名了标签,以后就无法重命名。
层类似于标签,但用于定义 Unity 应该如何在场景中渲染游戏对象。请参阅有关层的文档以了解更多信息。
在 Inspector 中,游戏对象的名称下面会显示 Tag 和 Layer 下拉菜单。要将现有标签应用于游戏对象,请打开 Tags 下拉选单,然后选择要应用的标签。游戏对象现在便与此标签关联。
只能为游戏对象分配一个标签。
Unity 包含一些未出现在标签管理器中的内置标签:
可以使用任何喜欢的词作为标签。甚至可以使用短语,但这种情况下可能需要增加 Inspector 的宽度才能看到标签的全名。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.