Canvas
Element that can be used for screen rendering.
Read time 8 minutesLast updated 5 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.UIModule
- Inherits from: Behaviour
[RequireComponent(typeof(RectTransform))]public sealed class Canvas : Behaviour
Remarks
Elements on a canvas are rendered AFTER Scene rendering, either from an attached camera or using overlay mode.
Examples
using System.Collections;using System.Collections.Generic;using UnityEditor;using UnityEngine;using UnityEngine.UI;// Create a Canvas that holds a Text GameObject.public class ExampleClass : MonoBehaviour{ void Start() { GameObject myGO; GameObject myText; Canvas myCanvas; Text text; RectTransform rectTransform; // Canvas myGO = new GameObject(); myGO.name = "TestCanvas"; myGO.AddComponent<Canvas>(); myCanvas = myGO.GetComponent<Canvas>(); myCanvas.renderMode = RenderMode.ScreenSpaceOverlay; myGO.AddComponent<CanvasScaler>(); myGO.AddComponent<GraphicRaycaster>(); // Text myText = new GameObject(); myText.transform.parent = myGO.transform; myText.name = "wibble"; text = myText.AddComponent<Text>(); text.font = (Font)Resources.Load("MyFont"); text.text = "wobble"; text.fontSize = 100; // Text position rectTransform = text.GetComponent<RectTransform>(); rectTransform.localPosition = new Vector3(0, 0, 0); rectTransform.sizeDelta = new Vector2(400, 200); }}
Properties
Property | Description |
|---|---|
| additionalShaderChannels | Get or set the mask of additional shader channels to be used when creating the Canvas mesh. |
| cachedSortingLayerValue | Cached calculated value based upon SortingLayerID. |
| isRootCanvas | Is this the root Canvas? |
| normalizedSortingGridSize | The normalized grid size that the canvas will split the renderable area into. |
| overridePixelPerfect | Allows for nested canvases to override pixelPerfect settings inherited from parent canvases. |
| overrideSorting | Allows for nested canvases to override the Canvas.sortingOrder from parent canvases. |
| pixelPerfect | Forces pixel alignment for elements in the canvas. It only applies when Canvas.renderMode is set to Screen Space. |
| pixelRect | Get the render rect for the Canvas. |
| planeDistance | How far away from the camera is the Canvas generated? It only applies when Canvas.renderMode is set to RenderMode.ScreenSpaceCamera. |
| referencePixelsPerUnit | The number of pixels per unit that is considered the default. |
| renderingDisplaySize | Provides the pixel dimensions of the display area where the UI canvas is rendered. |
| renderMode | Is the Canvas in World or Overlay mode? |
| renderOrder | The render order in which the canvas is being emitted to the Scene. (Read Only) |
| rootCanvas | Returns the Canvas closest to root, by checking through each parent and returning the last canvas found. If no other canvas is found then the canvas will return itself. |
| scaleFactor | Scales the entire canvas, ensuring it fits the screen. It only applies when Canvas.renderMode is set to Screen Space. |
| sortingLayerID | Unique ID of the Canvas' sorting layer. |
| sortingLayerName | Name of the Canvas' sorting layer. |
| sortingOrder | Canvas' order within a sorting layer. |
| targetDisplay | For Overlay mode, display index on which the UI canvas will appear. |
| updateRectTransformForStandalone | Should the Canvas size be updated based on the render target when a manual Camera.Render call is performed. |
| useReflectionProbes | Enables reflections on the Canvas when Canvas.renderMode is set to World Space. |
| vertexColorAlwaysGammaSpace | Should the Canvas vertex color always be in gamma space before passing to the UI shaders in linear color space work flow. |
| worldCamera | Camera used for sizing the Canvas when in Screen Space - Camera. Also used as the Camera that events will be sent through for a World Space Canvas. |
Static Properties
Property | Description |
|---|---|
| batchingInterval | Specifies whether Unity updates canvases for batching only when rendering is performed, such as with OnDemandRendering, or every update at runtime in Player builds. |
Static Methods
Method | Description |
|---|---|
| ForceUpdateCanvases | Force all canvases to update their content. |
| GetDefaultCanvasMaterial | Returns the default material that can be used for rendering normal elements on the Canvas. |
| GetETC1SupportedCanvasMaterial | Gets or generates the ETC1 Material. |
Static Events
Member | Description |
|---|---|
| preWillRenderCanvases | Event that is called just before Canvas rendering happens. |
| willRenderCanvases | Event that is called before Canvas is rendered. |
Enums
Enum | Description |
|---|---|
| Canvas.BatchingInterval | Options for when Unity updates canvases for batching. |
Inheritance
Operators
Operator | Description |
|---|---|
| operator == | Compares two object references to see if they refer to the same object. |
| bool | Determines whether the object exists. |
| operator != | Compares if two objects refer to a different object. |