zoukankan      html  css  js  c++  java
  • ScrollVIEW 2000个ITEM不会卡

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

      

  • 相关阅读:
    【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)
    【POJ 2152】 Fire (树形DP)
    【POJ 1741】 Tree (树的点分治)
    【POJ 2486】 Apple Tree (树形DP)
    【HDU 3810】 Magina (01背包,优先队列优化,并查集)
    【SGU 390】Tickets (数位DP)
    【SPOJ 2319】 BIGSEQ
    【SPOJ 1182】 SORTBIT
    【HDU 5456】 Matches Puzzle Game (数位DP)
    【HDU 3652】 B-number (数位DP)
  • 原文地址:https://www.cnblogs.com/hersen/p/3964942.html
Copyright © 2011-2022 走看看