Search Unity

Changing Camera.Rect in coroutine causes huge fps drops

Discussion in 'Editor & General Support' started by unity_MLt06q2IO0O7Uw, Sep 19, 2021.

  1. unity_MLt06q2IO0O7Uw

    unity_MLt06q2IO0O7Uw

    Joined:
    Mar 24, 2021
    Posts:
    1
    Hi, so I have two camera displaying to the screen. One camera shows the 3D player and the other shows a 3D board.
    When the player triggers something I want one camera to expand the cover the whole screen and the other camera to move off screen. I have this code that works but it seems that quickly changing the Camera.Rect each frame cause a huge fps drop (150 down to 1-3fps).
    I tried doing this in update but had the same fps drops.

    How can I smoothly expand the camera across the whole screen without causing performance issues?

    Code (CSharp):
    1. public Camera boardCamera;
    2.     public Camera worldCamera;
    3.     public float transitionTime = 2f;
    4.  
    5.     Vector4 boardCameraStart;
    6.     Vector4 worldCameraStart;
    7.  
    8.  
    9.     void Awake()
    10.     {
    11.         boardCamera = boardCamera.GetComponent<Camera>();
    12.         worldCamera = worldCamera.GetComponent<Camera>();
    13.     }
    14.  
    15.     public void FullScreenBoardCall()
    16.     {
    17.         GetCurrentCameraRects();
    18.         StartCoroutine(ChangeCameras(new Vector4(0f, 0f, 1f, 1f), new Vector4(1f, 0f, 0.5f, 1f)));
    19.     }
    20.  
    21.     public IEnumerator ChangeCameras(Vector4 boardCameraEnd, Vector4 worldCameraEnd)
    22.     {
    23.         float startTime = Time.time;
    24.  
    25.         Vector4 boardCameraStart = new Vector4(boardCamera.rect.x, boardCamera.rect.y, boardCamera.rect.width, boardCamera.rect.height);
    26.         Vector4 worldCameraStart = new Vector4(worldCamera.rect.x, worldCamera.rect.y, worldCamera.rect.width, worldCamera.rect.height);
    27.  
    28.         while(Time.time < startTime + transitionTime)
    29.         {
    30.             Vector4 worldRect = Vector4.Lerp(worldCameraStart, worldCameraEnd, (Time.time - startTime)/transitionTime);
    31.             Vector4 boardRect = Vector4.Lerp(boardCameraStart, boardCameraEnd, (Time.time - startTime)/transitionTime);
    32.             worldCamera.rect = new Rect(worldRect.x, worldRect.y, worldRect.z, worldRect.w);
    33.             boardCamera.rect = new Rect(boardRect.x, boardRect.y, boardRect.z, boardRect.w);
    34.             yield return null;
    35.         }
    36.         worldCamera.rect = new Rect(worldCameraEnd.x, worldCameraEnd.y, worldCameraEnd.z, worldCameraEnd.w);
    37.         boardCamera.rect = new Rect(boardCameraEnd.x, boardCameraEnd.y, boardCameraEnd.z, boardCameraEnd.w);
    38.  
    39.     }
    40.  
    41.     void GetCurrentCameraRects()
    42.     {
    43.         boardCameraStart = new Vector4(boardCamera.rect.x, boardCamera.rect.y, boardCamera.rect.width, boardCamera.rect.height);
    44.         worldCameraStart = new Vector4(worldCamera.rect.x, worldCamera.rect.y, worldCamera.rect.width, worldCamera.rect.height);
    45.     }