Exemple #1
0
        public ScrollViewItem CreateInstance(int index)
        {
            int type = GetTypeByIndex(index);

            ScrollViewItem item = null;

            if (caches != null && caches[type] != null && caches[type].Count > 0)
            {
                item = caches[type].Pop();
                item.gameObject.SetActive(true);
            }
            if (item == null)
            {
                ScrollViewPrefabData prefabData = GetPrefabDataByType(type);
                if (prefabData != null && prefabData.prefab != null)
                {
                    GameObject obj = GameObject.Instantiate(prefabData.prefab);
                    item = obj.AddComponent <ScrollViewItem>();

                    if (prefabData.widthStretch)
                    {
                        item.rectTransform.sizeDelta = new Vector2(scrollView.viewPortSzie.x, item.rectTransform.sizeDelta.y);
                    }

                    if (prefabData.heightStretch)
                    {
                        item.rectTransform.sizeDelta = new Vector2(item.rectTransform.sizeDelta.x, scrollView.viewPortSzie.y);
                    }
                    item.prefabType = type;
                }
            }

            if (item != null)
            {
                item.index = index;
                if (onNewItem != null)
                {
                    onNewItem.Invoke(index, item);
                }
            }

            return(item);
        }
Exemple #2
0
        private GameObject GetPrefabByType(int type)
        {
            ScrollViewPrefabData scrollViewPrefabData = GetPrefabDataByType(type);

            return(scrollViewPrefabData == null ? null : scrollViewPrefabData.prefab);
        }