当前位置: 首页 > news >正文

Unity插件SuperScrollView详解

一.SuperScrollView插件概述

UGUI SUPER SCROLLVIEW基于UGUI ScrollRect提供了易于定制的滚动浏览量。这是一组C#脚本,可帮助您创建所需的ScrollView。它非常强大且针对性能高度优化。

此插件是收费的,在AssetStore可以下载

SuperScrollView应该掌握到什么程度?

  • 1.使用时知道Editor中怎么设置,脚本中逻辑怎么写
  • 2.LoopListView2, LoopGridView and LoopStaggeredGridView 这3个核心组件分别在什么情况使用
  • 3.会使用脚本中的公有方法

二.SuperScrollView安装

将unitypackage包拖入Asset目录完成导入,生成SuperScrollView目录,包含了Demo,Editor和Scripts三个目录,还有一个很有帮助的文档DocumentV2_4,如下图所示:

三.官方文档总结提炼(快速入门)

3.1 概述

SuperScrollView中有3个核心组件: LoopListView2 , LoopGridView and LoopStaggeredGridView,他们都附加到ScrollRect组件所在的gameObject上,他们帮助ScrollRect实现高性能任意数量的item和节省内存。
实现原理上总的来说,使用内存池,监听滚动事件,实现了item的回收和复用,自动设置了每个item的位置。

3.2 LoopListView2组件

3.2.1 LoopListView2 Inspector Settings

Size:每个Item可以有不同的Prefab,Size指Item使用的Prefab总数

ElementX:每个Item有4个参数,当展开Element0后,可以看到有2个Element0,下面的Element0下面还有4个属性,以改变Padding为例,改变MPading和改变ItemPadding是一样的,这里推荐直接改ItemPadding,下面之所以多了4个属性可能是因为XPosOffset是自适应的属性

当Item有多个prefab时,需要添加多个Element

eg:一个排行榜有多个页签,切换页签时item不一样,这种情况需要添加多个Element

ItemPrefab:该Item使用gameObject的引用

ItemPadding:和下个Item的间距

XPosOffset:ScrollView最左侧到Item最左侧的间距

InitCreateCount:该Item对象池初始数量(可以填0,不知道为什么要手动设置,官方没有说)

SupportScrollbar:使用ScrollBar时勾上

ItemSnapEnable: 启用后,当滚动结束时,Item将被吸附(Snap)到预设的位置

ItemSnapPivot:给Item定义一个吸附轴心点,控制Item被吸附的位置

ViewPortSnapPivot:给ViewPort定义一个吸附轴心点,控制ViewPort吸附Item的位置

在上图中,ViewPortSnapPivot的Y设为1,代表ViewPort顶部将吸附item

ItemSnapPivot的Y设为0.5表示item中部将被吸附,滚动效果符合预期

ArrangeType:滚动方向,有4种选择

3.2.2 LoopListView2 Example

下文给出一个简化的LoopListView2 Example,可以取来作为快速开发的基础代码

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

namespace SuperScrollView
{
    public class ItemData1
    {
        public string itemName;
        public string itemDes;
        public ItemData1(string name, string des)
        {
            itemName = name;
            itemDes = des;
        }
    }
    public class TopToBottomDemoScript : MonoBehaviour
    {
        private LoopListView2 mLoopListView;
        private int itemNum = 15;
        List<ItemData1> ssvData = new List<ItemData1>();
        void Start()
        {
            mLoopListView = GetComponent<LoopListView2>();
            for (int i = 0; i < itemNum; i++)
            {
                ItemData1 data = new ItemData1("name"+i, "des"+i);
                ssvData.Add(data);
            }
            mLoopListView.InitListView(itemNum, OnGetItemByIndex);
        }
        LoopListViewItem2 OnGetItemByIndex(LoopListView2 listView, int index)
        {
            if (index < 0 || index >= itemNum)
            {
                return null;
            }
            ItemData1 itemData = ssvData[index];
            if(itemData == null)
            {
                return null;
            }
            LoopListViewItem2 item = listView.NewListViewItem("ItemPrefab1");
            ListItemX itemScript = item.GetComponent<ListItemX>();
            if (item.IsInitHandlerCalled == false)
            {
                item.IsInitHandlerCalled = true;
                itemScript.Init();
            }
            itemScript.UpdateItemUI(itemData,index);
            return item;
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace SuperScrollView
{
    public class ListItemX : MonoBehaviour
    {
        private Text mNameText;
        private Text mDescText;
        int mItemDataIndex = -1;
        public void Init()
        {
            mNameText = transform.Find("TextName").GetComponent<Text>();
            mDescText = transform.Find("TextDesc2").GetComponent<Text>();
        }

        public void UpdateItemUI(ItemData1 itemData, int itemIndex)
        {
            mItemDataIndex = itemIndex;
            mNameText.text = itemData.itemName;
            mDescText.text = itemData.itemDes;
        }
    }
}

相关文章:

  • 端到端语音识别案例
  • Docker部署sprintboot后端项目
  • Android 系统中,应用申请的权限相关信息介绍
  • 一文详解QT环境搭建:Windows使用CLion配置QT开发环境
  • 深度学习-153-DeepSeek之调用远程大模型API接口和可用的开源Deepseek服务
  • C#实现HTTP服务器:处理文件上传---解析MultipartFormDataContent
  • 26考研——线性表_ 线性表的链式表示_单链表(2)
  • OpenCV 图形API(或称G-API)(1)
  • 周学习总结
  • 本地后台运行redis服务
  • SpringMVC 拦截器(Interceptor)
  • 渗透测试:登录页面的测试-弱口令思路和实战
  • 计算机网络知识汇总
  • 【水印】水印识别的算法方案思考
  • 机器学习的一百个概念(5)数据增强
  • 习题2.2
  • 06-02-自考数据结构(20331)- 查找技术-动态查找知识点
  • C#: 输入(Console.ReadLine())和输出(Console.WriteLine())
  • Java-拼图小游戏跟学笔记
  • 前端各种for 循环
  • 哪个行业最需要做网站/搜索排名优化公司
  • 唐山网站制作app/郑州seo优化顾问阿亮
  • 对电子政务做技术支持的网站/快速提升关键词排名软件
  • 舞阳网站建设/关键词搜索站长工具
  • 各类网站建设/最基本的网站设计
  • canvas案例网站/seo在线推广