SpriteRenderer¶
A SpriteRenderer can source its texture either directly via textureId, or by referencing a
named sub-rect of a sprite sheet via spriteId. A Sprite is a named rectangle (with its own pivot
and nine-slice border) cut out of a Texture2D asset whose usage is Sprite2D; sprites are authored
in the Sprite Editor and requested by name via
request_sprite(path_to_asset, name_in_asset).
When spriteId is set it overrides textureId and the sprite’s own pivot; uvRect keeps
working, but is re-interpreted as a sub-rect within the sprite’s own rect (clamped so it can never
sample outside of it) rather than the whole atlas – useful for animating a multi-frame sprite by
stepping uvRect across an already-cut-out cell.
sortPoint controls how overlapping sprites are ordered for depth/screen-Y sorting: Center
(default) sorts by the sprite’s geometric center, while Pivot sorts by the node’s transform
position instead – mirroring Unity’s SpriteRenderer.spriteSortPoint. This only matters when the
sprite’s pivot is off-center, since the sprite’s geometry is shifted in world space so the pivot
lines up with the node’s transform.
In the editor¶
textureId and spriteId are mutually exclusive in the inspector: assigning one greys out the
other, so you never end up editing a stale field that has no effect. Clear whichever one is
currently set to switch to the other.
Classes¶
- SpriteRenderer : NativeComponent¶
This component allows to render textures as sprites.
Usage example:
node.add_component(new SpriteRenderer(
textureId = spriteTexture,
color = spriteColor,
flipX = false,
flipY = false,
uvRect = float4(0, 0, 1, 1),
useTextureSizeForScaling = true,
renderOrder = 0
))
Or, driven by a named Sprite sub-resource instead of a raw texture:
node.add_component(new SpriteRenderer(
spriteId = request_sprite("sprites.png", "player_idle"),
sortPoint = SpriteSortPoint.Pivot
))
- Properties:
- SpriteRenderer.textureId: TextureId¶
- SpriteRenderer.textureId =(value: TextureId)¶
Sprite texture resource. Ignored while spriteId is set (spriteId supplies the atlas texture instead), so its inspector field is disabled – clear spriteId to edit this.
- Arguments:
value : TextureId
- SpriteRenderer.spriteId: SpriteId¶
- SpriteRenderer.spriteId =(value: SpriteId)¶
Named sub-rect (Sprite) resource. If set, overrides textureId and pivot; uvRect is applied within the sprite rect. Its inspector field is disabled while textureId is set, to keep the two mutually exclusive in the editor – clear textureId first to assign a sprite.
- Arguments:
value : SpriteId
- SpriteRenderer.color: float4¶
- SpriteRenderer.color =(value: float4)¶
Color for the sprite
- Arguments:
value : float4
- SpriteRenderer.flipX: bool¶
- SpriteRenderer.flipX =(value: bool)¶
Flip sprite horizontally
- Arguments:
value : bool
- SpriteRenderer.flipY: bool¶
- SpriteRenderer.flipY =(value: bool)¶
Flip sprite vertically
- Arguments:
value : bool
- SpriteRenderer.uvRect: float4¶
- SpriteRenderer.uvRect =(value: float4)¶
.xy is uv offset and .zw is uv scale, default is (0, 0, 1, 1). With spriteId set, selects a sub-rect within the sprite rect (clamped to it).
- Arguments:
value : float4
- SpriteRenderer.useTextureSizeForScaling: bool¶
- SpriteRenderer.useTextureSizeForScaling =(value: bool)¶
Use texture size for scaling. Default is true. It multiply localScale.xy by texture size divided by 100.
- Arguments:
value : bool
- SpriteRenderer.sortPoint: SpriteSortPoint¶
- SpriteRenderer.sortPoint =(value: SpriteSortPoint)¶
Sort point used for depth/screen-Y ordering among overlapping sprites. Default is Center.
- Arguments:
value : SpriteSortPoint
- SpriteRenderer.renderOrder: int¶
- SpriteRenderer.renderOrder =(value: int)¶
Explicit sorting order for the sprite. Default is 0. Then bigger this value, the later sprite will be rendered.
- Arguments:
value : int