Unity排行榜优化滚动效果

 更新时间:2021年07月27日 14:54:39   作者:接啊是哦ida  
这篇文章主要为大家详细介绍了Unity排行榜优化滚动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Unity排行榜优化滚动效果的具体代码,供大家参考,具体内容如下

自己做的一个优化排行榜的功能,当有大量的数据需要在scroolRect中可以通过只夹在几个item循环利用便可以展示所需的内容;

下面是效果实现图

下面是我的一个中心思想

通过对处在视野第一个Item左上和左下左边点的位置来判断是将最后一个移动到第一个前面,还是将第一个移动到最后一个后面。

用到的我目前来说不太常用的数据结构 LinkedList 方便用于移除第一个和最后一个;

以下是代码

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

public class WuXianGunDong : MonoBehaviour
{
    List<string> strs = new List<string>();//需要修改

    public int DataTips;

    public Transform content;
    public GameObject loopItemPrefab;

    Vector3[] viewCorners = new Vector3[4];
    float hight;  //生成的loopItem所占的区域
    int current = -1;
    int itemCount;//生成的Item的数量

    public LinkedList<GameObject> loopItems = new LinkedList<GameObject>();

    #region 回调
    private void Awake()
    {
        for (int i = 0; i < DataTips; i++)
        {
            strs.Add(i.ToString());
        }

        hight = loopItemPrefab.GetComponent<RectTransform>().sizeDelta.y + content.GetComponent<VerticalLayoutGroup>().spacing;

        itemCount = (int)(transform.GetComponent<RectTransform>().sizeDelta.y / hight) + 2;

        if (itemCount > DataTips)
            itemCount = DataTips;
        for (int i = 0; i < itemCount; i++)
        {
            GameObject obj = Instantiate(loopItemPrefab, content);
            obj.GetComponentInChildren<Text>().text = strs[i];
            loopItems.AddLast(obj);
            current++;
        }
        transform.GetComponent<RectTransform>().GetWorldCorners(viewCorners);
        content.GetComponent<VerticalLayoutGroup>().enabled = true;
    }

    private void Start()
    {
        Invoke("DisGrid", 0.1f);
    }
    #endregion

    #region 拖拽的时候
    public void OnChange()
    {
        if (DataTips < itemCount)
        {
            return;
        }
        Vector3[] rectCorners = new Vector3[4];
        //当第一个离开视野的时候
        loopItems.First.Value.GetComponent<RectTransform>().GetWorldCorners(rectCorners);
        if (rectCorners[0].y > viewCorners[1].y)
        {
            if (current + 1 < strs.Count)
            {
                current++;
                loopItems.First.Value.transform.GetComponentInChildren<Text>().text = strs[current];
                loopItems.First.Value.GetComponent<RectTransform>().localPosition = loopItems.Last.Value.GetComponent<RectTransform>().localPosition - new Vector3(0, hight, 0);
                loopItems.AddLast(loopItems.First.Value);
                loopItems.RemoveFirst();
            }
        }
        //当最后一个进入视野的时候
        loopItems.First.Value.GetComponent<RectTransform>().GetWorldCorners(rectCorners);
        if (rectCorners[1].y < viewCorners[1].y)
        {
            if (current - itemCount >= 0)
            {
                loopItems.Last.Value.transform.GetChild(0).GetComponent<Text>().text = strs[current - itemCount];
                loopItems.Last.Value.GetComponent<RectTransform>().localPosition = loopItems.First.Value.GetComponent<RectTransform>().localPosition + new Vector3(0, hight, 0);
                loopItems.AddFirst(loopItems.Last.Value);
                loopItems.RemoveLast();
                current--;
            }
        }
    }
    #endregion

    public void DisGrid()
    {
        //关闭LayoutGroup
        content.GetComponent<VerticalLayoutGroup>().enabled = false;
        //设置宽度
        content.GetComponent<RectTransform>().sizeDelta = new Vector2(content.GetComponent<RectTransform>().sizeDelta.x, strs.Count * hight);
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 深入c#工厂模式的详解

    深入c#工厂模式的详解

    本篇文章是对c#中的工厂模式进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • winform多线程组件BackgroundWorker使用

    winform多线程组件BackgroundWorker使用

    这篇文章介绍了winform多线程组件BackgroundWorker的使用方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • C#如何绑定多个按钮到同一个事件

    C#如何绑定多个按钮到同一个事件

    这篇文章主要介绍了C#如何绑定多个按钮到同一个事件,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-04-04
  • 总结的5个C#字符串操作方法分享

    总结的5个C#字符串操作方法分享

    这篇文章主要介绍了总结的5个C#字符串操作方法分享,本文讲解了把字符串按照分隔符转换成 List、把字符串转 按照, 分割 换为数据、得到数组列表以逗号分隔的字符串、得到字符串长度等方法,需要的朋友可以参考下
    2015-05-05
  • C#获取计算机名,IP,MAC信息实现代码

    C#获取计算机名,IP,MAC信息实现代码

    利用C#获取计算机名,IP,MAC信息如何实现,一直是网友们的头疼问题,本文整理了一些实现代码,需要的朋友可以参考下
    2012-11-11
  • c#创建windows服务入门教程实例

    c#创建windows服务入门教程实例

    windows服务是windows系统中一类特殊的应用程序,一般情况下它们只会在后台运行,不会影响前台操作,非常适合做一些不需要用户参与的而又需要长时间执行的任务
    2014-04-04
  • C#找出字符串中第一个字母并大写的方法

    C#找出字符串中第一个字母并大写的方法

    这篇文章主要介绍了C#找出字符串中第一个字母并大写的方法,通过C#面向对象的方式定义了类的成员函数实现字符串转换的功能,需要的朋友可以参考下
    2016-02-02
  • C# 实现简单打印的实例代码

    C# 实现简单打印的实例代码

    C# 实现简单打印的实例代码,需要的朋友可以参考一下
    2013-03-03
  • 使用淘宝ip地址库查ip的示例

    使用淘宝ip地址库查ip的示例

    这篇文章主要介绍了使用淘宝ip地址库查ip的示例,需要的朋友可以参考下
    2014-03-03
  • 详解C#调用matlab生成的dll库

    详解C#调用matlab生成的dll库

    这篇文章主要介绍了C#调用matlab生成的dll库,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-09-09

最新评论