In this article, we will discuss Unity 3D Canvas. Before we move further to learn Canvas we must have to understand Unity 3D. So, Unity3D is a user-friendly development environment and a solid cross-platform 3D engine. Unity should appeal to everyone who wants to create 3D games and applications for mobile, desktop, the web, and consoles. It is simple enough for beginners and powerful enough for experts.
A canvas is not a large painting board on a wooden frame. It's a gaming area where you can draw user interface elements.
Let's move to the next section to know more about Unity 3D Canvas.
What is Canvas
All UI elements must be children of a Canvas, a Game Object with a Canvas component. All UI elements should be contained within the Canvas. If there is not already a canvas in the scene while creating a new UI element, such as an image using the menu GameObject -> UI -> Image, it automatically generates a canvas.
Canvas highlights as a rectangle in the Scene view. Canvas allows you to arrange UI items without keeping the Game View open all the time. Canvas uses the EventSystem object to assist the messaging system.
source - www.youtube.com/c/unity
Let’s move to the next section for a better understanding.
Draw order of Elements
The Canvas draws UI elements in the same order as they appear in the Hierarchy. The first child is removed first, followed by the second, and so forth. When two UI elements overlap, the latter appears on top of the former.
Reorder the hierarchy items by dragging them to modify which element appears on top of other elements. SetAsFirstSibling, SetAsLastSibling, and SetSiblingIndex methods on the Transform component can change the order from scripting.
In the next section, we'll discuss its property.
Canvas Component Properties
Canvas scripting is the first segment of the canvas itself. It has characteristics that control where and how the Canvas appears on the screen. Canvas Scalar, the second segment, is used to adjust UI elements' size and scales to fit different screen sizes.
The Render Mode is the most crucial property in the Canvas script.
Let’s understand Render Mode.
Render Modes
The Render Mode setting on the Canvas can be used to render in screen space or world space. The three Render Mode modes control how the Canvas is handled and rendered in the game.
Let’s know three Render Modes for better understanding.
Screen-Space Overlay
This Mode places UI elements at the top of the screen of the scene rendered. Mainly used when making a HUD or any similar static UI element. If the screen is enlarged or the resolution changes, the Canvas will change the size to match.
source- docs.unity3d.com
Screen-Space Camera
This Render Mode working is as similar to Overlay, but the difference is in this Mode, a canvas is positioned in front of a camera at a specific distance. This camera renders the UI elements; hence the Camera settings impact how the UI appears. When the screen is resized, the resolution changes, or the camera frustum changes, the Canvas automatically adjusts to fit. Developers rarely utilize this option because they prefer Overlay because of its ability to auto-scale the UI.
source- docs.unity3d.com
World Space
This Render Mode is also known as the "Diegetic interface" and is quite different from the other two modes. The Canvas will function similarly to any other scene object in this Render Mode. UI elements will render in front of or behind other items in the scene based on 3D positioning, and the size of the Canvas can be manually changed using its Rect Transform. It is crucial for UI, which is supposed to be part of the environment. In World Space, UI items have been regarded as another gameObject on the screen rather than being rendered first.
Let us now see some of the FAQs related to the topic.
Frequently Asked Questions
What is UI Element in Unity?
UI Elements is a retained-mode GUI system that adds stylesheets, dynamic/contextual event handling, and accessibility.
What is Unity 3D Canvas?
All UI elements should be contained within the Canvas. All UI elements must be children of a Canvas, a Game Object with a Canvas component.
What is Render Mode?
Render Mode lets you select whether or not the item uses transparency and, if so, which blending mode to use. Opaque - This is the default, and it's best for solid objects that don't have any transparent parts.
Can I have two canvases in Unity?
A single Canvas is adequate for all UI elements, although several Canvases in the scene are allowed. Nested Canvases, in which one Canvas is inserted as a child of another for optimization considerations, are also options. The Render Mode of a nested Canvas is the same as its parent.
What is Canvas Group Unity?
The Canvas Set can control specific aspects of a group of UI elements from a single location rather than dealing with each one separately. The Canvas Group's properties influence both the GameObject and its offspring.
Let’s conclude the article.
Conclusion
In this article, we have extensively discussed Unity 3D Canvas. We have learned canvas component properties, it's working, and also some faqs related to this topic.