Version: 2022.3
Language : English
Making UI elements fit the size of their content
Creating UI elements from scripting

Creating a World Space UI

The UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary
system makes it easy to create UI that is positioned in the world among other 2D or 3D objectsA 3D GameObject such as a cube, terrain or ragdoll. More info
See in Glossary
in the SceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary
.

Start by creating a UI element (such as an Image) if you don’t already have one in your scene by using GameObject > UI > Image. This will also create a Canvas for you.

Set the Canvas to World Space

Select your Canvas and change the Render Mode to World Space.

Now your Canvas is already positioned in the World and can be seen by all camerasA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary
if they are pointed at it, but it is probably huge compared to other objects in your Scene. We’ll get back to that.

Decide on a resolution

First you need to decide what the resolution of the Canvas should be. If it was an image, what should the pixelThe smallest unit in a computer image. Pixel size depends on your screen resolution. Pixel lighting is calculated at every screen pixel. More info
See in Glossary
resolution of the image be? Something like 800x600 might be a good starting point. You enter the resolution in the Width and Height values of the Rect Transform of the Canvas. It’s probably a good idea to set the position to 0,0 at the same time.

Specify the size of the Canvas in the world

Now you should consider how big the Canvas should be in the world. You can use the Scale tool to simply scale it down until it has a size that looks good, or you can decide how big it should be in meters.

If you want it to have a specific width in meters, you can can calculate the needed scale by using meter_size / canvas_width. For example, if you want it to be 2 meters wide and the Canvas width is 800, you would have 2 / 800 = 0.0025. You then set the Scale property of the Rect Transform on the Canvas to 0.0025 for both X, Y, and Z in order to ensure that it’s uniformly scaled.

Another way to think of it is that you are controlling the size of one pixel in the Canvas. If the Canvas is scaled by 0.0025, then that is also the size in the world of each pixel in the Canvas.

Position the Canvas

Unlike a Canvas set to Screen Space, a World Space Canvas can be freely positioned and rotated in the Scene. You can put a Canvas on any wall, floor, ceiling, or slanted surface (or hanging freely in the air of course). Just use the normal Translate and Rotate tools in the toolbarA row of buttons and basic controls at the top of the Unity Editor that allows you to interact with the Editor in various ways (e.g. scaling, translation). More info
See in Glossary
.

Create the UI

Now you can begin setting up your UI elements and layouts the same way you would with a Screen Space Canvas.

Making UI elements fit the size of their content
Creating UI elements from scripting