Skip to content

Commit b352322

Browse files
committed
feat(Utilities): ability to change object state based on loaded sdk
The new SDK Object State script allows the state of a GameObject or Component to be set to enabled/active or disabled based on the SDK information. This can be useful for toggling certain settings for different SDK types, headsets or connected controllers.
1 parent 5d73db2 commit b352322

File tree

5 files changed

+221
-1
lines changed

5 files changed

+221
-1
lines changed

Assets/VRTK/Documentation/API.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6530,6 +6530,7 @@ A collection of scripts that provide useful functionality to aid the creation pr
65306530
* [Transform Follow](#transform-follow-vrtk_transformfollow)
65316531
* [SDK Object Alias](#sdk-object-alias-vrtk_sdkobjectalias)
65326532
* [SDK Transform Modify](#sdk-transform-modify-vrtk_sdktransformmodify)
6533+
* [SDK Object State](#sdk-object-state-vrtk_sdkobjectstate)
65336534
* [Velocity Estimator](#velocity-estimator-vrtk_velocityestimator)
65346535

65356536
---
@@ -7768,6 +7769,40 @@ The UpdateTransform method updates the Transform data on the current GameObject
77687769

77697770
---
77707771

7772+
## SDK Object State (VRTK_SDKObjectState)
7773+
7774+
### Overview
7775+
7776+
The SDK Object State script can be used to set the enable/active state of a GameObject or Component based on SDK information.
7777+
7778+
The state can be determined by:
7779+
* The current loaded SDK setup.
7780+
* The current attached Headset type.
7781+
* The current attached Controller type.
7782+
7783+
### Inspector Parameters
7784+
7785+
* **Target:** The GameObject or Component that is the target of the enable/disable action. If this is left blank then the GameObject that the script is attached to will be used as the `Target`.
7786+
* **Object State:** The state to set the `Target` to when this script is enabled. Checking this box will enable/activate the `Target`, unchecking will disable/deactivate the `Target`.
7787+
* **Loaded SDK Setup:** If the currently loaded SDK Setup matches the one provided here then the `Target` state will be set to the desired `Object State`.
7788+
* **Headset Type:** If the attached headset type matches the selected headset then the `Target` state will be set to the desired `Object State`.
7789+
* **Controller Type:** If the current controller type matches the selected controller type then the `Target` state will be set to the desired `Object State`.
7790+
7791+
### Class Methods
7792+
7793+
#### SetStateByControllerReference/1
7794+
7795+
> `public virtual void SetStateByControllerReference(VRTK_ControllerReference controllerReference)`
7796+
7797+
* Parameters
7798+
* `VRTK_ControllerReference controllerReference` - A controller reference to check for the controller type of.
7799+
* Returns
7800+
* _none_
7801+
7802+
The SetStateByControllerReference method sets the object state based on the controller type of the given controller reference.
7803+
7804+
---
7805+
77717806
## Velocity Estimator (VRTK_VelocityEstimator)
77727807

77737808
### Overview

Assets/VRTK/Source/Scripts/Internal/VRTK_TrackedController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ protected virtual void OnEnable()
101101
protected virtual void OnDisable()
102102
{
103103
CancelCoroutines();
104+
index = uint.MaxValue;
104105
ManageControllerModelListeners(false);
105106
OnControllerDisabled(SetEventPayload());
106107
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
// SDK Object State|Utilities|90160
2+
namespace VRTK
3+
{
4+
using UnityEngine;
5+
using System.Collections;
6+
using System.Reflection;
7+
8+
/// <summary>
9+
/// The SDK Object State script can be used to set the enable/active state of a GameObject or Component based on SDK information.
10+
/// </summary>
11+
/// <remarks>
12+
/// The state can be determined by:
13+
/// * The current loaded SDK setup.
14+
/// * The current attached Headset type.
15+
/// * The current attached Controller type.
16+
/// </remarks>
17+
[AddComponentMenu("VRTK/Scripts/Utilities/VRTK_SDKObjectState")]
18+
public class VRTK_SDKObjectState : MonoBehaviour
19+
{
20+
[Header("Target Settings")]
21+
22+
[Tooltip("The GameObject or Component that is the target of the enable/disable action. If this is left blank then the GameObject that the script is attached to will be used as the `Target`.")]
23+
public Object target = null;
24+
[Tooltip("The state to set the `Target` to when this script is enabled. Checking this box will enable/activate the `Target`, unchecking will disable/deactivate the `Target`.")]
25+
public bool objectState = false;
26+
[Tooltip("If the currently loaded SDK Setup matches the one provided here then the `Target` state will be set to the desired `Object State`.")]
27+
public VRTK_SDKSetup loadedSDKSetup = null;
28+
[Tooltip("If the attached headset type matches the selected headset then the `Target` state will be set to the desired `Object State`.")]
29+
public VRTK_DeviceFinder.Headsets headsetType = VRTK_DeviceFinder.Headsets.Unknown;
30+
[Tooltip("If the current controller type matches the selected controller type then the `Target` state will be set to the desired `Object State`.")]
31+
public SDK_BaseController.ControllerType controllerType = SDK_BaseController.ControllerType.Undefined;
32+
33+
protected VRTK_SDKManager sdkManager;
34+
protected Coroutine checkToggleRoutine;
35+
//TODO: REPLACE WITH GENERIC CONTROLLER TYPE AVAILABLE EVENT
36+
protected int findControllerAttempts = 100;
37+
protected float findControllerAttemptsDelay = 0.1f;
38+
protected Coroutine attemptFindControllerModel;
39+
40+
/// <summary>
41+
/// The SetStateByControllerReference method sets the object state based on the controller type of the given controller reference.
42+
/// </summary>
43+
/// <param name="controllerReference">A controller reference to check for the controller type of.</param>
44+
public virtual void SetStateByControllerReference(VRTK_ControllerReference controllerReference)
45+
{
46+
if (VRTK_ControllerReference.IsValid(controllerReference))
47+
{
48+
SDK_BaseController.ControllerType foundControllerType = VRTK_DeviceFinder.GetCurrentControllerType(controllerReference);
49+
if (foundControllerType != SDK_BaseController.ControllerType.Undefined && controllerType == foundControllerType)
50+
{
51+
ToggleObject();
52+
}
53+
}
54+
}
55+
56+
protected virtual void OnEnable()
57+
{
58+
sdkManager = VRTK_SDKManager.instance;
59+
target = (target != null ? target : gameObject);
60+
sdkManager.LoadedSetupChanged += LoadedSetupChanged;
61+
checkToggleRoutine = StartCoroutine(CheckToggleAtEndOfFrame());
62+
}
63+
64+
protected virtual void OnDisable()
65+
{
66+
sdkManager.LoadedSetupChanged -= LoadedSetupChanged;
67+
if (checkToggleRoutine != null)
68+
{
69+
StopCoroutine(checkToggleRoutine);
70+
}
71+
if (attemptFindControllerModel != null)
72+
{
73+
StopCoroutine(attemptFindControllerModel);
74+
}
75+
}
76+
77+
protected virtual IEnumerator CheckToggleAtEndOfFrame()
78+
{
79+
yield return new WaitForEndOfFrame();
80+
CheckToggle();
81+
}
82+
83+
protected virtual void LoadedSetupChanged(VRTK_SDKManager sender, VRTK_SDKManager.LoadedSetupChangeEventArgs e)
84+
{
85+
CheckToggle();
86+
}
87+
88+
protected virtual void CheckToggle()
89+
{
90+
ToggleOnSDK();
91+
ToggleOnHeadset();
92+
ToggleOnController();
93+
}
94+
95+
protected virtual void ToggleOnSDK()
96+
{
97+
if (loadedSDKSetup != null && loadedSDKSetup == sdkManager.loadedSetup)
98+
{
99+
ToggleObject();
100+
}
101+
}
102+
103+
protected virtual void ToggleOnHeadset()
104+
{
105+
if (headsetType != VRTK_DeviceFinder.Headsets.Unknown && headsetType == VRTK_DeviceFinder.GetHeadsetType(true))
106+
{
107+
ToggleObject();
108+
}
109+
}
110+
111+
protected virtual void ToggleOnController()
112+
{
113+
if (controllerType != SDK_BaseController.ControllerType.Undefined)
114+
{
115+
attemptFindControllerModel = StartCoroutine(AttemptFindController(findControllerAttempts, findControllerAttemptsDelay));
116+
}
117+
}
118+
119+
protected virtual IEnumerator AttemptFindController(int attempts, float delay)
120+
{
121+
WaitForSeconds delayInstruction = new WaitForSeconds(delay);
122+
SDK_BaseController.ControllerType foundControllerType = VRTK_DeviceFinder.GetCurrentControllerType();
123+
124+
while (foundControllerType == SDK_BaseController.ControllerType.Undefined && attempts > 0)
125+
{
126+
foundControllerType = VRTK_DeviceFinder.GetCurrentControllerType();
127+
attempts--;
128+
yield return delayInstruction;
129+
}
130+
131+
if (foundControllerType != SDK_BaseController.ControllerType.Undefined && controllerType == foundControllerType)
132+
{
133+
ToggleObject();
134+
}
135+
}
136+
137+
protected virtual void ToggleObject()
138+
{
139+
if (target is GameObject)
140+
{
141+
ToggleGameObject();
142+
}
143+
else if (target.GetType().IsSubclassOf(typeof(Component)))
144+
{
145+
ToggleComponent();
146+
}
147+
}
148+
149+
protected virtual void ToggleGameObject()
150+
{
151+
if (target != null)
152+
{
153+
GameObject toggleTarget = (GameObject)target;
154+
toggleTarget.SetActive(objectState);
155+
}
156+
}
157+
158+
protected virtual void ToggleComponent()
159+
{
160+
if (target != null)
161+
{
162+
Component toggleTarget = (Component)target;
163+
PropertyInfo property = toggleTarget.GetType().GetProperty("enabled");
164+
if (property != null)
165+
{
166+
property.SetValue(toggleTarget, objectState, null);
167+
}
168+
}
169+
}
170+
}
171+
}

Assets/VRTK/Source/Scripts/Utilities/VRTK_SDKObjectState.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/VRTK/Source/Scripts/Utilities/VRTK_SDKTransformModify.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public class VRTK_SDKTransformModify : MonoBehaviour
3838
public List<VRTK_SDKTransformModifiers> sdkOverrides = new List<VRTK_SDKTransformModifiers>();
3939

4040
protected VRTK_SDKManager sdkManager;
41-
protected int findControllerAttempts = 25;
41+
//TODO: REPLACE WITH GENERIC CONTROLLER TYPE AVAILABLE EVENT
42+
protected int findControllerAttempts = 100;
4243
protected float findControllerAttemptsDelay = 0.1f;
4344
protected Coroutine attemptFindControllerModel;
4445

0 commit comments

Comments
 (0)