Проверено с версией:: 4
Tags in Unity, how to use them in the editor and via code.
Базовая Interface & Essentials
- 00:00 - 00:03
Tags are a way of identifying game objects in Unity.
- 00:03 - 00:05
As a name of a single object could identify it,
- 00:05 - 00:08
it can be useful to set tags also.
- 00:08 - 00:12
For example you may have an object called Ork or Tank
- 00:12 - 00:14
but these could all be tagged Enemy,
- 00:14 - 00:16
and in your code you could check for any objects
- 00:16 - 00:17
that have the tag Enemy.
- 00:18 - 00:21
Likewise a script on an enemy could check for a player character
- 00:21 - 00:23
by looking for a player tag.
- 00:23 - 00:26
To assign a tag to an object, select it and use the
- 00:26 - 00:28
drop-down menu at the top of the inspector.
- 00:31 - 00:34
If the tag you want isn't already present then add a new tag.
- 00:34 - 00:38
You can add a tag by clicking the option at the bottom of the menu
- 00:38 - 00:41
and then entering it in the list of tags at the top
- 00:41 - 00:42
of the tag manager.
- 00:42 - 00:45
Once you've done this, return to the object you
- 00:45 - 00:46
wish to place the tag on
- 00:46 - 00:48
and select it from the drop-down.
- 00:50 - 00:51
There are a number of functions in code,
- 00:51 - 00:54
which will allow you to find objects with tags,
- 00:54 - 00:57
the simplest one of these is GameObject.FindWithTag,
- 00:57 - 01:00
which allows you to specify a string with the name
- 01:00 - 01:01
of the tag inside it.
- 01:01 - 01:04
This script is attached to my enemy object
- 01:06 - 01:10
and I can use that to find an object with the tag Player.
- 01:10 - 01:13
So I'll set my robot to be tagged Player
- 01:15 - 01:20
and when the game starts my enemy is seeking out that
- 01:20 - 01:21
object and looking at it.
- 01:22 - 01:26
Likewise you could find multiple objects with the same tag
- 01:26 - 01:29
by using FindGameObjectWithTag.
- 01:30 - 01:34
See the scripting reference for more examples of this.