2D

Sprite Renderer

The SpriteRenderer component is used to display images in 3D/2D scenes.

Note: By default, the sprite renderer places the image on the XoY plane.

avatar

Properties

PropertyTypeDescription
spriteSpriteReference to the sprite
widthNumberWidth of the sprite renderer; if not customized by the developer, defaults to the sprite width
heightNumberHeight of the sprite renderer; if not customized by the developer, defaults to the sprite height
colorColorColor of the sprite
flipXBooleanWhether to flip on the X-axis when rendering
flipYBooleanWhether to flip on the Y-axis when rendering
drawModeSpriteDrawModeDrawing mode, supports normal, nine-slice, and tiling drawing modes
maskInteractionSpriteMaskInteractionMask interaction type, used to set whether the sprite needs a mask, and if so, whether to display inside or outside the mask
maskLayerSpriteMaskLayerMask layer to which the sprite belongs, used for matching with SpriteMask, defaults to Everything, indicating it can be masked with any SpriteMask

Usage

Creation

Create a Node with Sprite Renderer

By selecting a node in the Hierarchy Panel, you can quickly add a child node with a sprite renderer by right-clicking -> 2D Object -> Sprite Renderer.

avatar

Attach Sprite Renderer to a Node

To attach a sprite renderer to an existing node, simply go to the Inspector Panel of the selected node, then choose Add Component -> 2D -> Sprite Renderer.

avatar

Script Creation

Similarly, in scripts, we can attach a sprite renderer to a node with the following code:

const spriteRenderer = entity.addComponent(SpriteRenderer);
spriteRenderer.sprite = sprite;

Set Sprite

When displaying an image, first add a sprite component to an entity and then set the sprite asset as follows:

avatar

Rendering Size

Setting the width and height of SpriteRenderer can explicitly specify the size of the sprite in 3D space. If not set, the size of the Sprite will be used as the default value.

Set Color

Adjust the color by setting the color property to achieve fade-in and fade-out effects, as shown below:

avatar

Image Flip

In addition to basic image display, SpriteRenderer also supports image flipping. Simply set the flipX/flipY properties to achieve flipping, as shown below:

avatar

Drawing Modes

The sprite renderer currently provides three drawing modes: normal, nine-slice, and tiled drawing (default is normal drawing). By modifying the drawing width and height in different drawing modes, you can visually perceive the rendering differences between the modes, as shown below:

Masking

Please refer to the Sprite Mask documentation.

Custom Materials

Please refer to the Custom Shader documentation.

Was this page helpful?