Esempio n. 1
0
        protected MassivePickerItem AcquireItemObject()
        {
            if (m_ItemPool.Count > 0)
            {
                MassivePickerItem item = m_ItemPool.Pop();

                if (item != null)
                {
                    return(item);
                }
            }


            GameObject itemObject = Util.Instantiate(scrollRect.itemSource, rectTransform);

            itemObject.SetActive(false);
            MassivePickerItem pickerItem = itemObject.GetComponent <MassivePickerItem>();

            RectTransform itemTransform = pickerItem.rectTransform;

            itemTransform.localPosition = Vector3.zero;
            itemTransform.localRotation = Quaternion.identity;
            itemTransform.localScale    = Vector3.one;

            itemObject.SetActive(true);

            return(pickerItem);
        }
Esempio n. 2
0
 public void ScrollTo(MassivePickerItem item, bool immdiate = false)
 {
     if (item != null && pickerLayoutGroup != null)
     {
         float position = pickerLayoutGroup.IndexToPosition(item.itemIndex);
         ScrollTo(position, immdiate);
     }
 }
Esempio n. 3
0
        public void UpdateAllItemContent()
        {
            foreach (var kv in m_Items)
            {
                int index = kv.Key;
                MassivePickerItem item = kv.Value;

                if (item != null)
                {
                    item.SetItemContents(scrollRect, index);
                }
            }
        }
Esempio n. 4
0
 protected void ReleaseItemObject(MassivePickerItem item)
 {
     m_ItemPool.Push(item);
     item.rectTransform.localScale = Vector3.zero;
 }
Esempio n. 5
0
        protected void SetLayout(int axis)
        {
            if (scrollRect == null || scrollRect.layout != (RectTransform.Axis)axis || !Application.isPlaying)
            {
                return;
            }

            int itemCount = this.itemCount;

            if (itemCount <= 0 || scrollRect.itemSource == null)
            {
                foreach (MassivePickerItem item in m_Items.Values)
                {
                    ReleaseItemObject(item);
                }

                m_Items.Clear();
                return;
            }

            if (m_ChangedItemSource)
            {
                ClearAlreadyItems();
                m_ChangedItemSource = false;
            }

            RectTransform rectTransform = this.rectTransform;

            Vector2 position = rectTransform.anchoredPosition;
            float   itemSize = m_ItemSize[axis];
            float   spacing  = this.spacing;

            Vector2 windowSize = scrollRect.windowSize;


            bool          wheel         = scrollRect.wheelEffect;
            float         direction     = (axis == (int)RectTransform.Axis.Horizontal ? 1f : -1f);
            WheelEffect3D wheelEffect3D = GetComponentInParent <WheelEffect3D>();

            if (wheelEffect3D != null)
            {
                if (wheelEffect3D.IsActive() && (int)wheelEffect3D.layout == axis)
                {
                    wheel = true;
                }
                else
                {
                    wheelEffect3D = null;
                }
            }

            Vector2 areaSize = rectTransform.sizeDelta;

            areaSize[axis] = scrollRect.windowSize[axis] + (itemSize + spacing) * (itemCount - 1);
            if (rectTransform.sizeDelta != areaSize)
            {
                rectTransform.sizeDelta = areaSize;
            }

            int beginIndex = 0, endIndex = itemCount - 1;

            if (itemCount >= 3)
            {
                float range       = !wheel ? windowSize[axis] * 0.5f : windowSize[axis] * 0.25f * Mathf.PI;
                int   centerIndex = PositionToNearItemIndex(-position[axis]);
                int   indexRange  = Mathf.CeilToInt(range / (itemSize + spacing));
                indexRange = Mathf.Min(indexRange, itemCount / 2);

                if (indexRange + indexRange + 1 >= itemCount)
                {
                    beginIndex = 0;
                    endIndex   = itemCount - 1;
                }
                else
                {
                    if (scrollRect.infiniteScroll)
                    {
                        beginIndex = (centerIndex - indexRange + itemCount) % itemCount;
                        endIndex   = (centerIndex + indexRange + itemCount) % itemCount;

                        if (beginIndex == endIndex)
                        {
                            beginIndex = 0;
                            endIndex   = itemCount - 1;
                        }
                    }
                    else
                    {
                        beginIndex = System.Math.Max(0, centerIndex - indexRange);
                        endIndex   = System.Math.Min(itemCount - 1, centerIndex + indexRange);
                    }
                }
            }
            else
            {
                beginIndex = 0;
                endIndex   = itemCount - 1;
            }

            ShrinkItem(beginIndex, endIndex);

            float scrollOffset = position[axis];
            float pivot        = m_ChildPivot - 0.5f;

            if (!wheel)
            {
                while (true)
                {
                    MassivePickerItem item         = GetItem(beginIndex);
                    RectTransform     itemRect     = item.rectTransform;
                    Vector3           itemPosition = itemRect.localPosition;

                    itemPosition[axis] = -IndexToPosition(beginIndex);

                    Vector2 center        = itemRect.rect.center;
                    float   itemPivot     = itemRect.pivot[1 - axis] - 0.5f;
                    float   itemAnchorMin = itemRect.anchorMin[1 - axis];
                    float   itemAnchorMax = itemRect.anchorMax[1 - axis];
                    float   revision      = (itemPivot * (itemAnchorMax - itemAnchorMin) + (itemAnchorMax + itemAnchorMin - 1) * 0.5f) * windowSize[1 - axis];
                    itemPosition[1 - axis] = (windowSize[1 - axis] - itemRect.rect.size[1 - axis]) * pivot - center[1 - axis] - revision;
                    itemPosition.z         = 0;

                    itemRect.localPosition = itemPosition;

                    if (itemRect.localScale != Vector3.one)
                    {
                        itemRect.localScale = Vector3.one;
                    }

                    if (itemRect.localRotation != Quaternion.identity)
                    {
                        itemRect.localRotation = Quaternion.identity;
                    }

                    if (beginIndex == endIndex)
                    {
                        break;
                    }
                    beginIndex = (beginIndex + 1) % itemCount;
                }
            }
            else
            {
                float perspective   = m_ScrollRect.wheelPerspective;
                float wheelRadius   = windowSize[axis] * 0.5f;
                float circumference = (wheelRadius + wheelRadius) * Mathf.PI;
                float offsetToRad   = (Mathf.PI + Mathf.PI) / circumference * -direction;

                while (true)
                {
                    MassivePickerItem item     = GetItem(beginIndex);
                    RectTransform     itemRect = item.rectTransform;

                    float rad = (IndexToPosition(beginIndex) - scrollOffset) * offsetToRad;

                    if (Mathf.Abs(rad) < Mathf.PI * 0.5f)
                    {
                        if (wheelEffect3D == null)
                        {
                            float scale  = Mathf.Cos(rad);
                            float scale2 = (1 - perspective) + scale * perspective;

                            Vector3 localScale = itemRect.localScale;
                            localScale[axis]     = scale;
                            localScale[1 - axis] = scale2;
                            itemRect.localScale  = localScale;

                            Vector3 itemPosition = itemRect.anchoredPosition;
                            Vector2 center       = itemRect.rect.center;
                            itemPosition[axis] = Mathf.Sin(-rad) * wheelRadius * -direction - scrollOffset - center[axis] * scale;

                            float itemPivot     = itemRect.pivot[1 - axis] - 0.5f;
                            float itemAnchorMin = itemRect.anchorMin[1 - axis];
                            float itemAnchorMax = itemRect.anchorMax[1 - axis];
                            float revision      = (itemPivot * (itemAnchorMax - itemAnchorMin) + (itemAnchorMax + itemAnchorMin - 1) * 0.5f) * windowSize[1 - axis];
                            itemPosition[1 - axis] = (windowSize[1 - axis] - itemRect.rect.size[1 - axis] * scale2) * pivot - center[1 - axis] * scale2 - revision;

                            itemPosition.z = 0;

                            itemRect.localPosition = itemPosition;

                            if (itemRect.localRotation != Quaternion.identity)
                            {
                                itemRect.localRotation = Quaternion.identity;
                            }
                        }
                        else
                        {
                            Vector3 itemPosition = itemRect.anchoredPosition;
                            Vector2 center       = itemRect.rect.center;
                            itemPosition[axis] = Mathf.Sin(-rad) * wheelRadius * -direction - scrollOffset - center[axis];

                            Vector3 eulerAngles = itemRect.localRotation.eulerAngles;
                            eulerAngles[1 - axis]  = rad * -Mathf.Rad2Deg * direction;
                            itemRect.localRotation = Quaternion.Euler(eulerAngles);

                            float itemPivot     = itemRect.pivot[1 - axis] - 0.5f;
                            float itemAnchorMin = itemRect.anchorMin[1 - axis];
                            float itemAnchorMax = itemRect.anchorMax[1 - axis];
                            float revision      = (itemPivot * (itemAnchorMax - itemAnchorMin) + (itemAnchorMax + itemAnchorMin - 1) * 0.5f) * windowSize[1 - axis];
                            itemPosition[1 - axis] = (windowSize[1 - axis] - itemRect.rect.size[1 - axis]) * pivot - center[1 - axis] - revision;

                            itemPosition.z = wheelRadius - Mathf.Cos(rad) * wheelRadius;

                            itemRect.localPosition = itemPosition;

                            if (itemRect.localScale != Vector3.one)
                            {
                                itemRect.localScale = Vector3.one;
                            }
                        }
                    }
                    else
                    {
                        Vector3 scale = itemRect.localScale;

                        if (scale.x != 0 || scale.y != 0)
                        {
                            scale.x             = 0;
                            scale.y             = 0;
                            itemRect.localScale = scale;
                        }
                    }

                    if (beginIndex == endIndex)
                    {
                        break;
                    }
                    beginIndex = (beginIndex + 1) % itemCount;
                }
            }

            if (scrollRect != null && Application.isPlaying)
            {
                scrollRect.SetInitialPosition();
            }
        }
Esempio n. 6
0
        public GameObject GetSelectedItem()
        {
            MassivePickerItem item = GetSelectedPickerItem();

            return(item != null ? item.gameObject : null);
        }