Scroll View控制菜单栏的伸缩

来源:互联网 发布:java数组和指针的区别 编辑:程序博客网 时间:2024/06/15 10:40

一:操作篇

在Cavas 添加Scroll View,想要横排的物品栏。在Viewport 下的Content上添加组件:Grid Layout Grop和Content Size Fitter

在Content下添加button,

Content:计算要显示物品栏的多少

二:代码篇

   //所有的父(第一级主)菜单
    public List<GameObject> allObj = new List<GameObject>();
    Vector2 contentSizeData;//每次计算Content的所容纳的数量
    RectTransform contentRt;
    GameObject lastObj;//最后一次点击的父菜单
    void Start ()
    {
        List<GameObject> child = new List<GameObject>();//所有子类菜单
        child.Add(GameObject.Find("Button (1)"));
        child.Add(GameObject.Find("Button (2)"));
        child.Add(GameObject.Find("Button (3)"));
        child.Add(GameObject.Find("Button (4)"));
        child.Add(GameObject.Find("Button (5)"));
        child.Add(GameObject.Find("Button (6)"));
        child.Add(GameObject.Find("Button (7)"));
        child.Add(GameObject.Find("Button (8)"));
        child.Add(GameObject.Find("Button (9)"));
        child.Add(GameObject.Find("Button (10)"));
        child.Add(GameObject.Find("Button (11)"));
        child.Add(GameObject.Find("Button (12)"));
        child.Add(GameObject.Find("Button (13)"));
        child.Add(GameObject.Find("Button (14)"));
        child.Add(GameObject.Find("Button (15)"));
        child.Add(GameObject.Find("Button (16)"));
        child.Add(GameObject.Find("Button (17)"));
        child.Add(GameObject.Find("Button (18)"));
        child.Add(GameObject.Find("Button (19)"));
        child.Add(GameObject.Find("Button (20)"));
        AddItem(GameObject.Find("Button"), child);
        Init();
    }
    /// <summary>
    /// 初始化
    /// </summary>
    void Init()
    {
        lastObj = null;
        contentRt = GameObject.Find("Canvas/MY Scroll View/Viewport/Content").GetComponent<RectTransform>();
        contentSizeData = contentRt.sizeDelta;
        contentSizeData.y = 0;
        for (int i = 0; i < allObj.Count; i++)
        {
            ChildListState(allObj[i], 0, false);
            contentSizeData.y += allObj[i].GetComponent<RectTransform>().sizeDelta.y;
        }
        contentRt.sizeDelta = contentSizeData;
    }
    /// <summary>
    /// 加入一个父菜单和子菜单
    /// </summary>
    /// <param name="parent">父菜单物体</param>
    /// <param name="childList">子菜单</param>
    private void AddItem(GameObject parent, List<GameObject> childList)
    {                                                    //(应用委托,传一个方法)
        parent.GetComponent<Button>().onClick.AddListener(delegate { OnClickParent(parent); });
        allObj.Add(parent);//添加到父物体菜单
        ItemGroup group = parent.AddComponent<ItemGroup>();
        foreach (GameObject item in childList)
            group.childList.Add(item);
        childList.Clear();
    }
    /// <summary>
    /// 控制子菜单的显示和隐藏
    /// </summary>
    /// <param name="item">父菜单物体</param>
    /// <param name="isShow">是否显示,0隐藏1显示2列表的第一个物体的相反状态</param>
    /// <param name="costSizedata">是否计算sizedata的高度</param>
    public void ChildListState(GameObject item, int isShow, bool costSizedata = false)
    {
        List<GameObject> childlist = item.GetComponent<ItemGroup>().childList;
        if (isShow == 2 && childlist.Count > 0)
        {
            isShow = childlist[0].activeSelf == false ? 1 : 0;
        }
        for (int i = 0; i < childlist.Count; i++)
        {
            childlist[i].SetActive(isShow == 1 ? true : false);
            if (costSizedata)
            {
                if (childlist[i].activeSelf)
                    contentSizeData.y += childlist[i].GetComponent<RectTransform>().sizeDelta.y;
                else
                    contentSizeData.y -= childlist[i].GetComponent<RectTransform>().sizeDelta.y;
            }
            childlist[i].GetComponentInChildren<Text>().text = item.name + i;
        }
        contentRt.sizeDelta = contentSizeData;
    }
    /// <summary>
    /// 点击父菜单的时候弹出子菜单
    /// </summary>
    /// <param name="item">父菜单物体</param>
    public void OnClickParent(GameObject item)
    {
        ChildListState(item, 2, true);
        if (lastObj != null)
        {
            if (lastObj != item)
            {
                if (lastObj.GetComponent<ItemGroup>().childList[0].activeSelf)
                    ChildListState(lastObj, 2, true);
            }
        }
        lastObj = item;
    }

原创粉丝点击