UIWrapContent的使用

UIWrapContent的使用_第1张图片

using UnityEngine;
using System.Collections.Generic;

namespace Assets.QWrapContent
{
    public class ScrollSample : MonoBehaviour
    {
        [SerializeField]
        UIScrollView ScrollView;
        [SerializeField]
        UIWrapContent WrapContent;


        List<int> intList = new List<int>();

        void Start() {
            init();
        }

        // Update is called once per frame 
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.A))
            {
                init();
            }
        }

        void init()
        {
            intList.Clear();
            for (int i = 0; i < 12; i++)
            {
                intList.Add(i + 1);
            }
            var contentItems = WrapContent.GetComponentsInChildren();
            var maxSize = contentItems.Length;
            int count = intList.Count;
            for (int i = 0; i < contentItems.Length; i++)
            {

                contentItems[i].gameObject.SetActive((i < count));
                if (i < count)
                    contentItems[i].SetData(intList[i]);
                contentItems[i].transform.localPosition = Vector3.zero;
            }

            // 按照(start,max+start-1);

            // 按照(0,max-1);
            var start = 0;
            var end = count - 1;
            //start = -2;
            //end = count + start - 1;

            /*if (count <= maxSize)
            {
                return;
            }*/
            WrapContent.onInitializeItem = OnUpdateItem;
            WrapContent.minIndex = start;
            WrapContent.maxIndex = end;

            WrapContent.SortBasedOnScrollMovement();
            ScrollView.ResetPosition();

            WrapContent.enabled = true;
        }
        void OnUpdateItem(GameObject go, int index, int realIndex)
        {
            while (realIndex < 0)
            {
                realIndex += intList.Count;
            }
            int num = realIndex % intList.Count;

            var item = go.GetComponent();
            item.SetData(intList[num]);
        }
    }
}

你可能感兴趣的:(Unity)