ScrollVIEW 2000个ITEM不会卡

本文介绍了一个自定义的 Unity 脚本 UIWrapGrid,该组件用于管理 UI 元素在 UIScrollView 中的显示状态。它能确保只有在可视区域内的 UI 子元素保持激活状态,以提高渲染效率。

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

</pre><pre name="code" class="csharp">using UnityEngine;
using System.Collections.Generic;
 
public class UIWrapGrid : MonoBehaviour
{
    Transform mTrans;
    UIPanel mPanel;
    UIScrollView mScroll;
    bool mHorizontal = false;
    bool mFirstTime = true;
    List<Transform> mChildren = new List<Transform>();
 
    /// <summary>
    /// Initialize everything and register a callback with the UIPanel to be notified when the clipping region moves.
    /// </summary>
 
    protected virtual void Start () {
        InitGrid();
        mFirstTime = false;
    }
 
    /// <summary>
    /// Cache the scroll view and return 'false' if the scroll view is not found.
    /// </summary>
    public void InitGrid() {
        mTrans = transform;
        mPanel = NGUITools.FindInParents<UIPanel>(gameObject);
        mScroll = mPanel.GetComponent<UIScrollView>();
 
        if (mScroll != null) {
            mScroll.GetComponent<UIPanel>().onClipMove = OnMove;
        }
 
        mChildren.Clear();
        for (int i = 0; i < mTrans.childCount; ++i)
            mChildren.Add(mTrans.GetChild(i));
 
        // Sort the list of children so that they are in order
        mChildren.Sort(UIGrid.SortByName);
 
        if (mScroll == null) return;
        if (mScroll.movement == UIScrollView.Movement.Horizontal) mHorizontal = true;
        else if (mScroll.movement == UIScrollView.Movement.Vertical) mHorizontal = false;
 
        WrapContent();
    }
 
    /// <summary>
    /// Callback triggered by the UIPanel when its clipping region moves (for example when it's being scrolled).
    /// </summary>
 
    protected virtual void OnMove (UIPanel panel) { WrapContent(); }
 
    void WrapContent() {
        Vector3[] corners = mPanel.worldCorners;
 
        for (int i = 0; i < 4; ++i) {
            Vector3 v = corners[i];
            v = mTrans.InverseTransformPoint(v);
            corners[i] = v;
        }
        Vector3 center = Vector3.Lerp(corners[0], corners[2], 0.5f);
 
        if (mHorizontal) {  //横向
            for (int i = 0, imax = mChildren.Count; i < imax; ++i) {
                Transform t = mChildren[i];
                float distance = t.localPosition.x - center.x;
                float min = corners[0].x - 100;
                float max = corners[2].x + 100;
 
                distance += mPanel.clipOffset.x - mTrans.localPosition.x;
                if (!UICamera.IsPressed(t.gameObject)) {
                    NGUITools.SetActive(t.gameObject, (distance > min && distance < max), false);
                }
            }
        } else {            //竖向
            for (int i = 0, imax = mChildren.Count; i < imax; ++i) {
                Transform t = mChildren[i];
                float distance = t.localPosition.y - center.y;
                float min = corners[0].y - 100;
                float max = corners[2].y + 100;
 
                distance += mPanel.clipOffset.y - mTrans.localPosition.y;
                if (!UICamera.IsPressed(t.gameObject)) {
                    bool active = t.gameObject.activeSelf;
                    bool willactive = distance > min && distance < max;
                    if (active == willactive) continue;
                    NGUITools.SetActive(t.gameObject, willactive, false);
                }
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值