ScrollVIEW 2000個ITEM不會卡

來源:互聯網
上載者:User

標籤:blog   io   os   ar   for   art   div   cti   sp   

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);                }            }        }    }}

  

ScrollVIEW 2000個ITEM不會卡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.