Esempio n. 1
0
        public static FGLayPosition Create(float spaceDis, bool isVertical = false, int groupNum = 1, float groupDis = 0)
        {
            FGLayPosition lp = new FGLayPosition();

            lp.SetSpace(spaceDis).SetVerical(isVertical).SetGroup(groupNum, groupDis);
            return(lp);
        }
Esempio n. 2
0
        private void SetStart(int count, int startGroup = 0)
        {
            mScrollRect = this.gameObject.GetComponent <ScrollRect>();
            if (mScrollRect != null)
            {
                if (mContent == null)
                {
                    //初始化
                    mContent = mScrollRect.content;

                    if (mComponent == null)
                    {
                        mComponent = mContent.GetChild(0);
                    }
                    mStartContenPos = mContent.transform.localPosition;
                    mStartItemPos   = mComponent.transform.position;
                    //锚点置顶,防止拖动范围变大时,位置发生变化
                    var tempRect = mComponent.GetComponent <RectTransform>();
                    tempRect.anchorMin            = new Vector2(0, 1);
                    tempRect.anchorMax            = new Vector2(0, 1);
                    mComponent.transform.position = mStartItemPos;
                    mStartItemPos = mComponent.transform.localPosition;
                    m_Rect        = new Vector2(tempRect.sizeDelta.x, tempRect.sizeDelta.y);

                    var tempScroll = mScrollRect.GetComponent <RectTransform>();
                    m_CenterPosion = Vector3.zero;
                    m_IsVertical   = mScrollRect.vertical;
                    mGroups        = new FGroup <Transform>();
                    mGroups.Init(mComponent.transform);
                    var tempPos = mContent.transform.position;
                    tempPos = mScrollRect.transform.InverseTransformPoint(tempPos);
                    if (m_IsVertical)
                    {
                        m_SpaceDis       = m_Rect.y;
                        m_GroupDis       = m_Rect.x;
                        m_CenterPosion.x = tempPos.x;
                    }
                    else
                    {
                        m_SpaceDis       = m_Rect.x;
                        m_GroupDis       = m_Rect.y;
                        m_CenterPosion.y = tempPos.y;
                    }

                    if (m_IsCenter)
                    {
                        var offset = mScrollRect.transform.TransformPoint(m_CenterPosion);
                        offset = mContent.transform.InverseTransformPoint(offset);
                        m_DefaultCenterOffset = (m_IsVertical ? (offset.y - mStartItemPos.y) : (offset.x - mStartItemPos.x));
                    }

                    m_GLayPosition = FGLayPosition.Create(m_SpaceDis, m_IsVertical, m_GroupNum, m_GroupDis);
                    m_GLayPosition.SetStart(mStartItemPos);

                    if (mModeType == ScrollType.Repeat)
                    {
                        m_UpdateFun = UpdateRepeat;
                    }
                    else if (mModeType == ScrollType.Asy)
                    {
                        m_UpdateFun = UpdateAsy;
                    }
                    else if (mModeType == ScrollType.Circle)
                    {
                        m_UpdateFun = UpdateCircle;
                        mScrollRect.movementType = ScrollRect.MovementType.Unrestricted;
                    }
                    else
                    {
                        m_UpdateFun = UpdateNull;
                    }
                }
                else
                {
                    //复位
                    mContent.transform.localPosition = mStartContenPos;
                }
                m_LastIndex       = -99999;//重置
                m_LastCenterGroup = -99999;
                //配置滑动长度
                var       scrollTransform = mScrollRect.GetComponent <RectTransform>();
                Vector3[] sizeVector3     = new Vector3[4];
                scrollTransform.GetLocalCorners(sizeVector3);
                float sizeX = Mathf.Abs(sizeVector3[0].x - sizeVector3[2].x);
                float sizeY = Mathf.Abs(sizeVector3[0].y - sizeVector3[2].y);

                if (m_IsVertical)
                {
                    if (m_CacheLine <= 1)
                    {
                        SetCache(Mathf.Clamp((int)(sizeY / m_SpaceDis) + 2, 4, 999999));
                    }
                    mContent.sizeDelta = new Vector2(mContent.sizeDelta.x, m_SpaceDis * (1 + (count - 1) / m_GroupNum) + m_SpaceOffset);
                }
                else
                {
                    if (m_CacheLine <= 1)
                    {
                        SetCache(Mathf.Clamp((int)(sizeX / m_SpaceDis) + 2, 4, 999999));
                    }
                    mContent.sizeDelta = new Vector2(m_SpaceOffset + m_SpaceDis * (1 + (count - 1) / m_GroupNum), mContent.sizeDelta.y);
                }
                m_ActionNum = m_GroupNum * m_CacheLine;
                int rNum = mModeType == ScrollType.Circle ? m_ActionNum : Mathf.Min(m_ActionNum, count);
                mGroups.Refurbish(rNum);
                mItems.Clear();
                foreach (var k in mGroups)
                {
                    ScrollItem sItem = new ScrollItem(k.gameObject);
                    mItems.Add(sItem);
                }
                m_MaxNum  = count;
                m_MaxLine = 1 + (count - 1) / m_GroupNum;
                SelectGroup(startGroup);
                if (m_ScrollEx != null)
                {
                    m_ScrollEx.Init(this);
                }
            }
            else
            {
                Debug.LogError("ScrollRect未找到");
            }
        }