unity 优化 ScrollView

本文探讨了在Unity中优化ScrollView的策略,包括减少重绘、优化渲染对象池和使用Fixed Layout Group等方法,旨在提升游戏UI性能并降低资源消耗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using DG.Tweening;
using XLua;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 子物体横向移动左上对齐,纵向移动左上对齐
/// </summary>
public class ScrollableAreaController : MonoBehaviour
{
   
   
    public ScrollableCell cellPrefab;
    private RectTransform content;
    private RectTransform viewport;
    [SerializeField] private int NUMBER_OF_COLUMNS = 1;  //表示并排显示几个,比如是上下滑动,当此处为2时表示一排有两个cell
    private float cellWidth;
    private float cellHeight;
    [SerializeField] private float cellSpace = 0f; //cell的间距
    [SerializeField] private float gridSpace = 0f;//组间距
    [SerializeField] private bool isInitAnim = false;//生成动画
    private int visibleCellsTotalCount = 0;
    private int visibleCellsRowCount = 0;
    private LinkedList<GameObject> localCellsPool = new LinkedList<GameObject>(); //备用cell
    private LinkedList<GameObject> cellsInUse = new LinkedList<GameObject>(); //被用到的cell
    private ScrollRect rect;

    //private IList allCellsData ;
    private int cellDataCount = 0; //列表数据长度
    private int previousInitialIndex = 0; //先前第一个cell的序号
    private int initialIndex = 0;  //当前第一个cell的序号
    private float initpostion = 0;
   
    //下拉刷新
    private Action mTopAction = null;
    private bool needTopRefresh = false;
    //上拉刷新
    private Action mBottomAction = null;
    private bool needBotRefresh = false;

    private LuaTable mLuaTable = null;

    private bool isShow = false;

    public void PreInit()
    {
   
   
        rect = GetComponent<ScrollRect>();
        content = rect.content;
        viewport = rect.viewport;

        if (NUMBER_OF_COLUMNS == 0) NUMBER_OF_COLUMNS = 1;
        if (NUMBER_OF_COLUMNS == 1) gridSpace = 0;

        RectTransform cell = cellPrefab.GetComponent<RectTransform>();
        cellWidth = cell.sizeDelta.x;
        cellHeight = cell.sizeDelta.y;

        content.anchorMin = Vector2.up;
        content.anchorMax = Vector2.up;
        content.pivot = Vector2.up;
        cell.anchorMin = Vector2.up;
        cell.anchorMax = Vector2.up;
        cell.pivot = Vector2.up;
    }
    public void Init(LuaTable luatable)
    {
   
   
        PreInit();

        content.anchoredPosition = Vector2.zero;
        if (horizontal)        
            visibleCellsRowCount = Mathf.CeilToInt(viewport.rect.size.x / (cellWidth + cellSpace));
        else
            visibleCellsRowCount = Mathf.CeilToInt(viewport.rect.size.y / (cellHeight + cellSpace));
        
        visibleCellsTotalCount = visibleCellsRowCount + 1;
        visibleCellsTotalCount *= NUMBER_OF_COLUMNS;

        mLuaTable = luatable;
        CreateCellPool();
    }

    public void AddPageCallback(Action topAction,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值