Unity - Scripting API: SpriteRenderer
class in UnityEngine
/
Inherits from:Renderer
/
Implemented in:UnityEngine.CoreModule
Success!
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Submission failed
For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Description
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.
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. |
Public Methods
| Method | Description |
|---|---|
| RegisterSpriteChangeCallback | Registers a callback to receive a notification when the SpriteRenderer's Sprite reference changes. |
| UnregisterSpriteChangeCallback | Removes a callback (that receives a notification when the Sprite reference changes) that was previously registered to a SpriteRenderer. |
Inherited Members
Public Methods
| Method | Description |
|---|---|
| BroadcastMessage | Calls the method named methodName on every MonoBehaviour in this game object or any of its children. |
| CompareTag | Checks the GameObject's tag against the defined tag. |
| GetComponent | Gets a reference to a component of type T on the same GameObject as the component specified. |
| GetComponentInChildren | Gets a reference to a component of type T on the same GameObject as the component specified, or any child of the GameObject. |
| GetComponentIndex | Gets the index of the component on its parent GameObject. |
| GetComponentInParent | Gets a reference to a component of type T on the same GameObject as the component specified, or any parent of the GameObject. |
| GetComponents | Gets references to all components of type T on the same GameObject as the component specified. |
| GetComponentsInChildren | Gets references to all components of type T on the same GameObject as the component specified, and any child of the GameObject. |
| GetComponentsInParent | Gets references to all components of type T on the same GameObject as the component specified, and any parent of the GameObject. |
| SendMessage | Calls the method named methodName on every MonoBehaviour in this game object. |
| SendMessageUpwards | Calls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour. |
| TryGetComponent | Gets the component of the specified type, if it exists. |
| GetInstanceID | Gets the instance ID of the object. |
| ToString | Returns the name of the object. |
| GetClosestReflectionProbes | Returns an array of closest reflection probes with weights, weight shows how much influence the probe has on the renderer, this value is also used when blending between reflection probes occur. |
| GetMaterials | Returns all the instantiated materials of this object. |
| GetPropertyBlock | Get per-Renderer or per-Material property block. |
| GetSharedMaterials | Returns all the shared materials of this object. |
| HasPropertyBlock | Returns true if the Renderer has a material property block attached via SetPropertyBlock. |
| ResetBounds | Reset custom world space bounds. |
| ResetLocalBounds | Reset custom local space bounds. |
| SetMaterials | Assigns the shared materials of this renderer using the list of materials provided. |
| SetPropertyBlock | Lets you set or clear per-renderer or per-material parameter overrides. |
| SetSharedMaterials | Assigns the shared materials of this renderer using the list of materials provided. |
Static Methods
| Method | Description |
|---|---|
| Destroy | Removes a GameObject, component, or asset. |
| DestroyImmediate | Destroys the specified object immediately. Use with caution and in Edit mode only. |
| DontDestroyOnLoad | Do not destroy the target Object when loading a new Scene. |
| FindAnyObjectByType | Retrieves any active loaded object of Type type. |
| FindFirstObjectByType | Retrieves the first active loaded object of Type type. |
| FindObjectsByType | Retrieves a list of all loaded objects of Type type. |
| Instantiate | Clones the object original and returns the clone. |
| InstantiateAsync | Captures a snapshot of the original object (that must be related to some GameObject) and returns the AsyncInstantiateOperation. |
Operators
| Operator | Description |
|---|---|
| bool | Does the object exist? |
| operator != | Compares if two objects refer to a different object. |
| operator == | Compares two object references to see if they refer to the same object. |
Messages
| Message | Description |
|---|---|
| OnBecameInvisible | OnBecameInvisible is called when the object is no longer visible by any camera. |
| OnBecameVisible | OnBecameVisible is called when the object became visible by any camera. |