Skip to content

Commit b11acea

Browse files
committed
Moved the instance counter outside the class to prevent static initialization when not used
1 parent 4f9c4b2 commit b11acea

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

Assets/Tests/InputSystem/Plugins/OnScreenTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,25 +371,25 @@ public void Devices_DisablingLastOnScreenControlDoesReportActiveControl()
371371
{
372372
var gameObject = new GameObject();
373373

374-
Assert.That(OnScreenControl.HasAnyActive, Is.False);
374+
Assert.That(UGUIOnScreenControlUtils.HasAnyActive, Is.False);
375375

376376
var buttonA = gameObject.AddComponent<OnScreenButton>();
377377

378-
Assert.That(OnScreenControl.HasAnyActive, Is.True);
378+
Assert.That(UGUIOnScreenControlUtils.HasAnyActive, Is.True);
379379

380380
var buttonB = gameObject.AddComponent<OnScreenButton>();
381381
buttonA.controlPath = "/<Keyboard>/a";
382382
buttonB.controlPath = "/<Keyboard>/b";
383383

384-
Assert.That(OnScreenControl.HasAnyActive, Is.True);
384+
Assert.That(UGUIOnScreenControlUtils.HasAnyActive, Is.True);
385385

386386
buttonA.enabled = false;
387387

388-
Assert.That(OnScreenControl.HasAnyActive, Is.True);
388+
Assert.That(UGUIOnScreenControlUtils.HasAnyActive, Is.True);
389389

390390
buttonB.enabled = false;
391391

392-
Assert.That(OnScreenControl.HasAnyActive, Is.False);
392+
Assert.That(UGUIOnScreenControlUtils.HasAnyActive, Is.False);
393393
}
394394

395395
// https://fogbugz.unity3d.com/f/cases/1271942

Packages/com.unity.inputsystem/InputSystem/Plugins/OnScreen/OnScreenControl.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,14 @@ protected void SentDefaultValueToControl()
217217
InputSystem.QueueEvent(m_InputEventPtr);
218218
}
219219

220-
// Used by PlayerInput auto switch for scheme to prevent using Pointer device.
221-
internal static bool HasAnyActive => s_nbActiveInstances != 0;
222-
private static int s_nbActiveInstances = 0;
223-
224220
protected virtual void OnEnable()
225221
{
226-
++s_nbActiveInstances;
222+
++UGUIOnScreenControlUtils.s_nbActiveInstances;
227223
SetupInputControl();
228224
if (m_Control == null)
229225
return;
230226
// if we are in single player and if it the first active switch to the target device.
231-
if (s_nbActiveInstances == 1 &&
227+
if (UGUIOnScreenControlUtils.s_nbActiveInstances == 1 &&
232228
PlayerInput.isSinglePlayer)
233229
{
234230
var firstPlayer = PlayerInput.GetPlayerByIndex(0);
@@ -255,7 +251,7 @@ protected virtual void OnEnable()
255251

256252
protected virtual void OnDisable()
257253
{
258-
--s_nbActiveInstances;
254+
--UGUIOnScreenControlUtils.s_nbActiveInstances;
259255
if (m_Control == null)
260256
return;
261257

@@ -349,6 +345,11 @@ internal string GetWarningMessage()
349345

350346
internal static class UGUIOnScreenControlUtils
351347
{
348+
// Used by PlayerInput auto switch for scheme to prevent using Pointer device.
349+
// stored outside of the OnScreenControl to static initialization when it's no used
350+
internal static bool HasAnyActive => s_nbActiveInstances != 0;
351+
internal static int s_nbActiveInstances = 0;
352+
352353
public static RectTransform GetCanvasRectTransform(Transform transform)
353354
{
354355
var parentTransform = transform.parent;

Packages/com.unity.inputsystem/InputSystem/Plugins/PlayerInput/PlayerInput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,7 @@ private static bool OnPreFilterUnpairedDeviceUsed(InputDevice device, InputEvent
18691869
// Early out if the device isn't usable with any of our control schemes.
18701870
var actions = all[0].actions;
18711871
// Skip Pointer device if any OnScreenControl is active since they will use it to generate device event
1872-
return actions != null && (!OnScreenControl.HasAnyActive || !(device is Pointer)) && actions.IsUsableWithDevice(device);
1872+
return actions != null && (!UGUIOnScreenControlUtils.HasAnyActive || !(device is Pointer)) && actions.IsUsableWithDevice(device);
18731873
}
18741874

18751875
private void OnUnpairedDeviceUsed(InputControl control, InputEventPtr eventPtr)

0 commit comments

Comments
 (0)