유니티/UI

유니티 UI - infinite scroll

민또배기 2022. 5. 24. 15:39
반응형

유니티 버전 : 2020.3.25f1

작업환경 : Mac (Monterey 12.3.1)

 

무한스크롤!!을 한번 해보겠습니다. 

인스타그램이나 유튜브를 보면 맨 아래로 내려가면 다시 컨텐츠가 생겨나는... 

 

유니티 scrolll view 를 이용해서 작성하겠습니다.

 

먼저 전체적인 모습은 다음과 같습니다.

 

Text는 현재 생성된 갯수를 보여주고 Create는 초반에 10개의 컨텐츠를 보여줍니다.

Up 버튼은 맨 위로 올려주는 버튼입니다.

하이어라키는 다음과 같습니다.

 

ScrollView 오브젝트에 ScrollView 스크립트를 넣어서 작성했습니다.

ScorllView.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScrollView : MonoBehaviour
{
    public Image content;
    public GameObject touchGuard;
    public GameObject scrollContent;
    public GameObject scrollviewbar;
    Scrollbar sb;
    RectTransform rt;

    public Text counts;

    private void Start()
    {
        rt = scrollContent.GetComponent<RectTransform>();
        touchGuard.SetActive(false);
    }
    private void Update()
    {
        rt.sizeDelta = new Vector2(500, scrollContent.transform.childCount * (100));
        counts.text = scrollContent.transform.childCount.ToString();
        if (scrollviewbar.activeSelf)
        {
            sb = scrollviewbar.GetComponent<Scrollbar>();
            if (sb.value < 0)
            {
                sb.value = 0.1f;
                touchGuard.SetActive(true);
                CreateContent();
            }
        }
    }
    public void CreateContent()
    {
        for (int i = 0; i < 10; i++)
        {
            Image go = Instantiate(content, Vector3.zero, Quaternion.identity);
            go.color = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
            go.transform.parent = scrollContent.transform;
        }
        touchGuard.SetActive(false);
    }

    public void UpperContent()
    {
        touchGuard.SetActive(true);
        StartCoroutine(upperScrol());
    }
    IEnumerator upperScrol()
    {
        while (true)
        {
            yield return null;
            if (sb.value >= 1) break;
            sb.value += 0.1f;
        }
        touchGuard.SetActive(false);
    }
}

https://youtu.be/cJEzq66CMjs

 

InfiniteScroll.unitypackage
0.01MB

 

https://kmong.com/gig/449118

 

유니티 기반 게임,앱 개발해 드립니다. | 1000000원부터 시작 가능한 총 평점 0점의 IT·프로그래밍,

0개 총 작업 개수 완료한 총 평점 0점인 민또배기의 IT·프로그래밍, 2D·3D 게임 서비스를 0개의 리뷰와 함께 확인해 보세요. IT·프로그래밍, 2D·3D 게임 제공 등 1000000원부터 시작 가능한 서비스

kmong.com

 

반응형