2025-03-12 20:59:12 +08:00
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
2025-11-20 15:40:38 +08:00
|
|
|
namespace AlicizaX.UI
|
2025-03-12 20:59:12 +08:00
|
|
|
{
|
|
|
|
|
public class ScrollbarEx : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler
|
|
|
|
|
{
|
|
|
|
|
private RectTransform handle;
|
|
|
|
|
private Scrollbar scrollbar;
|
|
|
|
|
|
|
|
|
|
public Action OnDragEnd;
|
|
|
|
|
|
|
|
|
|
private bool dragging;
|
|
|
|
|
private bool hovering;
|
2026-04-29 14:44:30 +08:00
|
|
|
private float targetHandleScale = 1f;
|
2025-03-12 20:59:12 +08:00
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
|
|
|
|
scrollbar = GetComponent<Scrollbar>();
|
|
|
|
|
handle = scrollbar.handleRect;
|
2026-04-29 14:44:30 +08:00
|
|
|
targetHandleScale = GetCurrentHandleScale();
|
2025-03-12 20:59:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
|
|
|
{
|
|
|
|
|
dragging = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
|
|
|
{
|
|
|
|
|
dragging = false;
|
|
|
|
|
if (!hovering)
|
|
|
|
|
{
|
2026-04-29 14:44:30 +08:00
|
|
|
SetHandleScale(1f);
|
2025-03-12 20:59:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OnDragEnd?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
|
|
|
{
|
|
|
|
|
hovering = true;
|
2026-04-29 14:44:30 +08:00
|
|
|
SetHandleScale(2f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
|
|
|
{
|
|
|
|
|
hovering = false;
|
|
|
|
|
if (!dragging)
|
2025-03-12 20:59:12 +08:00
|
|
|
{
|
2026-04-29 14:44:30 +08:00
|
|
|
SetHandleScale(1f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetHandleScale(float target)
|
|
|
|
|
{
|
|
|
|
|
if (handle == null || Mathf.Approximately(targetHandleScale, target))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targetHandleScale = target;
|
|
|
|
|
bool vertical = IsVerticalScrollbar();
|
2025-08-06 10:56:25 +08:00
|
|
|
#if PRIMETWEEN_SUPPORT
|
2026-04-29 14:44:30 +08:00
|
|
|
if (vertical)
|
|
|
|
|
{
|
|
|
|
|
PrimeTween.Tween.ScaleX(handle, target, 0.2f);
|
2025-03-12 20:59:12 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-04-29 14:44:30 +08:00
|
|
|
PrimeTween.Tween.ScaleY(handle, target, 0.2f);
|
|
|
|
|
}
|
2025-08-06 10:56:25 +08:00
|
|
|
#else
|
2026-04-29 14:44:30 +08:00
|
|
|
Vector3 scale = handle.localScale;
|
|
|
|
|
if (vertical)
|
|
|
|
|
{
|
|
|
|
|
scale.x = target;
|
2025-03-12 20:59:12 +08:00
|
|
|
}
|
2026-04-29 14:44:30 +08:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
scale.y = target;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handle.localScale = scale;
|
|
|
|
|
#endif
|
2025-03-12 20:59:12 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-29 14:44:30 +08:00
|
|
|
private float GetCurrentHandleScale()
|
2025-03-12 20:59:12 +08:00
|
|
|
{
|
2026-04-29 14:44:30 +08:00
|
|
|
if (handle == null)
|
2025-03-12 20:59:12 +08:00
|
|
|
{
|
2026-04-29 14:44:30 +08:00
|
|
|
return 1f;
|
2025-03-12 20:59:12 +08:00
|
|
|
}
|
2026-04-29 14:44:30 +08:00
|
|
|
|
|
|
|
|
return IsVerticalScrollbar() ? handle.localScale.x : handle.localScale.y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsVerticalScrollbar()
|
|
|
|
|
{
|
|
|
|
|
return scrollbar != null &&
|
|
|
|
|
(scrollbar.direction == Scrollbar.Direction.TopToBottom ||
|
|
|
|
|
scrollbar.direction == Scrollbar.Direction.BottomToTop);
|
2025-03-12 20:59:12 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|