- 最新项目需求要求做一个下拉菜单列表(树形下拉菜单)。但UGUI并没有原生的插件,只能自己实现。
思路每个级的ItemPanel(一个单级菜单条)样式是一样的,于是需要一个panel来装所有的itemPanel,在整个菜单 顶部panel上添加vertically layout group组件,使下面的子菜单从上到下依次排列。文章最后会附上 demo下载地址 - 效果图
- 具体实现新建Canvas->Panel,
粘出部分代码,详细可以下载demo
控制端
public class PullDownList : MonoBehaviour
{
private List itemPanelList;
public GameObject itemPanel;
private void Awake()
{
itemPanelList = new List();
}
// Use this for initialization
void Start()
{
for (int i = 0; i < 10; i++)
{
GameObject newItemPanel = Instantiate(itemPanel);
itemPanelList.Add(newItemPanel);
newItemPanel.GetComponent().SetBaseParent(this.transform);
newItemPanel.GetComponent().InitPanelContent(new ItemBean("一级菜单" + i, i));
}
for (int i = 0; i < 5; i++)
{
GameObject newItemPanel2 = Instantiate(itemPanel);
itemPanelList.Add(newItemPanel2);
newItemPanel2.GetComponent().SetItemParent(itemPanelList[i].GetComponent());
newItemPanel2.GetComponent().InitPanelContent(new ItemBean("二级菜单" + i, i));
}
for (int i = 0; i < 2; i++)
{
GameObject newItemPanel3 = Instantiate(itemPanel);
itemPanelList.Add(newItemPanel3);
newItemPanel3.GetComponent().SetItemParent(itemPanelList[11].GetComponent());
newItemPanel3.GetComponent().InitPanelContent(new ItemBean("三级菜单" + i, i));
}
}
}
itempanel端
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ItemPanelBase : MonoBehaviour
{
private List childList;//子物体集合
[HideInInspector]
public Button downArrow;//下箭头按钮
public Sprite down, right,dot;
public bool isOpen { get; set; }//子物体开启状态
private Vector2 startSize;//起始大小
private void Awake()
{
childList = new List();
downArrow = this.transform.Find("ContentPanel/ArrowButton").GetComponent
- 最后附上项目地址 demo下载地址



