19
\$\begingroup\$

Unity has both a Texture2D class and a Sprite (with related SpriteRenderer). What is the difference between these two classes? Both represent an image, for 2D games should I favor one over the other?

Is the main difference between the two that using Sprite I don't need to create the "billboard" quad myself? And that the Sprite is always rendered directly against the camera?

\$\endgroup\$
16
\$\begingroup\$

You are correct - somewhat. Sprites, by default are rendered directly against the camera, however you can easily change this if you are using the Sprite Renderer in a 3D scene.

Sprites are physical objects in your scene, whereas Texture2D is exactly what it says it is. A texture. A texture must be attached to a material, and the material to a game object(e.g a plane).

Back in Unity 3.x days you didn't have sprite support right out of the box so you had to roll your own Sprite Manager/Class(or you had the option of buying an asset off the asset store that attempted to remedy this annoyance). Unity was really never meant to support 2D games(nevertheless developers found ways to make it happen), until 4.x, when the Unity Developers finally provided built-in support for 2D games(sprites, sprite sheets, 2D physics).

If you're doing a 2D game, always try to use the Sprite class. It's better than rolling your own, and offers more than enough.

\$\endgroup\$
  • \$\begingroup\$ AFAIK Sprites will also automatically be packed into atlases by Unity if you want. \$\endgroup\$ – bummzack Jan 16 '14 at 14:27
  • \$\begingroup\$ That is only true in Unity Pro, tricky buggers \$\endgroup\$ – jhocking Jan 21 '14 at 0:02
  • 4
    \$\begingroup\$ I believe this is now free with Unity 5 \$\endgroup\$ – Chris Hawkes Mar 6 '15 at 0:10
  • \$\begingroup\$ @ChrisHawkes Indeed it is - just to confirm the statement :) - sprites can be packed into atlases (and it's recommended practice) via Window > Sprite Packer \$\endgroup\$ – AgentKnopf Sep 27 '15 at 19:30
6
\$\begingroup\$

These are some of the advantages I love about Sprite :

  1. All SpriteRenderer use the same material by default, Sprite-Default even though they all have different image/atlas thus satisfying one condition of Unity's dynamic batching automatically.
  2. You can apply different color tint (via SpriteRenderer's inspector) to each sprite even though they all have the same Sprite-Default material. This is not possible in Texture2D based approach which 'color' property will be tied to the material. This is also very useful for fading out sprite via alpha value of color.
  3. Supports dynamic batching with non-uniform scaling.
  4. Has single mode/multiple mode so you can grab each image as separate sprite from your texture atlas with the multiple mode.
  5. You can grab the corner of sprite in scene view to scale or rotate it without changing to scale/rotate gizmo tool.
\$\endgroup\$
  • 4
    \$\begingroup\$ 6. Unity auto-generates geometry fit to the contours of the non-transparent parts of the sprite. This can reduce overdraw, compared to rendering the same sprite as a quad, and allow you/Unity to pack spritea more tightly in an atlas. \$\endgroup\$ – DMGregory May 20 '14 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.