Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Fixed canvas utility removing itself (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson authored Sep 16, 2019
1 parent f129dcf commit 3b02262
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using XRTK.Extensions;
using UnityEditor;
using UnityEngine;
using XRTK.Extensions;
using XRTK.Interfaces.InputSystem;
using XRTK.Services;
using XRTK.Utilities;
Expand All @@ -24,30 +24,36 @@ public class CanvasEditorExtension : Editor

private bool hasUtility = false;

private static bool IsUtilityValid
private static bool IsUtilityValid =>
MixedRealityToolkit.Instance != null &&
MixedRealityToolkit.HasActiveProfile &&
MixedRealityToolkit.Instance.ActiveProfile.IsInputSystemEnabled &&
MixedRealityToolkit.GetService<IMixedRealityInputSystem>(false) != null;

private bool CanUpdateSettings
{
get
{
var isInputSystemValid = MixedRealityToolkit.GetService<IMixedRealityInputSystem>(false) != null;
return MixedRealityToolkit.Instance != null &&
MixedRealityToolkit.HasActiveProfile &&
MixedRealityToolkit.Instance.ActiveProfile.IsInputSystemEnabled &&
isInputSystemValid;
if (!MixedRealityToolkit.IsInitialized ||
!MixedRealityPreferences.ShowCanvasUtilityPrompt)
{
return false;
}

var utility = canvas.GetComponent<CanvasUtility>();

hasUtility = utility != null;

return hasUtility && !IsUtilityValid ||
!hasUtility && IsUtilityValid;
}
}

private void OnEnable()
{
if (!MixedRealityToolkit.IsInitialized || !MixedRealityPreferences.ShowCanvasUtilityPrompt) { return; }

canvas = (Canvas)target;

var utility = canvas.GetComponent<CanvasUtility>();

hasUtility = utility != null;

if (hasUtility && !IsUtilityValid ||
!hasUtility && IsUtilityValid)
if (CanUpdateSettings)
{
UpdateCanvasSettings();
}
Expand All @@ -58,9 +64,7 @@ public override void OnInspectorGUI()
EditorGUI.BeginChangeCheck();
base.OnInspectorGUI();

if (EditorGUI.EndChangeCheck() &&
MixedRealityToolkit.IsInitialized &&
MixedRealityPreferences.ShowCanvasUtilityPrompt)
if (EditorGUI.EndChangeCheck() && CanUpdateSettings)
{
UpdateCanvasSettings();
}
Expand Down Expand Up @@ -110,7 +114,6 @@ private void UpdateCanvasSettings()
{
// Sets it back to MainCamera default.
canvas.worldCamera = null;
removeUtility = true;
}

var utility = canvas.GetComponent<CanvasUtility>();
Expand Down
5 changes: 3 additions & 2 deletions XRTK-Core/Packages/com.xrtk.core/Utilities/CanvasUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ private void Start()
{
Debug.Assert(Canvas != null);

if (MixedRealityToolkit.Instance.ActiveProfile.IsInputSystemEnabled &&
if (MixedRealityToolkit.IsInitialized &&
MixedRealityToolkit.Instance.ActiveProfile.IsInputSystemEnabled &&
Canvas.isRootCanvas && Canvas.renderMode == RenderMode.WorldSpace)
{
Canvas.worldCamera = MixedRealityToolkit.InputSystem?.FocusProvider?.UIRaycastCamera;
Canvas.worldCamera = MixedRealityToolkit.InputSystem.FocusProvider.UIRaycastCamera;
}
}
}
Expand Down

0 comments on commit 3b02262

Please sign in to comment.