Unity/유니티 기본
[4.27] NGUI를 이용한 ScrollView
ljw4104
2021. 4. 27. 14:21
구조
- UI Root에서 Scroll View 설치
- Scroll View 자식으로 Grid설치
Arrangement : 가로로 스크롤 할 경우에는 Horizontal, 아니면 Vertical로 해주면 된다.
Cell Width, Cell Height : 각각의 요소가 차지하는 공간을 설정, 설정 후에는 Execute를 해봐야 설정한 영역을 확인할 수 있다.
Test.cs
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
public class Test : MonoBehaviour
{
public GameObject prefab;
public GameObject prefabAD;
public UIGrid grid;
public Dictionary<int, ShopData> arrShop;
// Start is called before the first frame update
void Start()
{
var ta = Resources.Load<TextAsset>("ShopData");
var json = ta.text;
this.arrShop = JsonConvert.DeserializeObject<ShopData[]>(json).ToDictionary(x => x.id);
foreach(var item in this.arrShop)
{
if (item.Value.is_add)
{
var goAd = Instantiate(this.prefabAD, this.grid.transform);
var listItem = goAd.GetComponent<UIListItemAD>();
listItem.Init(item.Value);
}
else
{
var go = Instantiate(this.prefab, this.grid.transform);
var listItem = go.GetComponent<UIListItem>();
listItem.Init(item.Value);
}
}
this.grid.Reposition();
}
}