MixedRealityToolkit-Unity icon indicating copy to clipboard operation
MixedRealityToolkit-Unity copied to clipboard

CameraCache.Main Can't Assign Existing Cameras if It Finds Camera.main to be null

Open Holo-Fiedel opened this issue 5 years ago • 5 comments

Describe the bug

In the Microsoft.MixedReality.Toolkit.Utilities.CameraCache there is a loophole to fall through, if cachedCamera and Camera.main are both null.

The code reads (l. 36, ff.):

if (mainCamera == null)
{
    Debug.Log("No main camera found. Searching for cameras in the scene.");

    // If no main camera was found, try to determine one.
    Camera[] cameras = GameObject.FindObjectsOfType<Camera>();
    if (cameras.Length == 0)
    {
        Debug.LogWarning("No cameras found. Creating a \"MainCamera\".");
        mainCamera = new GameObject("Main Camera", typeof(Camera), typeof(AudioListener)) { tag = "MainCamera" }.GetComponent<Camera>();
    }
    else
    {
        Debug.LogWarning("The Mixed Reality Toolkit requires one camera in the scene to be tagged as \"MainCamera\". Please ensure the application's main camera is tagged.");
    }
}

So if Cameras are found, they are never assigned to the cameraCache.

To reproduce

Steps to reproduce the behavior:

  1. Create an app with no MRTK Profile assigned to the MRTK
  2. Assign an MRTK profile during runtime --> Observe that CameraCache.Main returns null --> Observe that it can never access the cameras found in the scene

Expected behavior

The MRTK can be assigned a profile and initialized AFTER preparing the scene

Your setup (please complete the following information)

  • Unity Version [e.g. 2019.4.21f1]
  • MRTK Version [e.g. v2.5.4]

Target platform (please complete the following information)

  • HoloLens
  • HoloLens 2
  • Unity Editor

Additional context

Add any other context about the problem here. I solved this by changing the CameraCache.cs script. But of course, I would rather not change scripts from the MRTK package

Holo-Fiedel avatar Mar 01 '21 12:03 Holo-Fiedel

The code path shown falls through if there are one or more cameras in the scene and none of them are tagged with MainCamera. MRTK cannot determine the correct one to choose.

Solution here would be to explicitly tag one of the cameras.

david-c-kline avatar Apr 07 '21 17:04 david-c-kline

For v3, we should investigate eliminating the CameraCache in favor of asking the CameraSystem for the "primary" camera (and allow secondary, etc)

david-c-kline avatar Apr 07 '21 17:04 david-c-kline

For v2.7 the code should replace the log warning with a log failure

david-c-kline avatar Apr 07 '21 17:04 david-c-kline

After discussing with the team, this functionality is by design. We can consider any potential improvements in a later release.

polar-kev avatar Apr 15 '21 18:04 polar-kev

This issue has been marked as stale by an automated process because it has not had any recent activity. It will be automatically closed in 30 days if no further activity occurs. If this is still an issue please add a new comment with more recent details and repro steps.

stale[bot] avatar Apr 18 '22 07:04 stale[bot]