SpriteRenderer
A component that renders a Sprite. If a GameObject has a SpriteRenderer component, the component renders the assigned Sprite asset on the screen using the position, rotation and scale from the Transform component. You can use this to draw characters, items, backgrounds, and other visual elements in a 2D game.
Read time 10 minutesLast updated 2 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
- Inherits from: Renderer
[RequireComponent(typeof(Transform))]public sealed class SpriteRenderer : Renderer
Remarks
Examples
using UnityEngine;// This example script demonstrates how to change the sprite and the sprite color, and flip the sprite horizontally and vertically.// Attach this script to a GameObject with a SpriteRenderer component and assign a Sprite to the Sprite field in the Inspector.public class Example : MonoBehaviour{ public Sprite sprite; private SpriteRenderer m_spriteRenderer; private void Awake() { // Fetch the SpriteRenderer from the GameObject m_spriteRenderer = GetComponent<SpriteRenderer>(); } private void Start() { // Change the sprite to the one in the sprite field m_spriteRenderer.sprite = sprite; // Change the color of the sprite to red m_spriteRenderer.color = Color.red; // Flip the sprite horizontally m_spriteRenderer.flipX = true; // Flip the sprite vertically m_spriteRenderer.flipY = true; }}
Properties
Property | Description |
|---|---|
| adaptiveModeThreshold | The current threshold for Sprite Renderer tiling. |
| color | Rendering color for the Sprite graphic. |
| drawMode | The current draw mode of the Sprite Renderer. |
| flipX | Flips the sprite on the X axis. |
| flipY | Flips the sprite on the Y axis. |
| maskInteraction | Specifies how the sprite interacts with the masks. |
| size | Property to set or get the size to render when the SpriteRenderer.drawMode is set to SpriteDrawMode.Sliced or SpriteDrawMode.Tiled. |
| sprite | The Sprite to render. |
| spriteSortPoint | Determines the position of the Sprite used for sorting the SpriteRenderer. |
| tileMode | The current tile mode of the Sprite Renderer. |
Methods
Method | Description |
|---|---|
| GetBlendShapeWeight | Gets the weight of a blend shape for this renderer. |
| RegisterSpriteChangeCallback | Registers a callback to receive a notification when the SpriteRenderer's Sprite reference changes. |
| SetBlendShapeWeight | Sets the weight of a blend shape for this renderer. |
| UnregisterSpriteChangeCallback | Removes a callback (that receives a notification when the Sprite reference changes) that was previously registered to a SpriteRenderer. |
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. |