Skip to content

Commit 2c5c619

Browse files
committed
Better handling for controllers buttons in the editor
1 parent 692a0e7 commit 2c5c619

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

Packages/webxr/Runtime/Scripts/WebXRController.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,22 @@ public enum Axis2DTypes
7171
private InputDeviceCharacteristics xrHand = InputDeviceCharacteristics.Controller;
7272
private InputDevice? inputDevice;
7373
private HapticCapabilities? hapticCapabilities;
74+
private int buttonsFrameUpdate = -1;
75+
76+
private void Update()
77+
{
78+
TryUpdateButtons();
79+
}
7480
#endif
7581

76-
public void TryUpdateButtons()
82+
private void TryUpdateButtons()
7783
{
7884
#if UNITY_EDITOR
85+
if (buttonsFrameUpdate == Time.frameCount)
86+
{
87+
return;
88+
}
89+
buttonsFrameUpdate = Time.frameCount;
7990
if (!WebXRManager.Instance.isSubsystemAvailable && inputDevice != null)
8091
{
8192
inputDevice.Value.TryGetFeatureValue(CommonUsages.trigger, out trigger);
@@ -151,6 +162,7 @@ private void UpdateButtons(WebXRControllerButton[] buttons)
151162

152163
public float GetAxis(AxisTypes action)
153164
{
165+
TryUpdateButtons();
154166
switch (action)
155167
{
156168
case AxisTypes.Grip:
@@ -163,6 +175,7 @@ public float GetAxis(AxisTypes action)
163175

164176
public Vector2 GetAxis2D(Axis2DTypes action)
165177
{
178+
TryUpdateButtons();
166179
switch (action)
167180
{
168181
case Axis2DTypes.Thumbstick:
@@ -175,6 +188,7 @@ public Vector2 GetAxis2D(Axis2DTypes action)
175188

176189
public bool GetButton(ButtonTypes action)
177190
{
191+
TryUpdateButtons();
178192
if (!buttonStates.ContainsKey(action))
179193
{
180194
return false;
@@ -209,6 +223,7 @@ private void SetPastButtonState(ButtonTypes action, bool isPressed)
209223

210224
public bool GetButtonDown(ButtonTypes action)
211225
{
226+
TryUpdateButtons();
212227
if (GetButton(action) && !GetPastButtonState(action))
213228
{
214229
SetPastButtonState(action, true);
@@ -219,6 +234,7 @@ public bool GetButtonDown(ButtonTypes action)
219234

220235
public bool GetButtonUp(ButtonTypes action)
221236
{
237+
TryUpdateButtons();
222238
if (!GetButton(action) && GetPastButtonState(action))
223239
{
224240
SetPastButtonState(action, false);
@@ -229,6 +245,7 @@ public bool GetButtonUp(ButtonTypes action)
229245

230246
public float GetButtonIndexValue(int index)
231247
{
248+
TryUpdateButtons();
232249
switch (index)
233250
{
234251
case 0:
@@ -249,6 +266,7 @@ public float GetButtonIndexValue(int index)
249266

250267
public float GetAxisIndexValue(int index)
251268
{
269+
TryUpdateButtons();
252270
switch (index)
253271
{
254272
case 0:

0 commit comments

Comments
 (0)