使用 UI 系统可以在世界中轻松创建位于场景内其他 2D 或 3D 对象之间的 UI。
如果场景中还没有 UI 元素(如图像),首先使用 GameObject > UI > Image 创建一个 UI 元素。此过程还将创建画布。
选择画布并将 Render Mode 更改为 World Space。
现在,画布已经定位在世界中,并可让所有摄像机看到(如果摄像机面向该画布),但与场景中的其他对象相比,画布可能过于巨大。我们稍后将讨论这一点。
首先,需要决定画布的分辨率应该是多少。如果是图像,图像的像素分辨率应该是多少?最开始设置为 800x600 这样的分辨率可能是比较好的选择。应在画布的矩形变换的 Width 和 Height 值中输入分辨率的相应值。同时,将位置设置为 0,0 可能是个不错的选择。
现在应考虑画布在世界中应该有多大。可使用缩放工具将画布缩小,直至其大小看起来合适,也可采用米为单位决定其应有的大小。
如果希望画布具有以米为单位的特定宽度,则可使用 meter_size / canvas_width 计算所需的比例。例如,如果希望画布宽为 2 米,而实际画布宽度为 800,则会得到 2 / 800 = 0.0025 的比例。然后,将画布上的矩形变换的 Scale 属性对于 X、Y 和 Z 均设置为 0.0025,从而确保画布均匀缩放。
另一种思路是控制画布中一个像素的大小。如果画布缩放到 0.0025 倍,那么这也是画布中每个像素在世界中的大小。
与设置为 Screen Space 的画布不同,World Space 画布可以在场景中自由定位和旋转。画布可以放置在任何墙壁、地板、天花板或倾斜表面上(当然也可以在空中自由悬挂)。只需使用工具栏中的常规移动和旋转工具即可。
现在,可以像使用 Screen Space 画布一样开始设置 UI 元素和布局。
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.