protected InternalStateGeneric(TParams sourceParams, ItemsDescriptor itemsDescriptor)
        {
            _SourceParams = sourceParams;
            _ItemsDesc    = itemsDescriptor;

            var lg = sourceParams.content.GetComponent <LayoutGroup>();

            if (lg && lg.enabled)
            {
                lg.enabled = false;
                Debug.Log("LayoutGroup on GameObject " + lg.name + " has beed disabled in order to use ScrollRectItemsAdapter8");
            }

            var contentSizeFitter = sourceParams.content.GetComponent <ContentSizeFitter>();

            if (contentSizeFitter && contentSizeFitter.enabled)
            {
                contentSizeFitter.enabled = false;
                Debug.Log("ContentSizeFitter on GameObject " + contentSizeFitter.name + " has beed disabled in order to use ScrollRectItemsAdapter8");
            }

            var layoutElement = sourceParams.content.GetComponent <LayoutElement>();

            if (layoutElement)
            {
                GameObject.Destroy(layoutElement);
                Debug.Log("LayoutElement on GameObject " + contentSizeFitter.name + " has beed DESTROYED in order to use ScrollRectItemsAdapter8");
            }

            if (sourceParams.scrollRect.horizontal)
            {
                startEdge           = RectTransform.Edge.Left;
                endEdge             = RectTransform.Edge.Right;
                transvStartEdge     = RectTransform.Edge.Top;
                _GetRTCurrentSizeFn = root => root.rect.width;
            }
            else
            {
                startEdge           = RectTransform.Edge.Top;
                endEdge             = RectTransform.Edge.Bottom;
                transvStartEdge     = RectTransform.Edge.Left;
                _GetRTCurrentSizeFn = root => root.rect.height;
            }


            _SourceParams.UpdateContentPivotFromGravityType();

            CacheScrollViewInfo();
        }
Exemplo n.º 2
0
        /// <summary>
        /// <para>Initialize the adapter. This is automatically called in Start(), but it may also be called manually, if for some reason you implement Start() and don't want to call base.Start()</para>
        /// <para>Will call Canvas.ForceUpdateCanvases(), Params.InitIfNeeded(), will initialize the internal state and will change the items count to 0</para>
        /// <para>IMPORTANT: Do not call it in Awake(), OnEnable(), OnDisable(). OnStart() is the best place to do it.</para>
        /// </summary>
        public void Init()
        {
            Canvas.ForceUpdateCanvases();

            _Params.InitIfNeeded(this);
            if (_Params.Snapper)
            {
                _Params.Snapper.Adapter = this;
            }

            if (_Params.scrollRect.horizontalScrollbar != null || _Params.scrollRect.verticalScrollbar != null)
            {
                throw new UnityException("SRIA only works with a " + typeof(ScrollbarFixer8).Name + " component added to the Scrollbar and the ScrollRect shouldn't have any scrollbar set up in the inspector (it hooks up automatically)");
            }

            //Func<int, float> getSizeFn; if (_Params.scrollRect.horizontal) getSizeFn = i => GetItemWidth(i); else getSizeFn = i => GetItemHeight(i);
            _ItemsDesc     = new ItemsDescriptor(_Params.DefaultItemSize);        //, _Params.DefaultItemSizeUsage == BaseParams.DefaultSizeUsage.PLACEHOLDER_SIZE);
            _InternalState = InternalState.CreateFromSourceParamsOrThrow(_Params, _ItemsDesc);

            _VisibleItems         = new List <TItemViewsHolder>();
            _AVGVisibleItemsCount = 0;

            Refresh();
            _InternalState.UpdateLastProcessedCTVirtualInsetFromParentStart();
            SetVirtualAbstractNormalizedScrollPosition(1f, false);             // scroll to start
            _Params.scrollRect.onValueChanged.AddListener(OnScrollViewValueChanged);

            if (ScrollPositionChanged != null)
            {
                ScrollPositionChanged(GetNormalizedPosition());
            }

            // Debug stuff
#if UNITY_EDITOR && !UNITY_WSA && !UNITY_WSA_10_0 // UNITY_WSA uses .net core, which SRIADebugger is not compatible with
            var debugger = GameObject.FindObjectOfType <SRIADebugger>();
            if (debugger)
            {
                debugger.InitWithAdapter(this);
            }
#endif

            Initialized = true;
        }
Exemplo n.º 3
0
 /// <summary>
 /// This is called during changing the items count.
 /// The base implementation reinitializes the items descriptor so that all items will have the same size, specified in <see cref="BaseParams.DefaultItemSize"/>
 /// If overriding the method and the item default size should remain the same as <see cref="BaseParams.DefaultItemSize"/>,
 /// don't forget to call the base implementation! Otherwise, call <see cref="ItemsDescriptor.ReinitializeSizes(ItemCountChangeMode, int, int, float?)"/> with the new default size as parameter.
 /// Use <see cref="ItemsDescriptor.BeginChangingItemsSizes(int)"/> before and <see cref="ItemsDescriptor.EndChangingItemsSizes()"/> after
 /// setting sizes. The indices of items for which you set custom sizes must be one after another (4,5,6,7.. etc). Gaps are not allowed.
 /// Use "itemsDesc[itemIndexInView] = size" syntax for setting custom sizes. In this call, <see cref="AbstractViewsHolder.ItemIndex"/> will be the same as <see cref="BaseItemViewsHolder.itemIndexInView"/>, even if looping is enabled.
 /// </summary>
 /// <param name="itemsDesc">The container for all the info related to items' sizes</param>
 protected virtual void CollectItemsSizes(ItemCountChangeMode changeMode, int count, int indexIfInsertingOrRemoving, ItemsDescriptor itemsDesc)
 {
     itemsDesc.ReinitializeSizes(changeMode, count, indexIfInsertingOrRemoving, _Params.DefaultItemSize);
 }