-
-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Utilities): ensure controller ready event happens on sdk switch
The Controller Ready event wasn't being emitted correctly due to the SDK switching happening when the scene first starts. This meant that the SDK Transform Modify and SDK Object State script were not working on SDK switch correctly. The solution is to ensure the Controller Ready event is always unregistered and re-registered when then SDK setup changes. This now happens in a new base script called SDK Controller Ready and the relevant scripts that need to deal with the controller being ready can now just extend that script and have access to the controller ready event even when the SDK setup switches. The Device Finder has also had a few more methods that make it easy to find the Controller Reference of the left or right hand.
- Loading branch information
1 parent
5021a12
commit 60111b7
Showing
6 changed files
with
243 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
Assets/VRTK/Source/Scripts/Internal/VRTK_SDKControllerReady.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
namespace VRTK | ||
{ | ||
using UnityEngine; | ||
|
||
public abstract class VRTK_SDKControllerReady : MonoBehaviour | ||
{ | ||
protected VRTK_SDKManager sdkManager; | ||
protected SDK_BaseController previousControllerSDK; | ||
|
||
protected virtual void OnEnable() | ||
{ | ||
sdkManager = VRTK_SDKManager.instance; | ||
if (sdkManager != null) | ||
{ | ||
sdkManager.LoadedSetupChanged += LoadedSetupChanged; | ||
} | ||
CheckControllersReady(); | ||
} | ||
|
||
protected virtual void OnDisable() | ||
{ | ||
if (sdkManager != null) | ||
{ | ||
sdkManager.LoadedSetupChanged -= LoadedSetupChanged; | ||
UnregisterPreviousLeftController(); | ||
UnregisterPreviousRightController(); | ||
} | ||
} | ||
|
||
protected virtual void LoadedSetupChanged(VRTK_SDKManager sender, VRTK_SDKManager.LoadedSetupChangeEventArgs e) | ||
{ | ||
RegisterLeftControllerReady(); | ||
RegisterRightControllerReady(); | ||
CheckControllersReady(); | ||
previousControllerSDK = VRTK_SDK_Bridge.GetControllerSDK(); | ||
} | ||
|
||
protected virtual void CheckControllersReady() | ||
{ | ||
VRTK_ControllerReference leftRef = VRTK_DeviceFinder.GetControllerReferenceLeftHand(); | ||
VRTK_ControllerReference rightRef = VRTK_DeviceFinder.GetControllerReferenceRightHand(); | ||
|
||
if (VRTK_ControllerReference.IsValid(leftRef)) | ||
{ | ||
RegisterLeftControllerReady(); | ||
ControllerReady(leftRef); | ||
} | ||
|
||
if (VRTK_ControllerReference.IsValid(rightRef)) | ||
{ | ||
RegisterRightControllerReady(); | ||
ControllerReady(rightRef); | ||
} | ||
} | ||
|
||
protected virtual void UnregisterPreviousLeftController() | ||
{ | ||
try | ||
{ | ||
previousControllerSDK.LeftControllerReady -= LeftControllerReady; | ||
} | ||
catch (System.Exception) | ||
{ | ||
} | ||
} | ||
|
||
protected virtual void UnregisterPreviousRightController() | ||
{ | ||
try | ||
{ | ||
previousControllerSDK.RightControllerReady -= RightControllerReady; | ||
} | ||
catch (System.Exception) | ||
{ | ||
} | ||
} | ||
|
||
protected virtual void RegisterLeftControllerReady() | ||
{ | ||
UnregisterPreviousLeftController(); | ||
try | ||
{ | ||
VRTK_SDK_Bridge.GetControllerSDK().LeftControllerReady -= LeftControllerReady; | ||
VRTK_SDK_Bridge.GetControllerSDK().LeftControllerReady += LeftControllerReady; | ||
} | ||
catch (System.Exception) | ||
{ | ||
VRTK_SDK_Bridge.GetControllerSDK().LeftControllerReady += LeftControllerReady; | ||
} | ||
} | ||
|
||
protected virtual void RegisterRightControllerReady() | ||
{ | ||
UnregisterPreviousRightController(); | ||
|
||
try | ||
{ | ||
VRTK_SDK_Bridge.GetControllerSDK().RightControllerReady -= RightControllerReady; | ||
VRTK_SDK_Bridge.GetControllerSDK().RightControllerReady += RightControllerReady; | ||
} | ||
catch (System.Exception) | ||
{ | ||
VRTK_SDK_Bridge.GetControllerSDK().RightControllerReady += RightControllerReady; | ||
} | ||
} | ||
|
||
protected virtual void RightControllerReady(object sender, VRTKSDKBaseControllerEventArgs e) | ||
{ | ||
ControllerReady(e.controllerReference); | ||
} | ||
|
||
protected virtual void LeftControllerReady(object sender, VRTKSDKBaseControllerEventArgs e) | ||
{ | ||
ControllerReady(e.controllerReference); | ||
} | ||
|
||
protected virtual void ControllerReady(VRTK_ControllerReference controllerReference) | ||
{ | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Assets/VRTK/Source/Scripts/Internal/VRTK_SDKControllerReady.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.