Beispiel #1
0
        void SetDataForItemAtIndex(ScrollItem item, int index)
        {
            switch (dataType)
            {
            case DataSourceType.CS:
                if (updateFuncCS != null && csData != null)
                {
                    updateFuncCS(item, csData[index]);
                }
                break;

#if USE_LUA_FRAMEWORK
            case DataSourceType.Lua:
                if (updateFuncLua != null && luaData != null)
                {
                    updateFuncLua.Call(item, luaData[index + 1]);
                }
                break;
#endif
            default:
                break;
            }

            SetPosForItemAtIndex(item, index);
        }
Beispiel #2
0
        void SetPosForItemAtIndex(ScrollItem item, int index)
        {
            Rect r = managedItems[index].rect;

            item.rectTransform.localPosition = r.position;
            item.rectTransform.sizeDelta     = r.size;
        }
Beispiel #3
0
        void InitPool()
        {
            GameObject poolNode = new GameObject("POOL");

            poolNode.SetActive(false);
            poolNode.transform.SetParent(transform, false);
            itemPool = new SimpleObjPool <ScrollItem>(
                poolSize,
                (ScrollItem item) => {
                if (recycleFunc != null)
                {
                    recycleFunc(item);
                }
                item.transform.SetParent(poolNode.transform, false);
            },
                () => {
                GameObject itemObj = Instantiate(itemTemplate.gameObject);
                ScrollItem item    = itemObj.GetComponent <ScrollItem>();
                itemObj.transform.SetParent(poolNode.transform, false);
                RectTransform rectTrans = item.rectTransform;

                rectTrans.anchorMin = Vector2.up;
                rectTrans.anchorMax = Vector2.up;
                rectTrans.pivot     = Vector2.zero;

                //rectTrans.pivot = Vector2.up;
                return(item);
            });
        }
Beispiel #4
0
        void CheckAndHideItem(int criticalItemType)
        {
            ScrollItem item          = null;
            int        criticalIndex = -1;

            while (true)
            {
                item          = GetCriticalItem(criticalItemType);
                criticalIndex = criticalItemIndex[criticalItemType];
                if (item != null && !ShouldItemSeenAtIndex(criticalIndex))
                {
                    itemPool.Recycle(item);
                    managedItems[criticalIndex].item = null;
                    //Debug.Log("回收了 " + criticalIndex);
                    criticalItemIndex[criticalItemType + 2] = criticalIndex;
                    if (criticalItemType == CriticalItemType.UpToHide)
                    {
                        // 最上隐藏了一个
                        criticalItemIndex[criticalItemType]++;
                    }
                    else
                    {
                        // 最下隐藏了一个
                        criticalItemIndex[criticalItemType]--;
                    }
                    criticalItemIndex[criticalItemType] = Mathf.Clamp(criticalItemIndex[criticalItemType], 0, m_dataCount - 1);
                }
                else
                {
                    break;
                }
            }
        }
Beispiel #5
0
 void OnGetItemForDataIndex(ScrollItem item, int index)
 {
     if (activateFunc != null)
     {
         activateFunc(item);
     }
     SetDataForItemAtIndex(item, index);
     item.transform.SetParent(content, false);
 }
Beispiel #6
0
        void CheckAndShowItem(int criticalItemType)
        {
            ScrollItem item          = null;
            int        criticalIndex = -1;

            while (true)
            {
                item          = GetCriticalItem(criticalItemType);
                criticalIndex = criticalItemIndex[criticalItemType];

                //if (item == null && ShouldItemFullySeenAtIndex(criticalItemIndex[criticalItemType - 2]))

                if (item == null && ShouldItemSeenAtIndex(criticalIndex))
                {
                    ScrollItem newItem = itemPool.Get();
                    OnGetItemForDataIndex(newItem, criticalIndex);
                    //Debug.Log("创建了 " + criticalIndex);
                    managedItems[criticalIndex].item = newItem;

                    criticalItemIndex[criticalItemType - 2] = criticalIndex;

                    if (criticalItemType == CriticalItemType.UpToShow)
                    {
                        // 最上显示了一个
                        criticalItemIndex[criticalItemType]--;
                    }
                    else
                    {
                        // 最下显示了一个
                        criticalItemIndex[criticalItemType]++;
                    }
                    criticalItemIndex[criticalItemType] = Mathf.Clamp(criticalItemIndex[criticalItemType], 0, m_dataCount - 1);
                }
                else
                {
                    break;
                }
            }
        }
Beispiel #7
0
        void UpdateManagedItems(bool resize)
        {
            if (resize)
            {
#if USE_LUA_FRAMEWORK
                int newDataCount = (dataType == DataSourceType.CS ? csData.Length : luaData.Length);
#else
                int newDataCount = csData.Count;
#endif
                if (newDataCount > managedItems.Length)  //增加
                {
                    Array.Resize(ref managedItems, newDataCount);
                }
                else //减少
                {
                    for (int i = newDataCount; i < m_dataCount; i++)
                    {
                        if (managedItems[i].item != null)
                        {
                            itemPool.Recycle(managedItems[i].item);
                            managedItems[i].item = null;
                        }
                    }
                    //Array.Resize(ref managedItems, newDataCount); //减少时保留之前多余的空位
                }
                m_dataCount = newDataCount;
            }

            CacheRect();

            int showCount = Mathf.Min(maxShownCount, m_dataCount);
            int restCount = showCount;

            bool hasItem, shouldShow;
            int  firstIndex = -1, lastIndex = -1;
            for (int i = 0; i < m_dataCount && restCount > 0; i++)
            {
                hasItem    = managedItems[i].item != null;
                shouldShow = ShouldItemSeenAtIndex(i);

                if (shouldShow)
                {
                    restCount--;
                    if (firstIndex == -1)
                    {
                        firstIndex = i;
                    }
                    lastIndex = i;
                }

                if (hasItem && shouldShow)
                {
                    // 应显示且已显示
                    SetDataForItemAtIndex(managedItems[i].item, i);
                    continue;
                }

                if (hasItem == shouldShow)
                {
                    // 不应显示且未显示
                    continue;
                }

                if (hasItem && !shouldShow)
                {
                    // 不该显示 但是有
                    itemPool.Recycle(managedItems[i].item);
                    managedItems[criticalItemIndex[i]].item = null;
                    continue;
                }

                if (shouldShow && !hasItem)
                {
                    // 需要显示 但是没有
                    ScrollItem item = itemPool.Get();
                    OnGetItemForDataIndex(item, i);
                    managedItems[i].item = item;
                    continue;
                }
            }

            // content.localPosition = Vector2.zero;
            criticalItemIndex[CriticalItemType.UpToHide]   = firstIndex;
            criticalItemIndex[CriticalItemType.DownToHide] = lastIndex;
            criticalItemIndex[CriticalItemType.UpToShow]   = Mathf.Max(firstIndex - 1, 0);
            criticalItemIndex[CriticalItemType.DownToShow] = Mathf.Min(lastIndex + 1, m_dataCount - 1);
        }