ClientServer/Client/Assets/Scripts/UI/MoveScrollRect.cs

48 lines
1.4 KiB
C#
Raw Normal View History

2025-09-06 17:17:39 +04:00
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using NaughtyAttributes;
//thomas09
//this script is to be attached to all buttons in a scroll list. So that when using controller to navigate. It will position the scroll view accordingly.
public class MoveScrollRect : MonoBehaviour, ISelectHandler
{
[Header("Only Assign in the inspector if the relationship is inactive.")]
[SerializeField] ScrollRect scrollRect = null;
private void Awake()
{
if (scrollRect == null)
scrollRect = GetComponentInParent<ScrollRect>();
}
public void OnSelect(BaseEventData _)
{
if (!scrollRect)
{
Debug.LogError("Could not find ScrollRect");
return;
}
if (Player.Instance.GetInputHandler().UsingGamePad)
{
RectTransform contentPanel = scrollRect.content;
Canvas.ForceUpdateCanvases();
float prevX = contentPanel.anchoredPosition.x;
Vector3 offset = contentPanel.position;
offset.y -= 35.0f;
Vector2 anchoredPosition =
(Vector2)scrollRect.transform.InverseTransformPoint(offset) -
(Vector2)scrollRect.transform.InverseTransformPoint(transform.position);
anchoredPosition.x = prevX;
contentPanel.anchoredPosition = anchoredPosition;
}
}
}