2025-07-08 10:46:31 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
namespace Superlazy.UI
|
|
|
|
|
{
|
|
|
|
|
public class SLUIScrollRectRefresh : SLUIComponent
|
|
|
|
|
{
|
|
|
|
|
public float delay = 0.5f;
|
|
|
|
|
public bool toTop = false; // 켰으면 반대방향
|
|
|
|
|
|
|
|
|
|
private ScrollRect scrollRect;
|
|
|
|
|
private RectTransform container;
|
|
|
|
|
|
|
|
|
|
private float lastSize;
|
|
|
|
|
private Coroutine autoScrollCoroutine;
|
|
|
|
|
|
|
|
|
|
protected override void Validate()
|
|
|
|
|
{
|
|
|
|
|
scrollRect = gameObject.GetComponent<ScrollRect>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Init()
|
|
|
|
|
{
|
|
|
|
|
container = scrollRect.content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Enable()
|
|
|
|
|
{
|
|
|
|
|
if (scrollRect.horizontal)
|
|
|
|
|
{
|
|
|
|
|
lastSize = container.rect.width;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lastSize = container.rect.height;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Disable()
|
|
|
|
|
{
|
|
|
|
|
if (autoScrollCoroutine != null)
|
|
|
|
|
{
|
|
|
|
|
StopCoroutine(autoScrollCoroutine);
|
|
|
|
|
autoScrollCoroutine = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void LateUpdate()
|
|
|
|
|
{
|
|
|
|
|
var currentSize = scrollRect.horizontal ? container.rect.width : container.rect.height;
|
|
|
|
|
|
|
|
|
|
if (Mathf.Abs(currentSize - lastSize) > 0.1f)
|
|
|
|
|
{
|
|
|
|
|
lastSize = currentSize;
|
|
|
|
|
|
|
|
|
|
if (autoScrollCoroutine != null)
|
|
|
|
|
{
|
|
|
|
|
StopCoroutine(autoScrollCoroutine);
|
|
|
|
|
}
|
|
|
|
|
autoScrollCoroutine = StartCoroutine(AutoScroll());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator AutoScroll()
|
|
|
|
|
{
|
|
|
|
|
var elapsed = 0f;
|
|
|
|
|
// 현재 스크롤 위치 (verticalNormalizedPosition: 1은 상단, 0은 하단)
|
|
|
|
|
var startPosition = scrollRect.horizontal ? scrollRect.horizontalNormalizedPosition : scrollRect.verticalNormalizedPosition;
|
|
|
|
|
// 목표 위치: 최신 항목이 보이도록 스크롤의 하단 (0)
|
|
|
|
|
var targetPosition = toTop ? 1f : 0f;
|
|
|
|
|
|
|
|
|
|
while (elapsed < delay)
|
|
|
|
|
{
|
|
|
|
|
elapsed += Time.deltaTime;
|
|
|
|
|
float t = elapsed / delay;
|
|
|
|
|
// EaseOutCubic easing 함수 적용 (부드럽고 자연스러운 감속)
|
|
|
|
|
t = EaseOutCubic(t);
|
|
|
|
|
// 현재 위치에서 목표 위치로 보간
|
|
|
|
|
|
|
|
|
|
var newPosition = Mathf.Lerp(startPosition, targetPosition, t);
|
|
|
|
|
if (scrollRect.horizontal)
|
|
|
|
|
{
|
|
|
|
|
scrollRect.horizontalNormalizedPosition = newPosition;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
scrollRect.verticalNormalizedPosition = newPosition;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (scrollRect.horizontal)
|
|
|
|
|
{
|
|
|
|
|
scrollRect.horizontalNormalizedPosition = targetPosition;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
scrollRect.verticalNormalizedPosition = targetPosition;
|
|
|
|
|
}
|
|
|
|
|
autoScrollCoroutine = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float EaseOutCubic(float t)
|
|
|
|
|
{
|
|
|
|
|
return 1f - Mathf.Pow(1f - t, 3f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|