Exemplo n.º 1
0
        public void Initiate(DynamicScrollRect scrollRect, IList <T> infoList, int startIndex, GameObject objReference, bool createMoreIfNeeded = true, int?forceAmount = null)
        {
            ScrollRect = scrollRect;
            if (ScrollRect == null)
            {
                throw new Exception("No scroll rect in gameObject.");
            }

            if (objReference == null)
            {
                throw new Exception("No Reference GameObject setted.");
            }

            this.infoList = infoList;

            ScrollRect.onValueChanged.AddListener(OnScroll);
            ScrollRect.onBeginDrag.AddListener(OnBeginDrag);
            ScrollRect.onEndDrag.AddListener(OnEndDrag);
            ScrollRect.onStopMoving.AddListener(OnStopMoving);

            mMovementType = ScrollRect.movementType;
            ScrollRect.realMovementType = ScrollRect.movementType;
            ScrollRect.movementType     = UnityEngine.UI.ScrollRect.MovementType.Unrestricted;

            if (ScrollRect.content.GetComponent <VerticalLayoutGroup>() != null)
            {
                mVerticalLayoutGroup         = ScrollRect.content.GetComponent <VerticalLayoutGroup>();
                mVerticalLayoutGroup.spacing = spacing;
            }

            if (ScrollRect.content.GetComponent <HorizontalLayoutGroup>() != null)
            {
                mHorizontalLayoutGroup         = ScrollRect.content.GetComponent <HorizontalLayoutGroup>();
                mHorizontalLayoutGroup.spacing = spacing;
            }

            if (ScrollRect.content.GetComponent <GridLayoutGroup>() != null)
            {
                mGridLayoutGroup         = ScrollRect.content.GetComponent <GridLayoutGroup>();
                mGridLayoutGroup.spacing = new Vector2(spacing, spacing);
            }

            if (ScrollRect.content.GetComponent <ContentSizeFitter>() != null)
            {
                mContentSizeFitter = ScrollRect.content.GetComponent <ContentSizeFitter>();
            }

            mIsHorizontal = ScrollRect.horizontal;
            mIsVertical   = ScrollRect.vertical;

            objectPool.createMoreIfNeeded = createMoreIfNeeded;
            objectPool.Initialize(forceAmount ?? 0, objReference, ScrollRect.content);

            CreateList(startIndex);

            objectPool.ForEach(x =>
            {
                x.SetRefreshListAction(RefreshPosition);
                x.OnObjectIsNotCentralized();
            });

            SetCentralizedObject();
            DisableGridComponents();

            if (!mIsHorizontal || !mIsVertical)
            {
                return;
            }
            Debug.LogError("DynamicScroll doesn't support scrolling in both directions, please choose one direction (horizontal or vertical)");
            mIsHorizontal = false;
        }
Exemplo n.º 2
0
        public void Initiate(ScrollRect scrollRect, T[] infoList, int startIndex, GameObject objReference, int initialAmount, bool createMoreIfNeeded = true)
        {
            mScrollRect = scrollRect;
            if (mScrollRect == null)
            {
                throw new Exception("No scroll rect in gameObject.");
            }

            if (objReference == null)
            {
                throw new Exception("No Reference GameObject setted.");
            }

            mInitialAmount = initialAmount;

            mInfoList = infoList;
            var currentIndex = startIndex;

            mScrollRect.onValueChanged.AddListener(OnScroll);
            mScrollRect.movementType = ScrollRect.MovementType.Unrestricted;

            if (mScrollRect.content.GetComponent <VerticalLayoutGroup>() != null)
            {
                mVerticalLayoutGroup         = mScrollRect.content.GetComponent <VerticalLayoutGroup>();
                mVerticalLayoutGroup.spacing = spacing;
            }

            if (mScrollRect.content.GetComponent <HorizontalLayoutGroup>() != null)
            {
                mHorizontalLayoutGroup         = mScrollRect.content.GetComponent <HorizontalLayoutGroup>();
                mHorizontalLayoutGroup.spacing = spacing;
            }

            if (mScrollRect.content.GetComponent <GridLayoutGroup>() != null)
            {
                mGridLayoutGroup         = mScrollRect.content.GetComponent <GridLayoutGroup>();
                mGridLayoutGroup.spacing = new Vector2(spacing, spacing);
            }

            if (mScrollRect.content.GetComponent <ContentSizeFitter>() != null)
            {
                mContentSizeFitter = mScrollRect.content.GetComponent <ContentSizeFitter>();
            }

            mIsHorizontal = mScrollRect.horizontal;
            mIsVertical   = mScrollRect.vertical;

            objectPool.createMoreIfNeeded = createMoreIfNeeded;
            objectPool.Initialize(Mathf.Min(initialAmount, infoList.Length), objReference, mScrollRect.content);

            float totalSize          = 0f;
            int   toCreate           = Mathf.Min(initialAmount, infoList.Length);
            var   lastObjectPosition = Vector2.zero;

            for (var i = 0; i < toCreate; i++)
            {
                var obj = objectPool.Collect();
                obj.updateScrollObject(mInfoList[currentIndex + i], currentIndex + i);
                var rect = obj.GetComponent <RectTransform>();
                var posX = i > 0 ? lastObjectPosition.x + (mIsHorizontal ? +spacing : 0) : 0;
                var posY = i > 0 ? lastObjectPosition.y - (mIsVertical ? spacing : 0) : 0;
                rect.anchoredPosition = new Vector2(posX, posY);
                lastObjectPosition    = new Vector2(posX + (mIsHorizontal ? obj.currentWidth : 0), posY - (mIsVertical ? obj.currentHeight : 0));

                totalSize += (mIsVertical) ? obj.currentHeight : obj.currentWidth;
            }

            totalSize = (totalSize / (float)toCreate) * infoList.Length;
            bool canDrag = (mIsHorizontal && totalSize > mScrollRect.content.rect.width) || (mIsVertical && totalSize > mScrollRect.content.rect.height);

            ToggleScroll(canDrag);

            DisableGridComponents();

            objectPool.ForEach(x => x.SetRefreshListAction(RefreshPosition));

            if (!mIsHorizontal || !mIsVertical)
            {
                return;
            }
            Debug.LogError("AssukarSynamicScroll doesn't support scrolling in both directions, please choose one direction (horizontal or vertical)");
            mIsHorizontal = false;
        }