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.
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*);*
} 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…