Skip to content

Commit

Permalink
Fix NullReferenceException when opening PointerProfile without main c…
Browse files Browse the repository at this point in the history
…amera in scene (#10715)

* Add some CameraCache null checks

* Use CameraProfile
  • Loading branch information
keveleigh committed Jul 7, 2022
1 parent 55bab2c commit 6223850
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,22 @@ public override void OnInspectorGUI()

EditorGUILayout.Space();

var gazeProvider = CameraCache.Main.GetComponent<IMixedRealityGazeProvider>();
CreateCachedEditor((Object)gazeProvider, null, ref gazeProviderEditor);
showGazeProviderProperties = EditorGUILayout.Foldout(showGazeProviderProperties, "Gaze Provider Settings", true, boldFoldout);
if (showGazeProviderProperties && !gazeProviderEditor.IsNull())
if (showGazeProviderProperties && CameraCache.Main != null)
{
var gazeProvider = CameraCache.Main.GetComponent<IMixedRealityGazeProvider>();
CreateCachedEditor((Object)gazeProvider, null, ref gazeProviderEditor);

// Provide a convenient way to toggle the gaze provider as enabled/disabled via editor
gazeProvider.Enabled = EditorGUILayout.Toggle("Enable Gaze Provider", gazeProvider.Enabled);

using (new EditorGUI.IndentLevelScope())
if (gazeProviderEditor != null)
{
// Draw out the rest of the Gaze Provider's settings
gazeProviderEditor.OnInspectorGUI();
using (new EditorGUI.IndentLevelScope())
{
// Draw out the rest of the Gaze Provider's settings
gazeProviderEditor.OnInspectorGUI();
}
}
}
}
Expand Down
24 changes: 13 additions & 11 deletions Assets/MRTK/Core/Services/MixedRealityToolkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -564,31 +564,33 @@ private void InitializeServiceLocator()

private void EnsureEditorSetup()
{
if (ActiveProfile.RenderDepthBuffer)
if (ActiveProfile.RenderDepthBuffer && CameraCache.Main != null)
{
CameraCache.Main.gameObject.EnsureComponent<DepthBufferRenderer>();
DebugUtilities.LogVerbose("Added a DepthBufferRenderer to the main camera");
CameraCache.Main.EnsureComponent<DepthBufferRenderer>();
DebugUtilities.LogVerbose("Added a DepthBufferRenderer to the main camera.");
}
}

private void EnsureMixedRealityRequirements()
{
// There's lots of documented cases that if the camera doesn't start at 0,0,0, things break with the WMR SDK specifically.
// We'll enforce that here, then tracking can update it to the appropriate position later.
CameraCache.Main.transform.position = Vector3.zero;
CameraCache.Main.transform.rotation = Quaternion.identity;
if (CameraCache.Main != null)
{
CameraCache.Main.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
}

// This will create the playspace
_ = MixedRealityPlayspace.Transform;

bool addedComponents = false;
if (!Application.isPlaying)
if (!Application.isPlaying && CameraCache.Main != null)
{
var eventSystems = FindObjectsOfType<EventSystem>();
EventSystem[] eventSystems = FindObjectsOfType<EventSystem>();

if (eventSystems.Length == 0)
{
CameraCache.Main.gameObject.EnsureComponent<EventSystem>();
CameraCache.Main.EnsureComponent<EventSystem>();
addedComponents = true;
}
else
Expand All @@ -611,10 +613,10 @@ private void EnsureMixedRealityRequirements()
}
}

if (!addedComponents)
if (!addedComponents && CameraCache.Main != null)
{
CameraCache.Main.gameObject.EnsureComponent<EventSystem>();
DebugUtilities.LogVerbose("Added an EventSystem to the main camera");
CameraCache.Main.EnsureComponent<EventSystem>();
DebugUtilities.LogVerbose("Added an EventSystem to the main camera.");
}
}

Expand Down
5 changes: 3 additions & 2 deletions Assets/MRTK/Services/CameraSystem/MixedRealityCameraSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,14 @@ public override void Initialize()
{
base.Initialize();

MixedRealityCameraProfile profile = ConfigurationProfile as MixedRealityCameraProfile;
MixedRealityCameraProfile profile = CameraProfile;
var cameraSettingProviders = GetDataProviders<IMixedRealityCameraSettingsProvider>();

if ((cameraSettingProviders.Count == 0) && (profile != null))
{
int settingsConfigurationsLength = profile.SettingsConfigurations.Length;
// Register the settings providers.
for (int i = 0; i < profile.SettingsConfigurations.Length; i++)
for (int i = 0; i < settingsConfigurationsLength; i++)
{
MixedRealityCameraSettingsConfiguration configuration = profile.SettingsConfigurations[i];

Expand Down
3 changes: 1 addition & 2 deletions Assets/MRTK/Services/InputSystem/MixedRealityInputSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private void CreateDataProviders()

private void InstantiateGazeProvider(MixedRealityPointerProfile pointerProfile)
{
if (pointerProfile != null && pointerProfile.GazeProviderType?.Type != null)
if (pointerProfile != null && pointerProfile.GazeProviderType?.Type != null && CameraCache.Main != null)
{
IMixedRealityGazeProvider existingGazeProvider = CameraCache.Main.gameObject.GetComponent<IMixedRealityGazeProvider>();
if (existingGazeProvider != null && existingGazeProvider.GetType() != pointerProfile.GazeProviderType.Type)
Expand All @@ -336,7 +336,6 @@ private void InstantiateGazeProvider(MixedRealityPointerProfile pointerProfile)
else
{
Debug.LogError("The input system is missing the required GazeProviderType!");
return;
}
}

Expand Down

0 comments on commit 6223850

Please sign in to comment.