63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
public class ManualScroller : MonoBehaviour, IScrollHandler, IPointerEnterHandler, IPointerExitHandler
|
|
{
|
|
public RectTransform viewport;
|
|
public RectTransform content;
|
|
public Slider slider;
|
|
public float scrollSpeed = 50.0f;
|
|
|
|
private bool isMouseOver = false;
|
|
//private Vector2 lastDragPosition;
|
|
void Start()
|
|
{
|
|
slider.onValueChanged.AddListener(OnSliderValueChanged);
|
|
}
|
|
public void OnScroll(PointerEventData eventData)
|
|
{
|
|
if (!isMouseOver) return;
|
|
|
|
Vector2 pos = content.anchoredPosition;
|
|
float max = content.rect.height - viewport.rect.height;
|
|
pos.y -= eventData.scrollDelta.y * scrollSpeed;
|
|
if (pos.y < 0) pos.y = 0;
|
|
if (pos.y > max) pos.y = max;
|
|
content.anchoredPosition = pos;
|
|
|
|
slider.value = pos.y / max;
|
|
}
|
|
//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;
|
|
//}
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
isMouseOver = true;
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
isMouseOver = false;
|
|
}
|
|
public void OnSliderValueChanged(float v)
|
|
{
|
|
Vector2 pos = content.anchoredPosition;
|
|
float max = content.rect.height - viewport.rect.height;
|
|
pos.y = max * v;
|
|
content.anchoredPosition = pos;
|
|
}
|
|
}
|