본문 바로가기
개발/Unity

유니티 C# 스크롤바 Scrollbar 사용법 간단 구현

by SPNK 2023. 5. 11.
반응형
  • 코드 작성
using UnityEngine;
using UnityEngine.UI;

public class ScrollbarController : MonoBehaviour
{
    public Scrollbar scrollbar;
    public RectTransform contentTransform;

    void Start()
    {
        scrollbar.onValueChanged.AddListener(OnScrollbarValueChanged);
    }

    void OnScrollbarValueChanged(float value)
    {
        float contentHeight = contentTransform.rect.height;
        float viewportHeight = scrollbar.GetComponent<RectTransform>().rect.height;
        float newYPos = (contentHeight - viewportHeight) * value;
        contentTransform.anchoredPosition = new Vector2(contentTransform.anchoredPosition.x, newYPos);
    }
}
반응형

댓글