Skip to content

Commit 0b9099d

Browse files
committed
Add ScrollWithInput script
1 parent e2f6d1e commit 0b9099d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

Runtime/ScrollWithInput.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using UnityEngine;
2+
using UnityEngine.EventSystems;
3+
using UnityEngine.InputSystem;
4+
using UnityEngine.UI;
5+
6+
namespace Zigurous.UI
7+
{
8+
[RequireComponent(typeof(ScrollRect))]
9+
[AddComponentMenu("Zigurous/UI/Navigation/Scroll With Input")]
10+
public class ScrollWithInput : MonoBehaviour
11+
{
12+
private ScrollRect _scrollRect;
13+
public InputAction scrollInput;
14+
public float sensitivity = 1.0f;
15+
16+
private void Reset()
17+
{
18+
this.scrollInput = new InputAction("ScrollInput", InputActionType.Value, null, null, null, "Vector2");
19+
this.scrollInput.AddBinding("<Gamepad>/rightStick");
20+
this.scrollInput.AddBinding("<Gamepad>/leftStick");
21+
this.scrollInput.AddBinding("<Gamepad>/dpad");
22+
}
23+
24+
private void Awake()
25+
{
26+
_scrollRect = GetComponent<ScrollRect>();
27+
}
28+
29+
private void OnEnable()
30+
{
31+
this.scrollInput.Enable();
32+
}
33+
34+
private void OnDisable()
35+
{
36+
this.scrollInput.Disable();
37+
}
38+
39+
private void Update()
40+
{
41+
EventSystem eventSystem = EventSystem.current;
42+
43+
if (eventSystem == null || eventSystem.currentSelectedGameObject == null) {
44+
return;
45+
}
46+
47+
if (eventSystem.currentSelectedGameObject == _scrollRect.gameObject ||
48+
eventSystem.currentSelectedGameObject.transform.parent == _scrollRect.content)
49+
{
50+
Vector2 input = this.scrollInput.ReadValue<Vector2>();
51+
_scrollRect.normalizedPosition += input * this.sensitivity * Time.unscaledDeltaTime;
52+
}
53+
}
54+
55+
}
56+
57+
}

Runtime/ScrollWithInput.cs.meta

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

0 commit comments

Comments
 (0)