Tags
标签
A Tag is a word which you link to one or more GameObjects. For instance, you might define “Player” and “Enemy” Tags for player-controlled characters and non-player characters respectively; a “Collectable” Tag could be defined for items the player can collect in the Scene; and so on. Clearly, Tags are intended to identify GameObjects for scripting purposes. We can use them to write script code to find a GameObject by looking for any object that contains our desired Tag. This is achieved using theGameObject.FindWithTag() function.
标签是一个链接到一个或多个对象上的单词.例如,你可以定义”玩家”和”敌人”标签用于表示玩家控制角色和非玩家角色,”可收集的”标签可定义场景内玩家可以收集的元素等等.很明显,标签的作用是用于脚本鉴别对象.我们可以用它们写脚本代码通过查看任何包含我们想要的标签的对象来寻找游戏对象.这可通过使用GameObject.FindWithTag() 函数实现.
For example:例如
// Instantiates respawnPrefab at the location
//在位置实例重生预设物
// of the game object with tag "Respawn"
//通过游戏对象的”重生”标签
//JS
var respawnPrefab : GameObject;
var respawn = GameObject.FindWithTag ("Respawn");
Instantiate (respawnPrefab, respawn.position, respawn.rotation);
//C#
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public GameObject respawnPrefab;
public GameObject respawn;
void Start() {
//如果respawn为空就在场景内寻找有”Respawn”标签的对象赋值给它
if (respawn == null)
respawn = GameObject.FindWithTag("Respawn");
//按respawn的transform实例化respawnPrefab
Instantiate(respawnPrefab, respawn.transform.position, respawn.transform.rotation) as GameObject;
}
}
This saves us having to manually add our GameObjects to a script’s exposed properties using drag and drop – a useful timesaver if the same script code is being used in a number of GameObjects. Another example is a TriggerCollider control script which needs to work out whether the player is interacting with an enemy, as opposed to, say, a random prop or collectable item. Tags make this kind of test easy.
这免去了我们手动去通过去拖放脚本的暴露属性来添加游戏对象,如果同一个脚本添加到多个对象上对节约时间很有用.另一个例子是碰撞检测器(TriggerCollider)控制脚本,用于解决玩家和敌人或者说一个随机道具或收集元素间的相互作用.标签使这种测试变得简单.
Applying a Tag
申请一个标签
The Inspector will show the Tag and Layer->Layers drop-down menus just below any GameObject’s name. To apply a Tag to a GameObject, simply open the Tags drop-down and choose the Tag you require:
检视面板上紧跟着对象名称下面在会显示标签和层级>层级下拉菜单.简单的打开标签下拉菜单选定一个你需要的标签来给对象申请一个标签:
The GameObject will now be associated with this Tag.
现在对象将和标签联系在一起.
Creating new Tags
创建新标签
To create a new Tag, click the “Add Tag…” option at the end of the drop-down menu. This will open up the Tag Manager in the Inspector. The Tag Manager is described here.
在下拉菜单的末尾点击”添加标签...”选项来创建一个新标签.这将在检视面板打开标签管理器.标签管理器在”标签和层级”页面有详细介绍.
Layers appear similar to Tags, but are used to define how Unity should render GameObjects in the Scene. See the Layers page for more information.
层级和标签一样显示,但被用于定义Unity在场景中该如何渲染对象.更多信息查看”层级”页面.
Hints
提示
1. A GameObject can only have one Tag assigned to it.
2. Unity includes some built-in Tags which do not appear in the Tag Manager:
a) “Untagged”
b) “Respawn”
c) “Finish”
d) “EditorOnly”
e) “MainCamera”
f) “Player”
g) and “GameController”.
3. You can use any word you like as a Tag. (You can even use short phrases, but you may need to widen the Inspector to see the tag’s full name.)
1.一个对象只能指定一个标签
2.Unity包含一些内建标签,它们没出现在标签管理器
a) 未加标签
b) 复位
c) 结束
d) 仅用于编辑器
e) 主摄像机
f) 玩家
g) 游戏控制器
3.你可以使用任何你喜欢的单词作为标签.(你甚至可以使用短语,但你可能需要拉宽检视面板来查看标签的全名.)