Skip to content

Commit

Permalink
fix(Utilities): prevent sdk change event unregistering on disable
Browse files Browse the repository at this point in the history
A number of scripts that utilise the `LoadedSetupChanged` event on
the SDKManager were set up so that if they were on a GameObject that
was a child of the current SDK camera rig then when the SDK was
changed, the camera rig would become disabled and the script listening
to the setup change event would then also have the `OnDisable` method
called and unregister the listener. This would mean that the script
would no longer be listening for the switch in SDK and any
functionality required on SDK switch would no longer be available.

For instance, the SDK Object Alias script would not re-child the
relevant object because the SDK change would not be observed as the
listener had been unregistered.

This is fixed by checking in the `OnDisable` method of the script
to see if the actual GameObject the script is on is disabled and
to only unregister the event if it is, therefore ignoring any
parent GameObject enabled state.
  • Loading branch information
thestonefox committed Jun 19, 2017
1 parent 230f0ec commit ee141c0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
39 changes: 26 additions & 13 deletions Assets/VRTK/Prefabs/Resources/Scripts/VRTK_FramesPerSecondViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,24 @@ public class VRTK_FramesPerSecondViewer : MonoBehaviour
protected float framesTime;
protected Canvas canvas;
protected Text text;
protected VRTK_SDKManager sdkManager;

protected virtual void OnEnable()
{
VRTK_SDKManager sdkManager = VRTK_SDKManager.instance;
sdkManager = VRTK_SDKManager.instance;
if (sdkManager != null)
{
sdkManager.LoadedSetupChanged += LoadedSetupChanged;
}
InitCanvas();
}

canvas = transform.GetComponentInParent<Canvas>();
text = GetComponent<Text>();

if (canvas != null)
{
canvas.planeDistance = 0.5f;
}

if (text != null)
protected virtual void OnDisable()
{
if (sdkManager != null && !gameObject.activeSelf)
{
text.fontSize = fontSize;
text.transform.localPosition = position;
sdkManager.LoadedSetupChanged -= LoadedSetupChanged;
}
SetCanvasCamera();
}

protected virtual void Update()
Expand Down Expand Up @@ -96,6 +91,24 @@ protected virtual void LoadedSetupChanged(VRTK_SDKManager sender, VRTK_SDKManage
SetCanvasCamera();
}

protected virtual void InitCanvas()
{
canvas = transform.GetComponentInParent<Canvas>();
text = GetComponent<Text>();

if (canvas != null)
{
canvas.planeDistance = 0.5f;
}

if (text != null)
{
text.fontSize = fontSize;
text.transform.localPosition = position;
}
SetCanvasCamera();
}

protected virtual void SetCanvasCamera()
{
Transform sdkCamera = VRTK_DeviceFinder.HeadsetCamera();
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRTK/Scripts/Utilities/VRTK_SDKObjectAlias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected virtual void OnEnable()

protected virtual void OnDisable()
{
if (sdkManager != null)
if (sdkManager != null && !gameObject.activeSelf)
{
sdkManager.LoadedSetupChanged -= LoadedSetupChanged;
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRTK/Scripts/Utilities/VRTK_SDKTransformModify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected virtual void OnEnable()

protected virtual void OnDisable()
{
if (sdkManager != null)
if (sdkManager != null && !gameObject.activeSelf)
{
sdkManager.LoadedSetupChanged -= LoadedSetupChanged;
}
Expand Down

0 comments on commit ee141c0

Please sign in to comment.