Skip to content

🧩 SpriteRenderer

Ekrol34 edited this page Sep 17, 2025 · 4 revisions

The SpriteRenderer component is responsible for drawing 2D images (sprites) in the scene. It serves as the link between a GameObject's transform (position, rotation, scale) and the visual representation displayed on screen.

A SpriteRenderer manages one or more textures, allows dynamically switching between them, and controls rendering details such as smoothing. It is typically used for any game entity that requires a visual appearance.

All transformations applied to the GameObject (movement, scaling, rotation) are automatically propagated to the sprite, ensuring that the rendered image always matches the logical state of the object in the game world.

Constructors

Default Constructor

SpriteRenderer()

Creates a new SpriteRenderer instance. Initializes the internal sprite and allocates cardinal point references (top-left, top-right, bottom-left, bottom-right).

Fields

  • Vector2 originOffset: Offset applied to the sprite’s origin, relative to its center.

Methods

AddTexture

void AddTexture(String file, String name)

Loads a texture from disk and registers it with a unique name.

Parameters:

  • file: Path to the texture file.

  • name: Identifier to reference the texture later.

Notes: If the name already exists, the method logs a warning and ignores the request.

SetTexture

void SetTexture(String name)

Sets the current sprite texture by name.

Parameters:

  • name: The identifier of a previously added texture.

Notes: Logs a warning if the texture name does not exist.

SetAlpha

void SetAlpha(int alpha)

Changes the transparency of the sprite by setting its alpha channel.

GetAlpha

int GetAlpha()

Returns the current alpha value (transparency) of the sprite.

RemoveTexture

void RemoveTexture(String name)

Removes a texture from the component by its name.

Parameters:

  • name: The identifier of the texture to remove.

Notes: If the texture does not exist, a warning is logged.

SetTextureSmooth

void SetTextureSmooth(String name, bool flag = true)

Enables or disables smoothing for a specific texture.

Parameters:

  • name: The identifier of the texture to modify.

  • flag: true to enable smoothing, false to disable.

Notes: Logs a warning if the texture does not exist.

GetLocalCenter

Vector2 GetLocalCenter()

Returns the local center of the sprite (relative to its own coordinate space).

GetGlobalCenter

Vector2 GetGlobalCenter()

Returns the global center of the sprite (taking into account the world transform).

GetCardinals

Dictionary<String, Vector2Ptr> GetCardinals()

Returns a dictionary with references to the sprite’s four cardinal points:

  • top-left

  • top-right

  • bottom-left

  • bottom-right

These points are updated automatically each frame based on the sprite’s transform and size.

Clone this wiki locally