Version: Unity 6.1 Alpha (6000.1)
LanguageEnglish
  • C#

SpriteRenderer

class in UnityEngine

/

Inherits from:Renderer

/

Implemented in:UnityEngine.CoreModule

Suggest a change

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.

Close

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.

Close

Cancel

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

adaptiveModeThresholdThe current threshold for Sprite Renderer tiling.
colorRendering color for the Sprite graphic.
drawModeThe current draw mode of the Sprite Renderer.
flipXFlips the sprite on the X axis.
flipYFlips the sprite on the Y axis.
maskInteractionSpecifies how the sprite interacts with the masks.
sizeProperty to set or get the size to render when the SpriteRenderer.drawMode is set to SpriteDrawMode.Sliced or SpriteDrawMode.Tiled.
spriteThe Sprite to render.
spriteSortPointDetermines the position of the Sprite used for sorting the SpriteRenderer.
tileModeThe current tile mode of the Sprite Renderer.

Public Methods

RegisterSpriteChangeCallbackRegisters a callback to receive a notification when the SpriteRenderer's Sprite reference changes.
UnregisterSpriteChangeCallbackRemoves a callback (that receives a notification when the Sprite reference changes) that was previously registered to a SpriteRenderer.

Inherited Members

Properties

gameObjectThe game object this component is attached to. A component is always attached to a game object.
tagThe tag of this game object.
transformThe Transform attached to this GameObject.
hideFlagsShould the object be hidden, saved with the Scene or modifiable by the user?
nameThe name of the object.
allowOcclusionWhenDynamicControls if dynamic occlusion culling should be performed for this renderer.
boundsThe bounding box of the renderer in world space.
enabledMakes the rendered 3D object visible if enabled.
forceRenderingOffAllows turning off rendering for a specific component.
isLOD0Is the renderer the first LOD level in its group.
isPartOfStaticBatchIndicates whether the renderer is part of a static batch with other renderers.
isVisibleIs this renderer visible in any camera? (Read Only)
lightmapIndexThe index of the baked lightmap applied to this renderer.
lightmapScaleOffsetThe UV scale & offset used for a lightmap.
lightProbeProxyVolumeOverrideIf set, the Renderer will use the Light Probe Proxy Volume component attached to the source GameObject.
lightProbeUsageThe light probe interpolation type.
localBoundsThe bounding box of the renderer in local space.
localToWorldMatrixMatrix that transforms a point from local space into world space (Read Only).
LODGroupThe LODGroup for this Renderer.
materialReturns the first instantiated Material assigned to the renderer.
materialsReturns all the instantiated materials of this object.
motionVectorGenerationModeSpecifies the mode for motion vector rendering.
probeAnchorIf set, Renderer will use this Transform's position to find the light or reflection probe.
rayTracingAccelerationStructureBuildFlagsThe flags Unity uses when it builds acceleration structures associated with geometry used by renderers.
rayTracingAccelerationStructureBuildFlagsOverrideWhether to override the default build flags specified when creating a RayTracingAccelerationStructure.
rayTracingModeDescribes how this renderer is updated for ray tracing.
realtimeLightmapIndexThe index of the real-time lightmap applied to this renderer.
realtimeLightmapScaleOffsetThe UV scale & offset used for a real-time lightmap.
receiveShadowsDoes this object receive shadows?
reflectionProbeUsageShould reflection probes be used for this Renderer?
rendererPriorityThis value sorts renderers by priority. Lower values are rendered first and higher values are rendered last.
renderingLayerMaskDetermines which rendering layer this renderer lives on, if you use a scriptable render pipeline.
shadowCastingModeDoes this object cast shadows?
sharedMaterialThe shared material of this object.
sharedMaterialsAll the shared materials of this object.
sortingLayerIDUnique ID of the Renderer's sorting layer.
sortingLayerNameName of the Renderer's sorting layer.
sortingOrderRenderer's order within a sorting layer.
staticShadowCasterIs this renderer a static shadow caster?
worldToLocalMatrixMatrix that transforms a point from world space into local space (Read Only).

Public Methods

BroadcastMessageCalls the method named methodName on every MonoBehaviour in this game object or any of its children.
CompareTagChecks the GameObject's tag against the defined tag.
GetComponentGets a reference to a component of type T on the same GameObject as the component specified.
GetComponentInChildrenGets a reference to a component of type T on the same GameObject as the component specified, or any child of the GameObject.
GetComponentIndexGets the index of the component on its parent GameObject.
GetComponentInParentGets a reference to a component of type T on the same GameObject as the component specified, or any parent of the GameObject.
GetComponentsGets references to all components of type T on the same GameObject as the component specified.
GetComponentsInChildrenGets references to all components of type T on the same GameObject as the component specified, and any child of the GameObject.
GetComponentsInParentGets references to all components of type T on the same GameObject as the component specified, and any parent of the GameObject.
SendMessageCalls the method named methodName on every MonoBehaviour in this game object.
SendMessageUpwardsCalls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour.
TryGetComponentGets the component of the specified type, if it exists.
GetInstanceIDGets the instance ID of the object.
ToStringReturns the name of the object.
GetClosestReflectionProbesReturns 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.
GetMaterialsReturns all the instantiated materials of this object.
GetPropertyBlockGet per-Renderer or per-Material property block.
GetSharedMaterialsReturns all the shared materials of this object.
HasPropertyBlockReturns true if the Renderer has a material property block attached via SetPropertyBlock.
ResetBoundsReset custom world space bounds.
ResetLocalBoundsReset custom local space bounds.
SetMaterialsAssigns the shared materials of this renderer using the list of materials provided.
SetPropertyBlockLets you set or clear per-renderer or per-material parameter overrides.
SetSharedMaterialsAssigns the shared materials of this renderer using the list of materials provided.

Static Methods

DestroyRemoves a GameObject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadDo not destroy the target Object when loading a new Scene.
FindAnyObjectByTypeRetrieves any active loaded object of Type type.
FindFirstObjectByTypeRetrieves the first active loaded object of Type type.
FindObjectsByTypeRetrieves a list of all loaded objects of Type type.
InstantiateClones the object original and returns the clone.
InstantiateAsyncCaptures a snapshot of the original object (that must be related to some GameObject) and returns the AsyncInstantiateOperation.

Operators

boolDoes 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

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.