Unity UI组件ScrollRect实现无限滚动条

在游戏开发中 经常遇到滚动显示的数据 特别是商店商品 排行榜 .......等 数据很多,每一条数据去加载一个UI来显示显然对内存浪费很大,这种情况处理一般就是用几个显示条可滚动循环显示无限数据条。本篇介绍实现过程和大体思路以及可重用的滑动脚本InfinityGridLayoutGroup和MarketLayoutGroup数据管理刷新脚本。MarketElement类要看具体项目中具体数据结构来设计;仅供参考。

一 .总体流程

建一个循环滑动脚本 InfinityGridLayoutGroup类并且有删除刷新功能;

一个数据管理刷新脚本如:MarketLayoutGroup(商店市场数据刷新管理器),继承InfinityGridLayoutGroup类 并且在滑动   的时候给每条数据对象赋值显示;

一个单数据对象MarketElement;

一个UI 用于显示滑动;

1 .InfinityGridLayoutGroup类:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

[RequireComponent(typeof(GridLayoutGroup))]
[RequireComponent(typeof(ContentSizeFitter))]
public class InfinityGridLayoutGroup : MonoBehaviour
{

    public int minAmount = 0;//实现无限滚动,需要的最少的child数量。屏幕上能看到的+一行看不到的,比如我在屏幕上能看到 2 行,每一行 2 个。则这个值为 2行*2个 + 1 行* 2个 = 6个。
    public bool changePanel = false;//切换面板
    public bool up = false;
    RectTransform rectTransform;

    GridLayoutGroup gridLayoutGroup;
    ContentSizeFitter contentSizeFitter;

    ScrollRect scrollRect;

    List<RectTransform> children = new List<RectTransform>();

    Vector2 startPosition;

    public int amount = 0;

    public delegate void UpdateChildrenCallbackDelegate(int index, Transform trans);
    public UpdateChildrenCallbackDelegate updateChildrenCallback = null;

    public int realIndex = -1;
    int realIndexUp = -1; //从下往上;
    #region // 改动
    Vector2 gridLayoutSizeLast;
    Vector2 gridLayoutPosLast;
    Vector2 currentPos;
    #endregion
    public bool hasInit = false;
    Vector2 gridLayoutSize;
    Vector2 gridLayoutPos;
    Dictionary<Transform, Vector2> childsAnchoredPosition = new Dictionary<Transform, Vector2>();
    Dictionary<Transform, int> childsSiblingIndex = new Dictionary<Transform, int>();

    // Use this for initialization
    void Start()
    {
        StartCoroutine(InitChildren());
    }

    IEnumerator InitChildren()
    {
        yield return 0;
        // Debug.Log("hasInit" + hasInit);
        minAmount = transform.childCount;
        if (!hasInit)
        {
            //childsAnchoredPosition.Clear();
            //获取Grid的宽度;
            rectTransform = GetComponent<RectTransform>();

            gridLayoutGroup = GetComponent<GridLayoutGroup>();
            gridLayoutGroup.enabled = false;
            contentSizeFitter = GetComponent<ContentSizeFitter>();
            contentSizeFitter.enabled = false;
            gridLayoutSizeLast = rectTransform.sizeDelta;
            gridLayoutPos = rectTransform.anchoredPosition;
            gridLayoutSize = rectTransform.sizeDelta;

            // Debug.Log("<Color=Red>children</Color>---" + children.Count + "realIndex---" + realIndex);
            //注册ScrollRect滚动回调;
            scrollRect = transform.parent.GetComponent<ScrollRect>();
            scrollRect.onValueChanged.AddListener((data) => { ScrollCallback(data); });

            //获取所有child anchoredPosition 以及 SiblingIndex;
            for (int index = 0; index < transform.childCount; index++)
            {
                Transform child = transform.GetChild(index);
                RectTransform childRectTrans = child.GetComponent<RectTransform>();
                childsAnchoredPosition.Add(child, childRectTrans.anchoredPosition);

                childsSiblingIndex.Add(child, child.GetSiblingIndex());
            }
            //Debug.Log("<Color=Blue>children</Color>---" + children.Count + "realIndex---" + realIndex);
        }
        else
        {
            //Debug.Log("gridLayoutPosLast--" + gridLayoutSizeLast.y);
            rectTransform.anchoredPosition = new Vector2(gridLayoutPos.x, Vector2.zero.y);
            // Debug.Log("current--" + currentPos.y);

            rectTransform.sizeDelta = gridLayoutSize;
            gridLayoutSizeLast = rectTransform.sizeDelta;
            // Debug.Log("rectTransform.sizeDelta--" + rectTransform.sizeDelta.y);

            children.Clear();

            realIndex = -1;
            realIndexUp = -1;

            //children重新设置上下顺序;
            foreach (var info in childsSiblingIndex)
            {
                info.Key.SetSiblingIndex(info.Value);
            }

            //children重新设置anchoredPosition;
            for (int index = 0; index < transform.childCount; index++)
            {
                Transform child = transform.GetChild(index);

                RectTransform childRectTrans = child.GetComponent<RectTransform>();
                if (childsAnchoredPosition.ContainsKey(child))
                {
                    childRectTrans.anchoredPosition = childsAnchoredPosition[child];
                }
                else
                {
                    Debug.LogError("childsAnchoredPosition no contain " + child.name);
                }
            }
        }

        //获取所有child;
        for (int index = 0; index < transform.childCount; index++)
        {
            Transform trans = transform.GetChild(index);
            trans.gameObject.SetActive(true);

            children.Add(transform.GetChild(index).GetComponent<RectTransform>());

            //初始化前面几个;
            UpdateChildrenCallback(children.Count - 1, transform.GetChild(index));
        }

        startPosition = rectTransform.anchoredPosition;
        // Debug.Log("<Color=Red>children</Color>---"+ children.Count+ "realIndex---"+ realIndex);
        realIndex = children.Count - 1;

        ShowElement(realIndex);
        //Debug.Log( scrollRect.transform.TransformPoint(Vector3.zero));  

        // Debug.Log(transform.TransformPoint(children[0].localPosition));  

        hasInit = true;

        //如果需要显示的个数小于设定的个数;
        for (int index = 0; index < minAmount; index++)
        {
            children[index].gameObject.SetActive(index < amount);
        }

        if (gridLayoutGroup.constraint == GridLayoutGroup.Constraint.FixedColumnCount)
        {
            //如果小了一行,则需要把GridLayout的高度减去一行的高度;
            int row = (minAmount - amount) / gridLayoutGroup.constraintCount;
            if (row > 0)
            {
                rectTransform.sizeDelta -= new Vector2(0, (gridLayoutGroup.cellSize.y + gridLayoutGroup.spacing.y) * row);
            }
        }
        else
        {
            //如果小了一列,则需要把GridLayout的宽度减去一列的宽度;
            int column = (minAmount - amount) / gridLayoutGroup.constraintCount;
            if (column > 0)
            {
                rectTransform.sizeDelta -= new Vector2((gridLayoutGroup.cellSize.x + gridLayoutGroup.spacing.x) * column, 0);
            }
        }
        //if (amount <= minAmount)
        //    scrollRect.enabled = false;
        //else
        //    scrollRect.enabled = true;
    }

    void ScrollCallback(Vector2 data)
    {
        UpdateChildren();
    }

    void UpdateChildren()
    {
        // Debug.Log("当前位置");
        if (transform.childCount < minAmount)
        {
            return;
        }
        // Debug.Log("当前位置" + rectTransform.anchoredPosition.y + "startPosition.y" + startPosition.y);
        currentPos = rectTransform.anchoredPosition;
        //Vector2 currentPos = rectTransform.anchoredPosition;

        if (gridLayoutGroup.constraint == GridLayoutGroup.Constraint.FixedColumnCount)
        {
            float offsetY = currentPos.y - startPosition.y;

            if (offsetY > 0)
            {
                //向上拉,向下扩展;
                {
                    if (realIndex >= amount - 1)
                    {
                        startPosition = currentPos;
                        return;
                    }
                    up = false;
                    float scrollRectUp = scrollRect.transform.TransformPoint(Vector3.zero).y;

                    Vector3 childBottomLeft = new Vector3(children[0].anchoredPosition.x, children[0].anchoredPosition.y - gridLayoutGroup.spacing.y - gridLayoutGroup.cellSize.y * 0.5f, 0f); //gridLayoutGroup.cellSize.y
                    float childBottom = transform.TransformPoint(childBottomLeft).y;

                    if (childBottom >= scrollRectUp)
                    {
                        Debug.Log("childBottom >= scrollRectUp");

                        //移动到底部;
                        for (int index = 0; index < gridLayoutGroup.constraintCount; index++)
                        {
                            children[index].SetAsLastSibling();

                            children[index].anchoredPosition = new Vector2(children[index].anchoredPosition.x, children[children.Count - 1].anchoredPosition.y - gridLayoutGroup.cellSize.y - gridLayoutGroup.spacing.y);

                            realIndex++;

                            if (realIndex > amount - 1)
                            {
                                children[index].gameObject.SetActive(false);
                            }
                            else
                            {
                                UpdateChildrenCallback(realIndex, children[index]);

                            }
                        }
                        ShowElement(realIndex);
                        //GridLayoutGroup 底部加长;  

                        rectTransform.sizeDelta += new Vector2(0, gridLayoutGroup.cellSize.y + gridLayoutGroup.spacing.y);
                        gridLayoutSizeLast = rectTransform.sizeDelta;
                        // Debug.Log("<Color=Red>gridLayoutSizeLast.y</Color>" + gridLayoutSizeLast.y);
                        //更新child;
                        for (int index = 0; index < children.Count; index++)
                        {
                            children[index] = transform.GetChild(index).GetComponent<RectTransform>();
                        }
                    }
                    // Debug.Log("realIndex向上--" + realIndex);
                }
            }
            else
            {
                //Debug.Log("Drag Down");
                //向下拉,下面收缩;
                if (realIndex + 1 <= children.Count)
                {
                    startPosition = currentPos;
                    return;
                }

                RectTransform scrollRectTransform = scrollRect.GetComponent<RectTransform>();
                Vector3 scrollRectAnchorBottom = new Vector3(0, -scrollRectTransform.rect.height - gridLayoutGroup.spacing.y, 0f);//- gridLayoutGroup.spacing.y
                float scrollRectBottom = scrollRect.transform.TransformPoint(scrollRectAnchorBottom).y;

                Vector3 childUpLeft = new Vector3(children[children.Count - 1].anchoredPosition.x, children[children.Count - 1].anchoredPosition.y + gridLayoutGroup.spacing.y * minAmount, 0f);//gridLayoutGroup.spacing.y realIndex-minAmount+1

                float childUp = transform.TransformPoint(childUpLeft).y;
                //Debug.Log("childUp----" + childUp + "scrollRectBottom---" + scrollRectBottom);
                if (childUp < scrollRectBottom)
                {
                    //Debug.Log("childUp < scrollRectBottom");
                    up = true;
                    //把底部的一行 移动到顶部
                    for (int index = 0; index < gridLayoutGroup.constraintCount; index++)
                    {
                        children[children.Count - 1 - index].SetAsFirstSibling();

                        children[children.Count - 1 - index].anchoredPosition = new Vector2(children[children.Count - 1 - index].anchoredPosition.x, children[0].anchoredPosition.y + gridLayoutGroup.cellSize.y + gridLayoutGroup.spacing.y);

                        children[children.Count - 1 - index].gameObject.SetActive(true);

                        UpdateChildrenCallback(realIndex - children.Count - index, children[children.Count - 1 - index]);
                    }

                    realIndex -= gridLayoutGroup.constraintCount;

                    ShowElement(realIndex);
                    //GridLayoutGroup 底部缩短;
                    //rectTransform.anchoredPosition = gridLayoutPos;

                    rectTransform.sizeDelta -= new Vector2(0, gridLayoutGroup.cellSize.y + gridLayoutGroup.spacing.y);
                    gridLayoutSizeLast = rectTransform.sizeDelta;
                    //Debug.Log("<Color=Red>gridLayoutSizeLast.y</Color>" + gridLayoutSizeLast.y);
                    //更新child;
                    for (int index = 0; index < children.Count; index++)
                    {
                        children[index] = transform.GetChild(index).GetComponent<RectTransform>();
                    }
                }
                // Debug.Log("realIndex向下--" + realIndex);
            }
        }
        #region 左右滑动
        else
        {
            float offsetX = currentPos.x - startPosition.x;

            if (offsetX < 0)
            {
                //向左拉,向右扩展;
                {
                    if (realIndex >= amount - 1)
                    {
                        startPosition = currentPos;
                        return;
                    }

                    float scrollRectLeft = scrollRect.transform.TransformPoint(Vector3.zero).x;

                    Vector3 childBottomRight = new Vector3(children[0].anchoredPosition.x + gridLayoutGroup.cellSize.x, children[0].anchoredPosition.y, 0f);
                    float childRight = transform.TransformPoint(childBottomRight).x;

                    // Debug.LogError("childRight=" + childRight);  

                    if (childRight <= scrollRectLeft)
                    {
                        //Debug.Log("childRight <= scrollRectLeft");  

                        //移动到右边;
                        for (int index = 0; index < gridLayoutGroup.constraintCount; index++)
                        {
                            children[index].SetAsLastSibling();

                            children[index].anchoredPosition = new Vector2(children[children.Count - 1].anchoredPosition.x + gridLayoutGroup.cellSize.x + gridLayoutGroup.spacing.x, children[index].anchoredPosition.y);

                            realIndex++;

                            if (realIndex > amount - 1)
                            {
                                children[index].gameObject.SetActive(false);
                            }
                            else
                            {
                                UpdateChildrenCallback(realIndex, children[index]);
                            }
                        }
                        if (realIndex >= 7)
                            //GridLayoutGroup 右侧加长;
                            rectTransform.sizeDelta += new Vector2(gridLayoutGroup.cellSize.x + gridLayoutGroup.spacing.x, 0);

                        //更新child;
                        for (int index = 0; index < children.Count; index++)
                        {
                            children[index] = transform.GetChild(index).GetComponent<RectTransform>();
                        }
                    }
                }
            }
            else
            {
                //Debug.Log("Drag Down");
                //向右拉,右边收缩;
                if (realIndex + 1 <= children.Count)
                {
                    startPosition = currentPos;
                    return;
                }
                RectTransform scrollRectTransform = scrollRect.GetComponent<RectTransform>();
                Vector3 scrollRectAnchorRight = new Vector3(scrollRectTransform.rect.width + gridLayoutGroup.spacing.x, 0, 0f);
                float scrollRectRight = scrollRect.transform.TransformPoint(scrollRectAnchorRight).x;

                Vector3 childUpLeft = new Vector3(children[children.Count - 1].anchoredPosition.x, children[children.Count - 1].anchoredPosition.y, 0f);

                float childLeft = transform.TransformPoint(childUpLeft).x;

                if (childLeft >= scrollRectRight)
                {
                    //Debug.LogError("childLeft > scrollRectRight");  

                    //把右边的一行 移动到左边;
                    for (int index = 0; index < gridLayoutGroup.constraintCount; index++)
                    {
                        children[children.Count - 1 - index].SetAsFirstSibling();

                        children[children.Count - 1 - index].anchoredPosition = new Vector2(children[0].anchoredPosition.x - gridLayoutGroup.cellSize.x - gridLayoutGroup.spacing.x, children[children.Count - 1 - index].anchoredPosition.y);

                        children[children.Count - 1 - index].gameObject.SetActive(true);

                        UpdateChildrenCallback(realIndex - children.Count - index, children[children.Count - 1 - index]);
                    }

                    //GridLayoutGroup 右侧缩短;
                    rectTransform.sizeDelta -= new Vector2(gridLayoutGroup.cellSize.x + gridLayoutGroup.spacing.x, 0);

                    //更新child;
                    for (int index = 0; index < children.Count; index++)
                    {
                        children[index] = transform.GetChild(index).GetComponent<RectTransform>();
                    }

                    realIndex -= gridLayoutGroup.constraintCount;
                }
                #endregion
            }
        }
        // Debug.Log("realIndex--" + realIndex);
        //Debug.Log("currentPos.y--" + currentPos.y + "rectTransform.sizeDelta---" + rectTransform.sizeDelta.y);
        startPosition = currentPos;
        gridLayoutPosLast = currentPos;
    }

    void UpdateChildrenCallback(int index, Transform trans)
    {
        if (updateChildrenCallback != null)
        {
            updateChildrenCallback(index, trans);
        }
    }

    public virtual void ShowElement(int endIndex)
    {

    }
    /// <summary>
    /// 设置总的个数;
    /// </summary>
    /// <param name="count"></param>
    public void SetAmount(int count)
    {
        amount = count;
        //如果切换面板
        if (!changePanel)
        {
            //hasInit = false;
            StartCoroutine(InitChildren());
            changePanel = true;
        }
        else
        {
            // Debug.Log("currentPos.y--" + currentPos.y);
            if (currentPos.y > 10)
            {

                如果需要显示的个数小于设定的个数;
                //Debug.Log("minAmount--"+minAmount);
                for (int index = 0; index < minAmount; index++)
                {
                    children[index].gameObject.SetActive(index < amount);
                }
                //删除操作
                if (GameInstance.isDecrase)
                {
                    if (realIndex > minAmount - 1)
                    {
                        realIndex--;
                        //把底部的一行 移动到顶部
                        for (int index = 0; index < gridLayoutGroup.constraintCount; index++)
                        {
                            children[children.Count - 1 - index].SetAsFirstSibling();

                            children[children.Count - 1 - index].anchoredPosition = new Vector2(children[children.Count - 1 - index].anchoredPosition.x, children[0].anchoredPosition.y + gridLayoutGroup.cellSize.y + gridLayoutGroup.spacing.y);

                            children[children.Count - 1 - index].gameObject.SetActive(true);

                            UpdateChildrenCallback(realIndex - children.Count - index, children[children.Count - 1 - index]);
                        }
                        //更新child;
                        for (int index = 0; index < children.Count; index++)
                        {
                            children[index] = transform.GetChild(index).GetComponent<RectTransform>();
                        }
                    }

                    GameInstance.isDecrase = false;//GameInstance类中bool变量控制条目删除的刷新使用

                    currentPos = new Vector2(currentPos.x, currentPos.y - gridLayoutGroup.cellSize.y - gridLayoutGroup.spacing.y);
                    if (realIndex > minAmount - 1)
                    {
                        gridLayoutSizeLast = new Vector2(gridLayoutSizeLast.x, gridLayoutSizeLast.y - gridLayoutGroup.cellSize.y - gridLayoutGroup.spacing.y);
                    }
                    else
                        rectTransform.anchoredPosition = currentPos;
                }
                else
                    rectTransform.anchoredPosition = currentPos;

                rectTransform.sizeDelta = gridLayoutSizeLast;
                rectTransform.anchoredPosition = new Vector2(currentPos.x, currentPos.y);
                startPosition = rectTransform.anchoredPosition;
                ShowElement(realIndex);

            }
            else
            {
                StartCoroutine(InitChildren());
            }
        }
   }
}

2.MarketLayoutGroup类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MarketLayoutGroup : InfinityGridLayoutGroup
{
    bool isNotOutRange = false;
    //商店市场的可买的商品集合
    private List<MarketGood> marketGoods;
    //可出售到商店的商品集合
    public List<SealGood> marketSeal;
    //可要在商店加工的商品集合
    private List<ExchangeGood> marketMake;

    //重写父类的调用循环刷新显示
    public override void ShowElement(int endIndex)
    {
        //获取商店窗体对象
        MarketActivity market = MainPanelContrller.Instance.presenter.MarketWindowsActivity[0] as MarketActivity;
        //判断当前窗体默认开启的类型是否是可买的商品集合
        if (market.current == CurrentPanel.Image_Buy)
        {
            //获取可买商品的数据( GameInstance全局数据储存脚本)
            marketGoods = GameInstance.marketGoods;
            //判断商品数
            amount = marketGoods.Count;
            //Debug.Log ("marketGoods--"+marketGoods.Count);
            //endIndex为可见ui的最大下标
            isNotOutRange = endIndex < marketGoods.Count ? true : false;
            //遍历ui对象数目 判断每一条ui应该显示的数据 并调用MarketElement赋值显示。
            for (int i = 0; i < transform.childCount; i++)
            {
                //获取ui显示对象
                MarketElement presenter = transform.GetChild(i).GetComponent<MarketElement>();
                //判断并且赋值显示
                if (isNotOutRange)
                {
                    //presenter.name.text = myGoodList[endIndex - minAmount + 1 + i].name;
                    presenter.SetData(marketGoods[endIndex - minAmount + 1 + i]);
                }
                else
                {
                    if (endIndex - minAmount + 1 + i < marketGoods.Count)
                    {
                        presenter.SetData(marketGoods[endIndex - minAmount + 1 + i]);
                    }
                    else
                    {
                        MarketGood good = new MarketGood();

                        presenter.SetData(good);
                    }
                }
            }
        }
        else if (market.current == CurrentPanel.Image_Seal)
        {
            if (GameInstance.marketSeals == null)
                return;
            //Debug.Log("GameInstance.marketSeals" + GameInstance.marketSeals.Count);
            // Debug.Log("marketSeal" + marketSeal);
            marketSeal = GameInstance.marketSeals;
            amount = marketSeal.Count;
            isNotOutRange = endIndex < marketSeal.Count ? true : false;
            for (int i = 0; i < transform.childCount; i++)
            {
                ElementSeal presenter = transform.GetChild(i).GetComponent<ElementSeal>();
                if (isNotOutRange)
                {
                    //Debug.Log("endIndex+" + endIndex);
                    //Debug.Log(endIndex - minAmount + 1 + i);
                    //presenter.name.text = myGoodList[endIndex - minAmount + 1 + i].name;
                    presenter.SetData(marketSeal[endIndex - minAmount + 1 + i]);
                }
                else
                {

                    // Debug.Log(endIndex - minAmount + 1 + i);
                    if (endIndex - minAmount + 1 + i < marketSeal.Count)
                    {
                        presenter.SetData(marketSeal[endIndex - minAmount + 1 + i]);
                    }
                    else
                    {
                        SealGood _good = new SealGood();
                        presenter.SetData(_good);
                    }
                }
            }
        }
        else
        {

            //Debug.Log("当前所在条数--" + realIndex);
            //加工
            marketMake = GameInstance.marketMakes;
            amount = marketMake.Count;
            isNotOutRange = endIndex < marketMake.Count ? true : false;
            for (int i = 0; i < transform.childCount; i++)
            {
                MarketElement presenter = transform.GetChild(i).GetComponent<MarketElement>();
                if (isNotOutRange)
                {
                    //presenter.name.text = myGoodList[endIndex - minAmount + 1 + i].name;
                    presenter.SetData(marketMake[endIndex - minAmount + 1 + i]);
                }
                else
                {
                    //Debug.Log ("marketGoods---"+marketGoods);
                    if (endIndex - minAmount + 1 + i < marketMake.Count)
                    {
                        presenter.SetData(marketMake[endIndex - minAmount + 1 + i]);
                    }
                    else
                    {
                        ExchangeGood good = new ExchangeGood();
                        presenter.SetData(good);
                    }
                }
            }
        }

    }
}

3.UI对象父类MarketElement:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MarketElement : MonoBehaviour
{
    public virtual void SetData(ExchangeGood good) { }
    public virtual void SetData(MarketGood good) { }
    public virtual void SetData(SealGood good) { }
    public virtual void SetData() { }

}

实现类:ElementMarket

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ElementMarket : MarketElement
{

    private MarketGood _goodData;
    [Header("物品框父物体")]
    public Image good_BG;
    public Text price;
    public Text num;
    public Text name;
    public Text nameGoods;
    public Button buy;

    LocalizeData xml_data = new LocalizeData();
    string config = LocalizeManager.Instance.GetCurrentLanguage();

    public override void SetData(MarketGood good)
    {
        base.SetData(good);
        if (good.goodsId == 0) return;

        _goodData = good;
        if (MainPanelContrller.Instance.openStyle == OpenMarketStyle.Skill)
        {
            GoodsInfoData myGood = new GoodsInfoData();
            GameManager.Instance.allXmlData.GetXmlData<GoodsInfoData>(ConfigFilesName.GOODS_CONFIG, good.goodsId, ref myGood);
            good_BG.transform.GetChild(0).GetComponent<Image>().sprite = SpriteManager.LoadAtlasSprite("Sprite/Item", "Item_" + myGood.Icon);
            price.text = "x" + good.price.ToString();
            nameGoods.text = myGood.Name;
            // num.text = "x" + good.num;
            // int trend = good.price - good.lastPrice;
            //使用描述
            name.text = myGood.Description;
            //物品描述
            num.text = myGood.Description_2;

        }
        else
        {
            //员外的
        }
    }

    void OnEnable()
    {
        buy.onClick.AddListener(BuyBtn);
    }
    void OnDisable()
    {
        buy.onClick.RemoveAllListeners();
    }

    /// <summary>
    /// 打开购买二级界面
    /// </summary>
    void BuyBtn()
    {
        Debug.Log("购买了资源商店:" + _goodData.goodsId + " 物品");
        this.gameObject.transform.parent.GetComponent<MarketLayoutGroup>().changePanel = true;
        Bundle bundle = new Bundle();
        bundle.PutObject("marketGood", _goodData);
        MainPanelContrller.Instance.presenter.MarketWindowsActivity[0].manager.PushWindow(typeof(BuyActivity), bundle);
    }

}

4.UI:

滑动总UI scrollrect

滑动组件Grid MarketLayerGourp

滑动条 ElementMarket:

Ok 介绍到这里:ElementMarket 根据具体项目的数据 进行调整,其他两项都可以重用。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Unity实现轮盘方式的按钮滚动效果

    近期在项目中,策划给出了一个需求就是,让按钮按照一个轮盘的轨迹进行滑动的效果,经过一番测试,实现了初步的效果. 我这里区分了横向滑动和纵向滑动,这里以纵向滑动为例子进行示范,实现按钮的滑动效果. 首先就是先进行位置初始化: /// <summary> ///从大到小排序,Y轴 /// </summary> private Comparison<CircleScrollRectItemBase> ComparisionY = delegate (CircleScrollR

  • Unity实现3D循环滚动效果

    本文实例为大家分享了Unity实现3D循环滚动效果展示的具体代码,供大家参考,具体内容如下 然后通过SetDepthAndPosition这个方法,实现图片的空间空间展开 Z轴和Y轴,系数是一样的 经过上面设置,空间就摆开了 using UnityEngine; using System.Collections; using System.Collections.Generic; public class SelectRole : MonoBehaviour { public GameObjec

  • unity实现文字滚动效果

    本文实例为大家分享了unity实现文字滚动效果的具体代码,供大家参考,具体内容如下 效果: 代码: using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine; using System; //移动类型 [Serializable] public enum MoveType { [EnumAttirbute("水平滚动")] horMove, [E

  • Unity UGUI实现卡片椭圆方向滚动

    本文实例为大家分享了UGUI实现卡片椭圆方向滚动的具体代码,供大家参考,具体内容如下 搭建简单的场景 运行效果 卡片移动动画通过插件DoTween实现 控制脚本: using UnityEngine; using System.Collections; using UnityEngine.UI; using DG.Tweening; public class CardMove : MonoBehaviour { GameObject[] sprites; int halfSize; Vector

  • Unity实现老虎机滚动抽奖效果的示例代码

    直接看下效果图吧: 制作思路: 设计四张图片,五个点,每个图片同时向下一个点移动,到最后一个就回到0号点,以此循环. 场景搭建: 创建Image命名为Bg作为电视框背景: 创建Image命名Mask并添加Mask组件作为电视框内容显示遮罩框: 创建四个Image作为滚动图片: 创建开始抽奖按钮: PS:实际项目中可以根据需求来动态修改图片显示,以达到的控制每次抽奖奖品内容. 源码分享: using System.Collections; using UnityEngine; using Unit

  • Unity实现游戏卡牌滚动效果

    最近项目中的活动面板要做来回滚动卡牌预览效果,感觉自己来写的话,也能写,但是可能会比较耗时,看到Github上有开源的项目,于是就借用了,Github的资源地址,感谢作者的分享. 本篇博客旨在告诉大家如何利用这个插件. 插件的核心在于工程中的6个脚本,以下是六个脚本的源码: DragEnhanceView.cs using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSys

  • Unity UI组件ScrollRect实现无限滚动条

    在游戏开发中 经常遇到滚动显示的数据 特别是商店商品 排行榜 .......等 数据很多,每一条数据去加载一个UI来显示显然对内存浪费很大,这种情况处理一般就是用几个显示条可滚动循环显示无限数据条.本篇介绍实现过程和大体思路以及可重用的滑动脚本InfinityGridLayoutGroup和MarketLayoutGroup数据管理刷新脚本.MarketElement类要看具体项目中具体数据结构来设计:仅供参考. 一 .总体流程 建一个循环滑动脚本 InfinityGridLayoutGroup

  • Vue组件大全包括(UI组件,开发框架,服务端,辅助工具,应用实例,Demo示例)

    Vue是一款比较流行的JS库,本文为大家介绍一些Vue组件,包括UI组件,开发框架,服务端,辅助工具,应用实例,Demo示例等开源项目 一.Vue常用UI组件 element ★11612 - 饿了么出品的Vue2的web UI工具套件 Vux ★7503 - 基于Vue和WeUI的组件库 iview ★5801 - 基于 Vuejs 的开源 UI 组件库 mint-ui ★5517 - Vue 2的移动UI元素 vue-material ★2790 - 通过Vue Material和Vue 2

  • 聊聊Unity自定义组件之序列帧播放组件问题

    我们知道在unity中播放序列帧动画有两种方式,第一种是利用Unity自带的animation组件来播放,我们只需要在工程目录中全选选中所有我们需要播放的图片,将其拖动到Hiercarchy上,Unity就会帮我们自动创建一个animation片段,我们就可以用animation组件来控制我们的动画,不过这种方式创建的图片Sprite Renderer类型的.第二种方式就是创建一个Image组件,利用代码创建一个sprite,写一段代码利用Update函数来逐帧替换Image的sprite来实现

  • 深入探究angular2 UI组件之primeNG用法

    前言:众所周知,primeNG是ng2的一个ui组件库,很强大,个人感觉比ng2-bootstrap还强大,下面就告诉大家怎么安装使用,官网:http://www.primefaces.org/primeng/#/ 本人使用angular cli 创建项目,所以讲述在cli下的配置安装步骤,如果没有使用cli也不用担心,官网有具体的安装步骤. 1. 安装 cd 你的项目目录 npm install primeng --save-dev 2. 配置angular-cli.json omega是一种

  • vue.js学习之UI组件开发教程

    本文主要给大家介绍了关于vue.js之UI组件开发的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: 1. 新建组件: <script src="/public/javascripts/vue.js"></script> <style> #app1{background-color: red} #app2{background-color: blue} </style> <body> <div id=&quo

  • ReactiveCocoa代码实践之-UI组件的RAC信号操作

    相关阅读: ReactiveCocoa代码实践之-更多思考 ReactiveCocoa代码实践之-RAC网络请求重构这一节是自己对网络层的一些重构,本节是自己一些代码小实践做出的一些demo程序,基本涵盖大多数UI控件操作. 一.用UISlider实现调色板 假设我们现在做一个demo,上面有一个View用来展示颜色,下面有三个UISlider滑竿分别控制RGB的色值,随着不同滑竿的拖动上面view的颜色会随之改变. 可以先脑补一下不用RAC该怎么写. 如果使用RAC只需要将三个信号包装起来用适

  • ui组件之input多选下拉实现方法(带有搜索功能)

    我的风格,先上效果图,如果大家感觉还不错,请参考实现代码. 废话不说 先看div层次结构 <!-- 最外层div 可以任意指定 主要用于定义子元素宽度 --> <div class="col-xs-10" style="width:800px"> <!-- 表单label 添加文字提示 --> <label for="" class="label-control">全文检索<

  • Android布局技巧之创建可重用的UI组件

    Android平台提供了大量的UI构件,你可以将这些小的视觉块(构件)搭建在一起,呈现给用户复杂且有用的画面.然而,应用程序有时需要一些高级的视觉组件.为了满足这一需求,并且能高效的实现,你可以把多个标准的构件结合起来成为一个单独的.可重用的组件. 例如,你可以创建一个可重用的组件包含一个进度条和一个取消按钮,一个Panel包含两个按钮(确定和取消动作),一个Panel包含图标.标题和描述等等.简单的,你可以通过书写一个自定义的View来创建一个UI组件,但更简单的方式是仅使用XML来实现. 在

  • 基于Vue 2.0的模块化前端 UI 组件库小结

    AT-UI 是一款基于 Vue.js 2.0 的轻量级.模块化前端 UI 组件库,主要用于快速开发 PC 网站产品. 专门为桌面应用程序构建,AT-UI 提供了一套 npm + webpack + babel 前端开发工作流程,以及一个体面的干净整洁的 UI 组件. 特性 基于 Vue 开发的 UI 组件 基于 npm + webpack + babel 的工作流,支持 ES2015 CSS 样式独立,保证不同的框架实现都能保持统一的 UI 风格( 详见: AT-UI Style) 提供友好的

  • 在vue项目中引入高德地图及其UI组件的方法

    引入高德地图: 打开index.html,引用高德地图的JavaScript API: <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=你的API key"></script> 在"key="这里添加你申请的key,key不需要加引号. 引入高德地图UI组件,只需要在上面代码后面再加一串代码: <script

随机推荐