Files
Simulation/Assets/Scripts/UI/ManualScroller.cs

63 lines
1.8 KiB
C#
Raw Permalink Normal View History

2025-04-29 10:50:48 +09:00
using UnityEngine;
2025-04-29 11:43:30 +09:00
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ManualScroller : MonoBehaviour, IScrollHandler, IPointerEnterHandler, IPointerExitHandler
2025-04-29 10:50:48 +09:00
{
public RectTransform viewport;
public RectTransform content;
2025-04-29 11:43:30 +09:00
public Slider slider;
2025-04-29 10:50:48 +09:00
public float scrollSpeed = 50.0f;
2025-04-29 11:43:30 +09:00
2025-04-29 10:50:48 +09:00
private bool isMouseOver = false;
2025-04-29 11:43:30 +09:00
//private Vector2 lastDragPosition;
void Start()
{
slider.onValueChanged.AddListener(OnSliderValueChanged);
}
2025-04-29 10:50:48 +09:00
public void OnScroll(PointerEventData eventData)
{
if (!isMouseOver) return;
Vector2 pos = content.anchoredPosition;
2025-04-29 11:43:30 +09:00
float max = content.rect.height - viewport.rect.height;
2025-04-29 10:50:48 +09:00
pos.y -= eventData.scrollDelta.y * scrollSpeed;
if (pos.y < 0) pos.y = 0;
2025-04-29 11:43:30 +09:00
if (pos.y > max) pos.y = max;
2025-04-29 10:50:48 +09:00
content.anchoredPosition = pos;
2025-04-29 11:43:30 +09:00
slider.value = pos.y / max;
2025-04-29 10:50:48 +09:00
}
2025-04-29 11:43:30 +09:00
//public void OnBeginDrag(PointerEventData eventData)
//{
// if (!isMouseOver) return;
//
// lastDragPosition = eventData.position;
//}
//public void OnDrag(PointerEventData eventData)
//{
// if (!isMouseOver) return;
//
// Vector2 delta = eventData.position - lastDragPosition;
// Vector2 pos = content.anchoredPosition;
// pos.y += delta.y;
// content.anchoredPosition = pos;
// lastDragPosition = eventData.position;
//}
2025-04-29 10:50:48 +09:00
public void OnPointerEnter(PointerEventData eventData)
{
isMouseOver = true;
}
public void OnPointerExit(PointerEventData eventData)
{
isMouseOver = false;
}
2025-04-29 11:43:30 +09:00
public void OnSliderValueChanged(float v)
{
Vector2 pos = content.anchoredPosition;
float max = content.rect.height - viewport.rect.height;
pos.y = max * v;
content.anchoredPosition = pos;
}
2025-04-29 10:50:48 +09:00
}