Unity滚动列表Mark

Unity滚动列表Mark

背景图片 和滚动区域图片

在滚动区域图片上添加Scroll Rect

添加Scrollbar 方向设置为bottom to top

设置好scrollbar大小 和handle与锚点的匹配

Unity滚动列表Mark

在ScrollRect 下创建空物体layout 铺满ScrollRect 设置好中心点 在layout上添加vertical layout group管理列表内物品

Unity滚动列表Mark

创建列表内物品的预制体 创建ShopItem

Unity滚动列表Mark

设置layout上的vertical layout group 取消勾选height 

Unity滚动列表Mark


ScrollRect上面添加 mask组件进行遮罩调整scrollRect显示的范围,layout的大小要根据layout下item显示的大小用代码进行控制,同时scrollRect上的content设置为layout

Unity滚动列表Mark     

Unity滚动列表Mark

设置Scrollbar 的value 和size

Unity滚动列表Mark

写代码对layout大小进行控制。

foreach (Item item in ShopItemList)
        {
            GameObject go=GameObject.Instantiate(ShopItemPrefab);
            go.transform.SetParent(Layout,false);
            
            go.GetComponent<ShopItem>().AddAShopItem(item);
            go.GetComponent<ShopItem>().ShowAShopItem();
        }
        
        
        int shopItemCount = ShopItemList.Count;
        Vector2 size = Layout.GetComponent<RectTransform>().sizeDelta;
        Layout.GetComponent<RectTransform>().sizeDelta = new Vector2(size.x,
            shopItemCount * (ShopItemPrefab.GetComponent<RectTransform>().sizeDelta.y + 5f));//space




        ShopScrollBar.value = 1;