纳金网

标题: [Unity 组件参考手册]脚本概念之什么是标签 [打印本页]

作者: .    时间: 2013-2-26 17:39
标题: [Unity 组件参考手册]脚本概念之什么是标签
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 the GameObject.FindWithTag() function.一个标签是你用来索引一个或一组游戏对象的词。例如,你可以为玩家控制的角色定义Player和Enemy标签,区分出非玩家控制角色;一个Collectable标签能被用于定义玩家能在场景中收集的物体;等等。简单来说,标签是为了编程的目的而对游戏对象的标注。我们能使用标签来书写脚本代码通过搜索找到包含我们想要的标签的对象。通过GameObject.FindWithTag() 我们可以实现这个功能。For example 例如 :// Instantiates respawnPrefab at the location
// of the game object with tag "Respawn"
// 在标签为"Respawn"的游戏对象的位置实例化预设var respawnPrefab : GameObject;
var respawn = GameObject.FindWithTag ("Respawn");
Instantiate (respawnPrefab, respawn.position, respawn.rotation);This saves us having to manually add our GameObjects to a script抯 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 Trigger Collider 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.这可以让我们拥有通过鼠标拖放来扩展我们使用脚本添加对象时自定义位置的能力 -- 如果同样的脚本被多次使用,可以节约很多时间。另一个例子是一个碰撞触发器控制脚本, 它需要计算出角色是和一个敌方角色交互呢,还是相反,对话,随机道具的使用或是整理物品,标签将使这类的测试变的简单。
【Applying a Tag 应用一个标签】The Inspector will show the Tag and Layer 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:在检视面板任意游戏物体名字的下面显示Tag和Layer下拉菜单。来应用一个标签给一个游戏物体,打开标签下拉菜单并选择一个标签:The GameObject will now be associated with this Tag.
该游戏物体将于这个标签相关联。
【Creating new Tags 创建新的标签】To create a new Tag, click the Add new 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.要创建一个新的标签,在下拉菜单的末尾,点击Add tag...选项,这将在检视面板打开标签管理器。在这里看标签管理器的描述。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怎样在场景渲染游戏物体。参见Layers页面的更多信息。
【Hints 提示】    A GameObject can only have one Tag assigned to it.
    一个游戏物体只能指定一个标签。
    Unity includes some built-in Tags which do not appear in the Tag Manager:
    Unity包含一些内置标签,没有显示在标签管理器:
        "Untagged"
        "Respawn"
        "Finish"
        "EditorOnly"
        "MainCamera"
        "layer"
        and "GameController".
    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.)
    可以使用任意单词作为标签。(你甚至可以使用短句,但名称太长的话会撑开下拉菜单)
【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/




欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5