Unity 3D:将UI元素添加到游戏画布

Unity 3D:将UI元素添加到游戏画布 (Unity 3D: Adding UI Elements to Game Canvas)

So now that we have a good idea of how the Canvas works, let's try adding our own elements into the game. We'll start with a very simple label. A label is simply static text that you use on the User Interface. Let's have our label say Use the arrow keys to move.

因此,既然我们对Canvas的工作原理有了一个很好的了解,让我们尝试将自己的元素添加到游戏中。 我们将从一个非常简单的标签开始。 标签只是您在用户界面上使用的静态文本。 让我们让标签说“ 使用箭头键移动”

First off, to add any UI element, we now know that we need a Canvas first. However, Unity saves us the trouble of inserting a new Canvas into the scene, by doing it automatically when you create a UI element in a scene that doesn't already have a Canvas.

首先,要添加任何UI元素,我们现在知道首先需要Canvas 。 但是,当您在尚无Canvas的场景中创建UI元素时,通过自动执行操作,Unity避免了将新Canvas插入场景的麻烦。

To add a label into the scene, right click in the Hierarchy and select UI Elements → Text.

要将标签添加到场景中,请在“ 层次结构”中单击鼠标右键,然后选择“ UI元素”→“文本”

Unity 3D:将UI元素添加到游戏画布



As soon as you click on Text, you'll notice a new addition in your Hierarchy view.

一旦单击“ 文本” ,您就会在“ 层次结构”视图中注意到一个新的添加。

Unity 3D:将UI元素添加到游戏画布



You can safely ignore or get rid of the EventSystem gameObject. It deals with, as the name implies, handling events, something that we won't be covering as of now. See how Unity added a new Canvas and automatically made the Text element a child of that Canvas.

您可以放心地忽略或摆脱EventSystem游戏对象。 顾名思义,它处理事件,这是我们目前不会涉及的内容。 了解Unity如何添加新的Canvas并自动将Text元素设为该Canvas的子元素。

Now, have a look at the properties of the Text Label.

现在,看看“文本标签”的属性。

Unity 3D:将UI元素添加到游戏画布



Most of what you see is quite self-explanatory. There are options for changing the color of the text, text size, font (by importing Font files into the Assets), alignments and so on. Now, take a look at the Game view.

您所看到的大部分内容都是不言而喻的。 有一些选项可用于更改文本的颜色 ,文本大小字体 (通过将字体文件导入到资源中), 对齐方式等。 现在,看看“游戏”视图。

Unity 3D:将UI元素添加到游戏画布



What's this new corner (circular dot) that's appeared on the screen? Try zooming out of the game until you can see the Canvas entirely.

屏幕上出现的这个新角(圆点)是什么? 尝试缩小游戏,直到完全看到“画布”为止。

Unity 3D:将UI元素添加到游戏画布

Unity 3D:了解画布大小 (Unity 3D: Understanding the Canvas Size)

Why is the Canvas so large? You see, lengths in Unity are measured in Units. These units are tiny little squares that span the entirety of the game world. The Canvas uses a default scaling factor of 1 UI pixel = 1 Unit, meaning the Canvas is actually 800 units long in the scene editor.

为什么画布这么大? 您会看到,Unity中的长度以Units为单位 。 这些单位是覆盖整个游戏世界的微小小方块。 画布使用默认的比例因子1 UI pixel = 1 Unit ,这意味着画布在场景编辑器中的长度实际上为800单位。

Unity 3D:将UI元素添加到游戏画布



Now that the size understanding is out of our way, select the Text element in the Hierarchy, and zoom in on the Text so that you can see it properly. Let's give the text a bright red color and write what we want it to say in the Text field.

既然已经不再需要大小了,请在Hierarchy中选择Text元素,然后放大Text以使您可以正确地看到它。 让我们给文本加上鲜红的颜色,然后在“ 文本”字段中写下我们想要说的内容。

Unity 3D Canvas:格式化文本UI元素 (Unity 3D Canvas: Formatting the Text UI Element)

Unity 3D:将UI元素添加到游戏画布



Since the default Label size is slightly shorter than the text that we want it to show, we can increase the size of our label box by dragging the blue handles that we've been using so far with our sprites. In case you can't see the blue handles, simply click on the last option in the top-left corner of the editor, with Text selected.

由于默认的Label 大小比我们想要显示的文本短一点,因此我们可以通过拖动到目前为止我们一直使用的Sprite的蓝色手柄来增加Label框的大小。 如果看不到蓝色手柄,只需单击编辑器左上角的最后一个选项,然后选择“ 文本”即可

Unity 3D:将UI元素添加到游戏画布



We would also recommend trying to change the Horizontal and Vertical Overflow options in the Text's properties, since they deal with the same issue that we just solved by resizing.

我们还建议您尝试更改“文本”属性中的“ 水平垂直溢出”选项,因为它们处理的是我们刚刚通过调整大小解决的相同问题。

Now, drag around the Label to the top right corner of the giant Canvas. You'll see that Unity has a lot of area where UI elements will fix itself into place. This includes corners and middle-points of the Canvas. This will be very useful when you work on more complex UIs that need to look organized and aligned.

现在,将Label拖动到巨型Canvas的右上角。 您将看到Unity拥有很多区域,UI元素可以将其固定在适当的位置。 这包括“画布”的角和中间点。 当您使用需要看起来井井有条和对齐的更复杂的UI时,这将非常有用。

Unity 3D:将UI元素添加到游戏画布



Finally, change the size of the Label further and increase the font size in the properties. This ensures we have a Label that's clearly visible in the game.

最后,进一步更改标签的大小并增加属性中的字体大小。 这样可以确保我们拥有在游戏中清晰可见的标签。

Unity 3D:将UI元素添加到游戏画布



In the next tutorial we will leatn how to update the text UI element in realtime to use it to display score and other game progress information.

在下一个教程中,我们将学习如何实时更新文本UI元素,以使用它显示分数和其他游戏进度信息。

翻译自: https://www.studytonight.com/game-development-in-2D/adding-ui-elements