CameraCache.Main Can't Assign Existing Cameras if It Finds Camera.main to be null
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:
- Create an app with no MRTK Profile assigned to the MRTK
- 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
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.
For v3, we should investigate eliminating the CameraCache in favor of asking the CameraSystem for the "primary" camera (and allow secondary, etc)
For v2.7 the code should replace the log warning with a log failure
After discussing with the team, this functionality is by design. We can consider any potential improvements in a later release.
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.