Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question 2D Game Map Editor - FPS Reduction the Longer the Game Runs

Discussion in '2D' started by negodfre, Aug 29, 2021.

  1. negodfre

    negodfre

    Joined:
    Mar 5, 2016
    Posts:
    3
    I have created a 'Custom Editor' that is used to programmatically create 'maps' that are stored as JSON files. Part of the 'map' creation process is utilizing the camera to store a snapshot. Additionally, many GameObjects are created & destroyed.

    I will attempt to make 50 'maps' (create objects, move objects, take pictures of objects, delete objects, & repeat). However, as the program continues to run the FPS continues to decrease. It starts off with an FPS ~ 200, but then drops to 2-3 FPS at about 10-20 maps in.
    Additionally, the "batches" in the stats starts to increase as well by double.

    Another odd thing I have noticed is that if I go to the "Scene" tab & select any 2D sprite, go back to "Game" tab, and look at the FPS has jumped back up to ~ 200.

    I noticed during profiling that it appears to be partially related to the "ScreenShotHandler.OnPostRender()" function (see image attached).
    Note: Images are posted as reply (forums keep saying this is spam).

    Is there anything wrong with the ScreenShotHandler() that could explain the drop in FPS (see Script below)?
     
    Last edited: Aug 29, 2021
  2. negodfre

    negodfre

    Joined:
    Mar 5, 2016
    Posts:
    3
    Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ScreenShotHandler : MonoBehaviour
    6. {
    7.  
    8.     private Camera myCamera;
    9.     private static ScreenShotHandler instance;
    10.     private bool takeScreenShotOnNextFrame;
    11.     private int width, height;
    12.     public float occlusion;
    13.  
    14.     private void Awake()
    15.     {
    16.         myCamera = gameObject.GetComponent<Camera>();
    17.         instance = this;
    18.         width = 500;
    19.         height = 500;
    20.     }
    21.  
    22.     private void OnPostRender()
    23.     {
    24.         if(takeScreenShotOnNextFrame)
    25.         {
    26.             takeScreenShotOnNextFrame = false;
    27.             RenderTexture renderTexture = myCamera.targetTexture;
    28.  
    29.             Texture2D renderResult = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false);
    30.  
    31.             // 8-29-2021 -- Will this prevent memory leak / fps drop?
    32.             // https://forum.unity.com/threads/texture2d-destroy-how-to-do-it-right.385249/
    33.             renderResult.hideFlags = HideFlags.HideAndDontSave;
    34.             Rect rect = new Rect(0, 0, renderTexture.width, renderTexture.height);
    35.             renderResult.ReadPixels(rect, 0, 0);
    36.  
    37.             float myValue = 0;
    38.             float totalPixels = renderResult.width * renderResult.height;
    39.  
    40.             // Sum color values
    41.             for (int i = 0; i < renderResult.width; i++)
    42.             {
    43.                 for (int j = 0; j < renderResult.height; j++)
    44.                 {
    45.                     Color myColor = renderResult.GetPixel(i, j);
    46.                     myValue += myColor.r;
    47.                     //Debug.Log("Pixel (" + i + "," + j + "): " + myColor.r);
    48.                 }
    49.             }
    50.  
    51.             occlusion = ((myValue / totalPixels) * 100);
    52.  
    53.  
    54.             // Cleanup
    55.             RenderTexture.ReleaseTemporary(renderTexture);
    56.             RenderTexture.ReleaseTemporary(myCamera.targetTexture);
    57.             Destroy(renderResult);
    58.             myCamera.targetTexture = null;
    59.             renderResult = null;
    60.         }
    61.     }
    62.  
    63.     private void TakeScreenshot(int screenWidth, int screenHeight)
    64.     {
    65.         width = screenWidth;
    66.         height = screenHeight;
    67.  
    68.         if(myCamera.targetTexture != null)
    69.         {
    70.             RenderTexture.ReleaseTemporary(myCamera.targetTexture);
    71.         }
    72.  
    73.         myCamera.targetTexture = RenderTexture.GetTemporary(width, height, 16);
    74.         takeScreenShotOnNextFrame = true;
    75.     }
    76.    
    77.     public static void TakeScreenshot_Static(int screenWidth, int screenHeight)
    78.     {
    79.         instance.TakeScreenshot(screenWidth, screenHeight);
    80.     }
    81. }
    82.  
     

    Attached Files: