3
\$\begingroup\$

I'm using unity for a 2D game.

We have a lot of props for grass, plants, flowers, etc that are static and always in the background. I'd like to optimize the background and reduce the drawcalls as much as possible by making a big image with the final result of the background.

The idea would be to draw on a texture all the elements in the background, and then use this texture as the background. This way a single element has to be drawn.

How can I create these background texture at runtime, so it is generated as soon as the scene is loaded?

\$\endgroup\$
6
  • \$\begingroup\$ Unity already optimizes the drawcalls by packing sprites into one texture. How many elements and drawcalls do you currently have? \$\endgroup\$
    – bummzack
    Apr 28, 2015 at 12:36
  • \$\begingroup\$ Around 200 drawcalls, even with a package name set on the background. Its all in the same Sorting layer, but in different Sorting orders and different Z positions. There are a lot of different elements in different positions. \$\endgroup\$
    – Leo
    Apr 28, 2015 at 12:43
  • \$\begingroup\$ Did you isolate the problem? Are you sure the drawcalls are due to the sprites? Did you enable the sprite-packer in the settings (must set to "Always Enabled" or it won't work when testing in the Editor). If I enable the sprite-packer I regularly get just 1 drawcall for simple scenes. \$\endgroup\$
    – bummzack
    Apr 28, 2015 at 13:21
  • \$\begingroup\$ I am 100% sure the drawcalls are from the background, since there is nothing else in the scene. And the option for the sprite packer is already set in "Always Enabled". I was also expecting 1 drawcall, since everything is statci, and nothing gets between the different background props \$\endgroup\$
    – Leo
    Apr 28, 2015 at 14:38
  • \$\begingroup\$ Did you specify a packing-tag for the sprites? \$\endgroup\$
    – bummzack
    Apr 28, 2015 at 14:48

2 Answers 2

1
\$\begingroup\$

While you could render your scene to a texture and use this as the background, it's most likely not worth the effort. Here's why:

  • Packing your Sprites should already provide a significant reduction in draw-calls (potentially you can draw your whole background with just 1 draw-call and thus your only optimization would be the amount of triangles to draw)
  • If you're planning to animate some parts of the background sometime later in the development process, you're stuck with a very inflexible solution.
  • You might introduce (minor) lag at the start of the level because of the initialization and rendering of the RenderTexture.

If your sprites use lots of different blend-modes or other features that trigger new draw-calls, and if you're sure you're not going to animate your background in foreseeable future, then the RenderTexture is probably right for you. In all other cases I'd first optimize the drawcalls by making use of sprite-packing.

\$\endgroup\$
0
\$\begingroup\$

What you're describing is referred to as "render to texture". As in "render the scene and store that rendering in a texture." Unity provides a special kind of texture for that purpose, called a Render Texture.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .