欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

unity实现无限列表功能

程序员文章站 2023-11-14 14:57:46
本文实例为大家分享了unity实现无限列表功能的具体代码,供大家参考,具体内容如下public static class recttransformextensions { public stati...

本文实例为大家分享了unity实现无限列表功能的具体代码,供大家参考,具体内容如下

public static class recttransformextensions
 {
  public static bool overlaps(this recttransform a, recttransform b)
  {
   return a.worldrect().overlaps(b.worldrect());
  }
  public static bool overlaps(this recttransform a, recttransform b, bool allowinverse)
  {
   return a.worldrect().overlaps(b.worldrect(), allowinverse);
  }
 
  public static rect worldrect(this recttransform recttransform)
  {
   vector2 sizedelta = recttransform.sizedelta;
   float recttransformwidth = sizedelta.x * recttransform.lossyscale.x;
   float recttransformheight = sizedelta.y * recttransform.lossyscale.y;
 
   vector3 position = recttransform.position;
 
   return new rect(
    position.x - recttransformwidth * recttransform.pivot.x,
    position.y - recttransformheight * recttransform.pivot.y,
    recttransformwidth,
    recttransformheight);
  }
  /// <summary>
  /// 
  /// </summary>
  /// <param name="recttransform"></param>
  /// <param name="pos">世界坐标的position</param>
  /// <returns></returns>
  public static rect worldrect2(this recttransform recttransform, vector3 pos)
  {
   rect rect = new rect();
   vector2 sizedelta = recttransform.sizedelta;
   float recttransformwidth = sizedelta.x * recttransform.lossyscale.x;
   float recttransformheight = sizedelta.y * recttransform.lossyscale.y;
 
   vector3 position = pos;
 
   rect.x = position.x - recttransformwidth * recttransform.pivot.x;
   rect.y = position.y - recttransformheight * recttransform.pivot.y;
   rect.width = recttransformwidth;
   rect.height = recttransformheight;
 
   return rect;
  }
 }

以上拓展方法是判断两个recttransform类型的物体是否相交。

然后scrollrec的滑动回调方法中更新ui位置,代码如下

private void onscrollrectvaluechanged(vector2 arg0)
  {
   dictionary<int, dynamicrect> inoverlaps = new dictionary<int, dynamicrect>();
 
   mrectmask = 遮罩物体的recttransform.worldrect();
   //m_dynamicrectdic这个字典保存的是你所有ui需要放置的位置数据,
   //判断所有ui哪个是可见哪个不可见 ,保存起来
   foreach (dynamicrect dr in m_dynamicrectdic.values)
   {
    tmptra.localposition = dr.localpos;
    //获取每个位置ui的世界坐标rect
    rect rect = m_levelitemprefabrt.worldrect2(tmptra.position);
    if (rect.overlaps(mrectmask))
    {
     inoverlaps.add(dr.index, dr);
    }
   }
   //m_levelitemlist是保存你实例化后的ui列表,比如你这个遮罩页面最多显示3个ui,你需要实例化4个ui,然后动态修改gameobject的显示与隐藏
   int len = m_levelitemlist.count;
   for (int i = 0; i < len; ++i)
   {
    //levelitem是ui上挂载的脚本,用于更新ui界面的显示和数据存储的
    levelitem item = m_levelitemlist[i];
    if (item.drect != null && !inoverlaps.containskey(item.drect.index))
    {
     //item的drect为null时,隐藏物体,否则显示物体
     item.drect = null;
    }
   }
   //判断哪些可以重复利用的ui,然后赋予新的数据与位置
   foreach (dynamicrect dr in inoverlaps.values)
   {
    if (getdynmicitem(dr) == null)
    {
     levelitem item = getnulldynmicitem();
 
     if (item == null) continue;
 
     item.drect = dr;
     //更新ui的位置和显示(自己计算,每种显示不一样)
     _updatechildtransformpos(item.gameobject, dr.index);
    }
   }
  }
 
  /// <summary>
  /// 通过动态格子获得动态渲染器
  /// </summary>
  /// <param name="rect"></param>
  /// <returns></returns>
  private levelitem getdynmicitem(dynamicrect rect)
  {
   int len = m_levelitemlist.count;
   for (int i = 0; i < len; ++i)
   {
    levelitem item = m_levelitemlist[i];
    if (item.drect == null)
     continue;
    if (rect.index == item.drect.index)
     return item;
   }
   return null;
  }
  /// <summary>
  /// 获得待渲染的渲染器
  /// </summary>
  /// <returns></returns>
  private levelitem getnulldynmicitem()
  {
   int len = m_levelitemlist.count;
   for (int i = 0; i < len; ++i)
   {
    levelitem item = m_levelitemlist[i];
    if (item.drect == null)
     return item;
   }
 
   return null;
  }
 
 public class dynamicrect
 {
  /// <summary>
  /// 本地坐标
  /// </summary>
  public vector3 localpos;
  /// <summary>
  /// 格子索引
  /// </summary>
  public int index;
 
  public dynamicrect(int index, vector3 localpos)
  {
   this.index = index;
   this.localpos = localpos;
  }
 }

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