Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NullReferenceException when opening PointerProfile without main camera in scene #10715

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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