예제 #1
0
    public void BindData <T>(string templatedName, List <T> dataList, System.Action <GameObject, T> bindEvent, bool itemCanClick = true, bool showSelectedItem = false)
    {
        ShowSelectedItem = showSelectedItem;
        int childCount = Content.transform.childCount;

        for (int i = childCount - 1; i >= 0; i--)
        {
            MultiObjectManager.Despawn(Content.transform.GetChild(i));
        }
        float totalHeight = 0;

        foreach (T t in dataList)
        {
            Transform item = MultiObjectManager.Spawn(templatedName);
            item.GetComponent <RectTransform>().SetParent(Content.transform);
            if (itemCanClick)
            {
                Button btnCtr = item.GetComponent <Button>();
                if (btnCtr != null && item.GetComponent <ButtonClickListener>() == null)
                {
                    ButtonClickListener btnListener = item.gameObject.AddComponent <ButtonClickListener>();
                    btnListener.OnClick = MenuItem_OnClicked;
                    btnCtr.transition   = Selectable.Transition.None;
                    btnCtr.onClick.RemoveAllListeners();
                    btnCtr.onClick.AddListener(() => { btnListener.OnClicked(); });
                }
            }
            bindEvent(item.gameObject, t);
            if (item.GetComponent <LayoutElement>() != null)
            {
                totalHeight += item.GetComponent <LayoutElement>().preferredHeight;
            }
        }
    }
예제 #2
0
    private void CreateItem(int index, ref float totalHeight)
    {
        Transform     item     = MultiObjectManager.Spawn(TemplatedName, Content.transform);
        RectTransform itemRect = item.GetComponent <RectTransform>();

        //itemRect.SetParent(Content.transform);
        if (item.GetComponent <DataItemControl>() == null)
        {
            item.gameObject.AddComponent <DataItemControl>();
        }
        item.GetComponent <DataItemControl>().DataIndex = index;

        BindEvent(item.gameObject, DataList[index]);

        float offsetMinY = -totalHeight - itemRect.GetComponent <LayoutElement>().preferredHeight - Spacing;
        float offsetMaxY = -totalHeight - Spacing;

        if (!ItemList.ContainsKey(index))
        {
            ItemList.Add(index, new ItemLayoutInfo()
            {
                MinOffsetY = offsetMinY, MaxOffsetY = offsetMaxY
            });
        }
        //设置item的位置
        itemRect.anchoredPosition3D = new Vector3(0, 1, 0);
        itemRect.offsetMax          = new Vector2(0, offsetMaxY);
        itemRect.offsetMin          = new Vector2(0, offsetMinY);
        totalHeight += itemRect.GetComponent <LayoutElement>().preferredHeight + Spacing;

        if (ItemCanClick)
        {
            Button btnCtr = item.GetComponent <Button>();
            if (btnCtr != null && item.GetComponent <ButtonClickListener>() == null)
            {
                ButtonClickListener btnListener = item.gameObject.AddComponent <ButtonClickListener>();
                btnListener.OnClick = MenuItem_OnClicked;
                btnCtr.transition   = Selectable.Transition.None;
                btnCtr.onClick.RemoveAllListeners();
                btnCtr.onClick.AddListener(() => { btnListener.OnClicked(); });
            }
        }
    }
예제 #3
0
    public void BindData <T>(GameObject templated, List <T> dataList, System.Action <GameObject, T> bindEvent, bool itemCanClick = true, bool showSelectedItem = false)
    {
        ShowSelectedItem = showSelectedItem;
        int childCount = Content.transform.childCount;

        for (int i = childCount - 1; i >= 0; i--)
        {
            GameObject item = Content.transform.GetChild(i).gameObject;
            if (item.name != "template")
            {
                GameObject.Destroy(item);
            }
        }
        float totalHeight = 0;

        foreach (T t in dataList)
        {
            Transform item = GameObject.Instantiate <GameObject>(templated).transform;
            item.gameObject.SetActive(true);
            item.GetComponent <RectTransform>().SetParent(Content.transform);
            item.GetComponent <RectTransform>().localScale = new Vector3(1, 1, 1);
            if (itemCanClick)
            {
                Button btnCtr = item.GetComponent <Button>();
                if (btnCtr != null && item.GetComponent <ButtonClickListener>() == null)
                {
                    ButtonClickListener btnListener = item.gameObject.AddComponent <ButtonClickListener>();
                    btnListener.OnClick = MenuItem_OnClicked;
                    btnCtr.transition   = Selectable.Transition.None;
                    btnCtr.onClick.RemoveAllListeners();
                    btnCtr.onClick.AddListener(() => { btnListener.OnClicked(); });
                }
            }
            bindEvent(item.gameObject, t);
            if (item.GetComponent <LayoutElement>() != null)
            {
                totalHeight += item.GetComponent <LayoutElement>().preferredHeight;
            }
        }
    }