Search Unity

Memory leak and/or high memory usage caused by a script that billboards an object to the camera

Discussion in 'Editor & General Support' started by mariosgakuto, Feb 13, 2020.

  1. mariosgakuto

    mariosgakuto

    Joined:
    Oct 18, 2015
    Posts:
    14
    Hey all,

    I have been having a serious problem that only recently was able to identify. I am using a LookAtCamera script to make my objects always look at the camera. In a specific part of my scene this script is running for around 6 elements at the same time (one 3D model and 5 world space UI canvases). Every time I enter that part I observe that the memory usage by Unity is rising by approximately 0.02 GB per second. Playing that part has exploded my memory usage to almost 8gb, and that particular memory does not get released.

    I am using this script on both 3D models and WorldSpaceUI. Anyone can identify what could the problem be?


    Code (CSharp):
    1. public class LookAtCamera : MonoBehaviour
    2.     {
    3.         public Camera m_Camera;
    4.         private Canvas myCanvas;
    5.         private Transform cameraTransform, myTransform;
    6.      
    7.         [SerializeField] private bool inverseForward = false;
    8.      
    9.         [Tooltip("Only billboard on the Y Axis. used for speech bubbles etc")]
    10.         [SerializeField] private bool yAxisBillboard = false;
    11.  
    12.         [Tooltip("Only billboard on the Y and X axis. Used for labels etc")]
    13.         [SerializeField] private bool xAndYAxisBillboard;
    14.      
    15.  
    16.         private void Awake()
    17.         {
    18.             if (m_Camera == null)
    19.             {
    20.                 m_Camera = Camera.main;
    21.             }
    22.  
    23.             if (myCanvas = GetComponent<Canvas>())
    24.             {
    25.                 myCanvas.worldCamera = m_Camera;
    26.             }
    27.  
    28.             cameraTransform = m_Camera.transform;
    29.             myTransform = transform;
    30.         }
    31.  
    32.         //Orient the camera after all movement is completed this frame to avoid jittering
    33.         void LateUpdate()
    34.         {
    35.             var rotation = cameraTransform.rotation;
    36.             var forward = inverseForward ? -Vector3.forward : Vector3.forward;
    37.          
    38.             myTransform.LookAt(myTransform.position + rotation * forward,
    39.                 rotation * Vector3.up);
    40.  
    41.             if (yAxisBillboard)
    42.             {
    43.                 Vector3 eulerAngles = myTransform.eulerAngles;
    44.                 eulerAngles.x = 0;
    45.                 eulerAngles.z = 0;
    46.                 myTransform.eulerAngles = eulerAngles;
    47.             }
    48.             else if (xAndYAxisBillboard)
    49.             {
    50.                 Vector3 eulerAngles = myTransform.eulerAngles;
    51.                 eulerAngles.z = 0;
    52.                 myTransform.eulerAngles = eulerAngles;
    53.             }
    54.         }
    55.     }
    Any help appreciated.

    EDIT: surprisingly if I remove the 2 if statements at the end of LateUpdate I can see in the Profiler that the Total System Memory Usage rapidly started dropping back to more normal levels, and it stopped rapidly raising.
     
    Last edited: Feb 13, 2020