Search Unity

Faster than camera.main ?

Discussion in 'Cinemachine' started by Kev00, Mar 5, 2020.

  1. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    I was looking for an alternative to camera.main / camera.current and since I'm using cinemachine throughout I thought I would write this function.

    Code (CSharp):
    1.   public static CinemachineBrain GetActiveBrain()
    2.         {
    3.             CinemachineBrain brain = null;
    4.             if (CinemachineCore.Instance != null)
    5.             {
    6.                 if(CinemachineCore.Instance.BrainCount > 0)
    7.                 {
    8.                     brain = CinemachineCore.Instance.GetActiveBrain(0);
    9.                 }
    10.             }
    11.             return brain;
    12.         }
    13.  
    Is this faster? recommended?
     
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Your question is a bit vague. :( Could you clarify?

    Is it faster than what?

    Why do want to have this function?
     
  3. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
  4. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    I still don't understand your question.

    Is getting an active brain faster than getting camera.main?
     
  5. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    sorry I suppose I should repeat the title question in the body of the post.

    So yes, is using GetActiveBrain faster than camera.main?

    As for the use case, I have several scenes in my game with their own camera brains that get loaded and unloaded all the time. As a result, I wanted to quick way to get the active camera. Caching camera.main on awake() doesn't always work because the camera brain changes during play.

    I'm also using it to issue blending started and blending ended custom events by constantly polling brain.IsBlending
     
    Last edited: Mar 5, 2020
  6. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    In my game I have an extensive need to know when camera blending has started and ended. To this end, I created the following script.

    Code (CSharp):
    1. public class CameraBrainBlendingEvents : MonoBehaviour
    2.     {
    3.         private bool BlendingStarted = false;
    4.      
    5.         public void Update()
    6.         {
    7.             CinemachineVirtualCameraBase activeCamera = CinemachineHelpers.GetActiveVCam();
    8.  
    9.             if (activeCamera == null) return;
    10.  
    11.             CinemachineBrain brain = CinemachineHelpers.GetActiveBrain();
    12.    
    13.             if (brain != null && BlendingStarted == false && brain.IsBlending)  // detect if blending has started
    14.             {
    15.                 BlendingStarted = true;
    16.          
    17.                 EventCommand.Camera.CameraBlendingStarted(activeCamera);  // my custom event
    18.             }
    19.             else if (brain != null && BlendingStarted == true && !brain.IsBlending)  // detect if blending has ended
    20.             {
    21.                 BlendingStarted = false;
    22.            
    23.                 EventCommand.Camera.CameraBlendingEnded(activeCamera); // my custom event
    24.             }
    25.         }
    26.      
    27.     }
    Would it be possible to add something like this to Cinemachine?
     
    gaborkb likes this.
  7. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Your GetActiveBrain function is about 2-3 times faster than getting camera.main (in my tests).

    Probably yes, I'll add it as a feature request ;)
     
  8. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    ok well that's fantastic. I just wanted to make sure there wasn't some unknown gotcha hiding inside CinemachineCore.Instance.GetActiveBrain(0)
     
    gaborkb likes this.