Unity UGUI 效果 之 Scroll 根据内容多少,动态实现鼠标滑动滚动浏览预览( Scrollbar + ScrollRect + XXLayoutGroup)

47 篇文章 17 订阅
13 篇文章 7 订阅

 

 

Unity UGUI 效果 之  Scroll 根据内容多少,动态实现鼠标滑动滚动浏览预览( Scrollbar + ScrollRect + XXLayoutGroup)

 

目录

Unity UGUI 效果 之  Scroll 根据内容多少,动态实现鼠标滑动滚动浏览预览( Scrollbar + ScrollRect + XXLayoutGroup)

一、简单介绍

二、实现原理

三、注意实现

四、效果预览

五、实现步骤

六、关键代码

附录方法二:使用 Vertical Layout Group + Content Size Fitter 无需代码轻松实现滚动预览


 

一、简单介绍

UGUI,是Unity自带的 GUI 系统,有别于 NGUI;使用 UGUI 也能制作出比较酷炫的效果 。

本节介绍,使用 UGUI 通过代码实现在内容变化不定的情况下,动态调整Scroll等,实现可以鼠标拖动预览的效果,方法不唯一,欢迎指正。

 

二、实现原理

1、ScrollRect、Scrollbar,实现可鼠标滑动滚动功能

2、Mask 遮罩多余的内容

3、XXLayoutGroup 方便排布内容

 

三、注意实现

1、XXLayoutGroup  组件的 Pivot ,根据需要设定位置(例如内容向下增长,即最好Pivot 设置在中上处,(0.5,1)位置为宜)

2、这里只是实现了垂直方向的滚动滑动浏览,水平方向同理,大家可以自行实现哈

 

四、效果预览

 

五、实现步骤

1、打开Unity,新建一个工程

 

2、添加一个 Image,最为 ScrollRect 背景,Scrollbar 动态滚动滑动使用,这里GameObject,添加 VerticalLayoutGroup,垂直布局

 

3、ScrollRectDynamicHeight_Image,添加 Mask ,ScrollRect 组件,并对应赋值

 

4、ItemsContent_VerticalLayoutGroup 设置如下,根据需要设置,不唯一

 

5、Scrollbar 合理调整大小,设置可以默认,不唯一

 

6、Item_Image 作为 ItemsContent_VerticalLayoutGroup  的 item 条目,这里只是示范,根据具体内容指定哈

 

7、新建脚本,实现动态内容滚动滑动预览的功能

 

8、ScrollVerticalDynamicHeightRect

 

9、运行场景,效果如上

 

六、关键代码

1、ScrollVerticalDynamicHeightRect

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

namespace XANTools
{
    public class ScrollVerticalDynamicHeightRect : MonoBehaviour
    {
        // 设置获取相关参数
        public Transform ItemsContent_VerticalLayoutGroup;

        // Item 预制体
        public GameObject itemImagePrefab;

        // ScrollRect 对象(动态激活他,看情况使用)
        public ScrollRect scrollRect;

        // GridVertical 物体的原始大小
        private Vector2 originSize;

        // Use this for initialization
        void Start()
        {

            //获取最初goParent的宽高
            originSize = ItemsContent_VerticalLayoutGroup.GetComponent<RectTransform>().sizeDelta;

        }

        // Update is called once per frame
        void Update()
        {

            //按下空格键调用添加ItemImage
            if (Input.GetKeyDown(KeyCode.Space))
            {
                // 随机增加若干个 Item
                AddScrollItem(Random.Range(1,5));
            }

        }

        /// <summary>
        /// 生成指定数量 Item,实现动态调整 Scroll 
        /// </summary>
        /// <param name="count"></param>
        private void AddScrollItem(int count)
        {

            //生成ItemImage
            for (int i = 0; i < count; i++)
            {
                GameObject go = Instantiate(itemImagePrefab) as GameObject;
                go.transform.SetParent(ItemsContent_VerticalLayoutGroup);

            }

            //调整ScrollRect边框大小
            //获取当前的ItemImage的实际个数
            int itemImageCount = ItemsContent_VerticalLayoutGroup.childCount;

            //计算当前实际生成ItemImage所需要的goParent的高度
            float ScrollRectY = itemImageCount * ((itemImagePrefab.GetComponent<RectTransform>().sizeDelta.y)
                + ItemsContent_VerticalLayoutGroup.GetComponent<VerticalLayoutGroup>().spacing);

            //比原先小则,保持原有尺寸不变,反之,高度设置为所需要的
            if (ScrollRectY <= originSize.y)
            {

                ItemsContent_VerticalLayoutGroup.GetComponent<RectTransform>().sizeDelta = originSize;
                // 不必要激活可滚动浏览
                scrollRect.enabled = false;
            }
            else
            {

                ItemsContent_VerticalLayoutGroup.GetComponent<RectTransform>().sizeDelta = new Vector2(originSize.x, ScrollRectY);
                // 激活可滚动浏览
                scrollRect.enabled = true;
            }



        }
    }
}

 

附录方法二:使用 Vertical Layout Group + Content Size Fitter 无需代码轻松实现滚动预览

原理简述:Content Size Fitter 会自动根据Vertical Layout Group 的子物体的多少自动调整 Vertical Layout Group 的整体大小,然后再 Scroll 实现滚动预览

1、在场景中添加一个Image,调整大小,挂载 ScrollRect 和 Mask ,把 LayoutContent 和 Scrollbar 对应赋值

 

2、Layout Content 添加 VerticallayoutGroup 和 Content Size Fitter ,其中 Content Size Fitter 的对应属性 vertical Fit (根据需要对应设置) Preferred Size ,这样 Layout Content 大小就会随着子物体的多少自动调整大小了

 

3、Scrollbar UGUI 自带的UI组件,无需做其他修改,只是把滚动预览方向改为竖直即可

 

4、Layout Content 子物体,根据需要添加好,运行场景,效果如下

 

5、注意,如果Layout Content每次添加子物体,预览总从中部开始,可以调整Pivot 为(0.5,1),调整一次

Layout Content 的边框位置,下次添加子物体和预览就不会从中部开始了

 

 

 

  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
实现多页翻页和多页任意翻页的方式有很多种,下面介绍一种较为简单的实现方式。 1. 创建一个 ScrollRect 对象,并将 Content 对象的 Rect Transform 的 Anchor Presets 设置为 Stretch。 2. 在 Content 下创建多个 Page 对象,并将它们的 Rect Transform 的 Anchor Presets 设置为 Stretch。每个 Page 对象都应该放置一个要显示的 UI 元素。 3. 为 ScrollRect 添加一个 Horizontal Layout Group 组件,并将 Child Force Expand 和 Child Control Height 设置为 true。 4. 在代码中添加以下代码,实现多页翻页和多页任意翻页的效果: ```csharp using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections.Generic; public class ScrollRectPage : MonoBehaviour, IBeginDragHandler, IEndDragHandler { public ScrollRect scrollRect; public float smoothTime = 0.5f; public float sensitivity = 0.2f; private List<float> pages = new List<float>(); private int currentPage = 0; private float targetPage = 0; private bool isDragging = false; void Start() { int pageCount = scrollRect.content.childCount; for (int i = 0; i < pageCount; i++) { float page = i * scrollRect.viewport.rect.width / (pageCount - 1); pages.Add(-page); } } void Update() { if (!isDragging) { float nearestPage = pages[currentPage]; for (int i = 0; i < pages.Count; i++) { if (Mathf.Abs(pages[i] - scrollRect.horizontalNormalizedPosition) < Mathf.Abs(nearestPage - scrollRect.horizontalNormalizedPosition)) { nearestPage = pages[i]; targetPage = i; } } if (currentPage != targetPage) { currentPage = (int)targetPage; } scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, nearestPage, smoothTime * Time.deltaTime); } } public void OnBeginDrag(PointerEventData eventData) { isDragging = true; } public void OnEndDrag(PointerEventData eventData) { isDragging = false; float delta = eventData.delta.x / scrollRect.viewport.rect.width; if (Mathf.Abs(delta) > sensitivity) { if (delta > 0) { currentPage--; } else { currentPage++; } } currentPage = Mathf.Clamp(currentPage, 0, pages.Count - 1); targetPage = currentPage; } } ``` 5. 将 ScrollRectPage 脚本挂载到 ScrollRect 对象上,并将 scrollRect 属性设置为 ScrollRect 对象。 6. 运行程序,就可以实现多页翻页和多页任意翻页的效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

仙魁XAN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值