Skip to content

Commit 1532d3f

Browse files
FIX: Misaligned Virtual Cursor when changing resolution (ISXB-1119) (#2315)
Co-authored-by: Paulius Dervinis <54306142+Pauliusd01@users.noreply.github.com>
1 parent bdd4741 commit 1532d3f

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

Assets/Samples/GamepadMouseCursor/GamepadMouseCursorSample.unity

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ OcclusionCullingSettings:
1313
--- !u!104 &2
1414
RenderSettings:
1515
m_ObjectHideFlags: 0
16-
serializedVersion: 9
16+
serializedVersion: 10
1717
m_Fog: 0
1818
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
1919
m_FogMode: 3
@@ -42,8 +42,8 @@ RenderSettings:
4242
--- !u!157 &3
4343
LightmapSettings:
4444
m_ObjectHideFlags: 0
45-
serializedVersion: 12
46-
m_GIWorkflowMode: 1
45+
serializedVersion: 13
46+
m_BakeOnSceneLoad: 0
4747
m_GISettings:
4848
serializedVersion: 2
4949
m_BounceScale: 1
@@ -66,9 +66,6 @@ LightmapSettings:
6666
m_LightmapParameters: {fileID: 0}
6767
m_LightmapsBakeMode: 1
6868
m_TextureCompression: 1
69-
m_FinalGather: 0
70-
m_FinalGatherFiltering: 1
71-
m_FinalGatherRayCount: 256
7269
m_ReflectionCompression: 2
7370
m_MixedBakeMode: 2
7471
m_BakeBackend: 1
@@ -977,10 +974,10 @@ MonoBehaviour:
977974
m_Script: {fileID: 11500000, guid: 0cd44c1031e13a943bb63640046fad76, type: 3}
978975
m_Name:
979976
m_EditorClassIdentifier:
980-
m_UiScaleMode: 0
977+
m_UiScaleMode: 1
981978
m_ReferencePixelsPerUnit: 100
982979
m_ScaleFactor: 1
983-
m_ReferenceResolution: {x: 800, y: 600}
980+
m_ReferenceResolution: {x: 1920, y: 1080}
984981
m_ScreenMatchMode: 0
985982
m_MatchWidthOrHeight: 0
986983
m_PhysicalUnit: 3
@@ -1100,15 +1097,14 @@ Light:
11001097
m_PrefabAsset: {fileID: 0}
11011098
m_GameObject: {fileID: 832288260}
11021099
m_Enabled: 1
1103-
serializedVersion: 10
1100+
serializedVersion: 12
11041101
m_Type: 1
1105-
m_Shape: 0
11061102
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
11071103
m_Intensity: 1
11081104
m_Range: 10
11091105
m_SpotAngle: 30
11101106
m_InnerSpotAngle: 21.80208
1111-
m_CookieSize: 10
1107+
m_CookieSize2D: {x: 10, y: 10}
11121108
m_Shadows:
11131109
m_Type: 2
11141110
m_Resolution: -1
@@ -1152,8 +1148,12 @@ Light:
11521148
m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0}
11531149
m_UseBoundingSphereOverride: 0
11541150
m_UseViewFrustumForShadowCasterCull: 1
1151+
m_ForceVisible: 0
11551152
m_ShadowRadius: 0
11561153
m_ShadowAngle: 0
1154+
m_LightUnit: 1
1155+
m_LuxAtDistance: 1
1156+
m_EnableSpotReflector: 1
11571157
--- !u!4 &832288262
11581158
Transform:
11591159
m_ObjectHideFlags: 0

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ however, it has to be formatted properly to pass verification tests.
2020
- Fixed the `Auto-Save` toggle button with some extra pixels to align the text in the window better.
2121
- Align title font size with toolbar style in `Input Action` window.
2222
- Updated Action Properties headers to use colors consistent with GameObject component headers.
23+
- Fixed misaligned Virtual Cursor when changing resolution [ISXB-1119](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1119)
2324

2425
### Added
2526

Packages/com.unity.inputsystem/InputSystem/Plugins/UI/VirtualMouseInput.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
////TODO: add support for acceleration
1616

17-
////TODO: automatically scale mouse speed to resolution such that it stays constant regardless of resolution
18-
1917
////TODO: make it work with PlayerInput such that it will automatically look up actions in the actual PlayerInput instance it is used with (based on the action IDs it has)
2018

2119
////REVIEW: should we default the SW cursor position to the center of the screen?
@@ -291,7 +289,10 @@ protected void OnEnable()
291289

292290
// Add mouse device.
293291
if (m_VirtualMouse == null)
292+
{
294293
m_VirtualMouse = (Mouse)InputSystem.AddDevice("VirtualMouse");
294+
TryFindCanvas();
295+
}
295296
else if (!m_VirtualMouse.added)
296297
InputSystem.AddDevice(m_VirtualMouse);
297298

@@ -370,6 +371,7 @@ protected void OnDisable()
370371
private void TryFindCanvas()
371372
{
372373
m_Canvas = m_CursorGraphic?.GetComponentInParent<Canvas>();
374+
m_CanvasScaler = m_CursorGraphic?.GetComponentInParent<CanvasScaler>();
373375
}
374376

375377
private void TryEnableHardwareCursor()
@@ -421,6 +423,14 @@ private void UpdateMotion()
421423
}
422424
else
423425
{
426+
var resolutionXScale = 1f;
427+
var resolutionYScale = 1f;
428+
if (m_CanvasScaler != null)
429+
{
430+
resolutionXScale = m_Canvas.pixelRect.xMax / m_CanvasScaler.referenceResolution.x;
431+
resolutionYScale = m_Canvas.pixelRect.yMax / m_CanvasScaler.referenceResolution.y;
432+
}
433+
424434
var currentTime = InputState.currentTime;
425435
if (Mathf.Approximately(0, m_LastStickValue.x) && Mathf.Approximately(0, m_LastStickValue.y))
426436
{
@@ -430,7 +440,7 @@ private void UpdateMotion()
430440

431441
// Compute delta.
432442
var deltaTime = (float)(currentTime - m_LastTime);
433-
var delta = new Vector2(m_CursorSpeed * stickValue.x * deltaTime, m_CursorSpeed * stickValue.y * deltaTime);
443+
var delta = new Vector2(m_CursorSpeed * resolutionXScale * stickValue.x * deltaTime, m_CursorSpeed * resolutionYScale * stickValue.y * deltaTime);
434444

435445
// Update position.
436446
var currentPosition = m_VirtualMouse.position.value;
@@ -454,7 +464,9 @@ private void UpdateMotion()
454464
if (m_CursorTransform != null &&
455465
(m_CursorMode == CursorMode.SoftwareCursor ||
456466
(m_CursorMode == CursorMode.HardwareCursorIfAvailable && m_SystemMouse == null)))
457-
m_CursorTransform.anchoredPosition = newPosition;
467+
{
468+
m_CursorTransform.anchoredPosition = m_CanvasScaler == null ? newPosition : new Vector2(newPosition.x / resolutionXScale, newPosition.y / resolutionYScale);
469+
}
458470

459471
m_LastStickValue = stickValue;
460472
m_LastTime = currentTime;
@@ -508,6 +520,7 @@ private void UpdateMotion()
508520
[SerializeField] private InputActionProperty m_ScrollWheelAction;
509521

510522
private Canvas m_Canvas; // Canvas that gives the motion range for the software cursor.
523+
private CanvasScaler m_CanvasScaler;
511524
private Mouse m_VirtualMouse;
512525
private Mouse m_SystemMouse;
513526
private Action m_AfterInputUpdateDelegate;

0 commit comments

Comments
 (0)