- Home /
Sprite Renderer Performance
We are developing a 2D Game with many sprites.
Only the background has 729 sprites. These sprites are semi transparent.
All sprites are in one atlas; so that our draw calls are relatively ok; maximum 20 draw calls due to layering system we use.
Each sprite has 2 vertices; so there are 1458 vertices just for background. Although background is created dynamically it is static.
What would be the best approach to take a snapshot of the background image and use it as a single sprite? If we disable those sprites; FPS increases.
I told background is static; but actually it is moving; but it moves as a whole. It is semi-transparent and actually there is a "real static" background image.
Do you still have any information regarding this? I'm having a similar problem on my project and your link to documentation no longer works.
Answer by $$anonymous$$ · Jun 11, 2015 at 05:01 PM
We figured out hat this can be solved with http://docs.unity3d.com/Manual/class-RenderTexture.html.
However; with proper sorting; draw calls dramatically reduced to 2-3 calls; so we did not need id.
Edit: Later on we need it for some other performance enhancements :) here how the code looks like:
@SmashingSuccess here is the code:
public class Photographer : MonoBehaviour
{
public Camera MasterCamera;
public Camera SelfCamera;
public SpriteRenderer BackgroundDisplayer;
public LevelBuilder CurrentLevelBuilder;
public LayerMask Layer1;
public void TakeShot()
{
SelfCamera.transform.position = MasterCamera.transform.position;
SelfCamera.orthographicSize = MasterCamera.orthographicSize;
var w = SelfCamera.pixelWidth;
var h = SelfCamera.pixelHeight;
//Background
SelfCamera.cullingMask = Layer1;
var texture = new RenderTexture(w, h, 0, RenderTextureFormat.ARGB32);
SelfCamera.targetTexture = texture;
SelfCamera.Render();
RenderTexture.active = texture;
var textureFormat = TextureFormat.RGB24;
//https://issuetracker.unity3d.com/issues/error-regarding-invalid-textureformat-when-calling-supportstextureformat
#if UNITY_ANDROID
// if (SystemInfo.SupportsTextureFormat(TextureFormat.ETC_RGB4))
// {
// textureFormat = TextureFormat.ETC_RGB4;
// }
#elif UNIT_IOS
// if (SystemInfo.SupportsTextureFormat(TextureFormat.PVRTC_RGB4))
// {
// textureFormat = TextureFormat.PVRTC_RGB4;
// }
#endif
var virtualPhoto = new Texture2D(w, h, textureFormat, false);
virtualPhoto.name = "PhotoBackgroundTexture";
var r = new Rect(0, 0, w, h);
virtualPhoto.ReadPixels(r, 0, 0); // you get the center section
virtualPhoto.Apply();
var bg = Sprite.Create(virtualPhoto, r, new Vector2(0.5f, 0.5f), 100f);
bg.name = "PhotoBackgroundSprite";
RenderTexture.active = null;
BackgroundDisplayer.sprite = bg;
SelfCamera.targetTexture = null;
Destroy(texture);
var scaleNeeded = 100 * (MasterCamera.orthographicSize * 2) / h;
BackgroundDisplayer.transform.localScale = new Vector3(scaleNeeded, scaleNeeded, scaleNeeded);
SelfCamera.enabled = false;
BackgroundDisplayer.enabled = true;
MasterCamera.clearFlags = CameraClearFlags.SolidColor;
var all = GameObject.FindGameObjectsWithTag("ToBePhotographed");
for (var i = 0; i < all.Length; i++)
{
Destroy(all[i]);
}
Destroy(gameObject);
}
}
SelfCamera is responsible to take screenshot. It only renders "Layer1". All other cameras; renders everything except "Layer1". Items who are going to be photographed are tagged with "ToBePhotographed" and are configured to be rendered in "Layer1". After screenshot is taken; they are destroyed..
Your answer
![](https://koobas.hobune.stream/wayback/20220613154230im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do you change a sprite's sorting layer in C#? 6 Answers
How to change color property of the Sprite Renderer in C# 1 Answer
changing the scale of sprite changes its position center?,changing scale of 2d sprite 1 Answer
Sprite not showing in spriteRenderer 2 Answers
why blurry effect on moving sprites 1 Answer