我直接上代码了
----------------------------------------------------------
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using CUI.UI;
using CUI.View;
public class UIDropdownMenu : BaseForms
{
public delegate void DropdownMenuItemClickCallback(UIDropdownMenuData data);
private DropdownMenuItemClickCallback m_selectItemCallback = null; // 选择子类回调执行
public Text txtFold; // 选中显示
public Text txtDesc; // 选中显示
private CustomButton DropdownBtn; // 点击按钮
private CustomButton Mask; // 全屏按钮
public GameObject ScrollVertList;
public GameObject parent; // 父级
public UIDropdownMenuItem itemPrefab; // 子类
[Tooltip("是否默认选中第一个")]
public bool isDefaultSelectFrist;
[Tooltip("重复选中是否需要执行回调")]
public bool isRepeatSelectExecuteCallback;
private UIDropdownMenuItem m_selectItem = null; // 选中的
private List<UIDropdownMenuItem> m_itemShows = new List<UIDropdownMenuItem>(); // Items
///考虑了下 暂时不需要用这个 以后看需求把这个放开
//private Dictionary<string, UIDropdownMenuItem> m_dataItemDic = new Dictionary<string, UIDropdownMenuItem>(); // 数据对应物体
public bool IsInitialize { get; private set; } = false;
private void OnOpenScrollUI(CustomButton go)
{
ScrollVertList.gameObject.SetActive(true);
int index = m_itemShows.IndexOf(m_selectItem);
if (index == -1)
{
return;
}
m_selectItem.SetSelectState(true);
}
private void OnCloseScrollUI(CustomButton go)
{
ScrollVertList.gameObject.SetActive(false);
}
public void Add(UIDropdownMenuData data)
{
UIDropdownMenuItem _item = CreateItem();
_item.SetData(data);
//m_dataItemDic.Add(data.Name, _item);
m_itemShows.Add(_item);
}
public void SetSelectItemCallback(DropdownMenuItemClickCallback delegateSelectItem)
{
m_selectItemCallback = delegateSelectItem;
}
public void Begin()
{
if (IsInitialize) return;
IsInitialize = true;
ScrollVertList.gameObject.SetActive(false);
TryGetCompRef("DropdownLabel", ref DropdownBtn);
DropdownBtn.onClickCustom = OnOpenScrollUI;
TryGetCompRef("Mask", ref Mask);
Mask.onClickCustom = OnCloseScrollUI;
if (isDefaultSelectFrist && m_itemShows.Count >= 1)
{
OnClickItem(m_itemShows[0]);
}
}
protected override void OnDestroy()
{
base.OnDestroy();
Clear();
}
public void Clear()
{
for (int i = m_itemShows.Count - 1; i >= 0; i--)
{
RecycleItem(m_itemShows[i]);
}
m_selectItem = null;
m_itemShows.Clear();
IsInitialize = false;
}
//外部调用选中某个
public void OnClickIndex(int index, bool isCallback = true)
{
if (m_itemShows.Count > index)
{
OnClickItem(m_itemShows[index], isCallback);
}
}
private void OnClickItem(UIDropdownMenuItem item, bool isCallback = true)
{
if (item.Data == null) return;
// 已经选中
if (item.SelectState)
{
if (isRepeatSelectExecuteCallback && m_selectItem == item)
{
if (m_selectItemCallback != null && isCallback)
{
m_selectItemCallback(item.Data);
}
return;
}
}
else
{
if (m_selectItem != null)
{
m_selectItem.SetSelectState(false);
}
m_selectItem = item;
m_selectItem.SetSelectState(true);
txtFold.text = m_selectItem.Data.Name;
txtDesc.text = (string)m_selectItem.Data.Data;
if (m_selectItemCallback != null && isCallback)
{
m_selectItemCallback(item.Data);
}
}
ScrollVertList.gameObject.SetActive(false);
}
private UIDropdownMenuItem CreateItem()
{
UIDropdownMenuItem _item = null;
GameObject _go = ToolKit.AddChildIn(parent, itemPrefab.gameObject);
_item = _go.GetComponent<UIDropdownMenuItem>();
//添加点击事件
_item.onClick.AddListener(delegate () { OnClickBtnItem(_item); });
if (_item == null)
{
return null;
}
_item.transform.parent = parent.transform;
_item.gameObject.SetActive(true);
return _item;
}
private void RecycleItem(UIDropdownMenuItem item)
{
item.SetSelectState(false);
//移除事件
item.onClick.RemoveAllListeners();
//m_dataItemDic.Remove(item.Data.Name);
m_itemShows.Remove(item);
Destroy(item);
}
private void OnClickBtnItem(UIDropdownMenuItem item)
{
OnClickItem(item, true);
}
}
```
-------------------------------------------------------------------------
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIDropdownMenuItem : Button
{
public Text txtInfo;
public Text txtDesc;
public Image iconSelect;
public UIDropdownMenuData Data { get; private set; } = null;
public bool SelectState { get; private set; }
public virtual void SetData(UIDropdownMenuData data)
{
Data = data;
if(iconSelect != null) iconSelect.enabled = false;
if (Data != null)
{
txtInfo.text = Data.Name;
if (txtDesc != null) txtDesc.text = (string)Data.Data;
}
else
{
txtInfo.text = "";
}
SetSelectState(false);
}
public virtual void SetSelectState(bool state)
{
SelectState = state;
if (iconSelect != null) iconSelect.enabled = SelectState;
}
}
```
-------------------------------------------------------------------------------------
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIDropdownMenuData
{
public string Name { get; }
public object Data { get; } = null;
public int Index { get; }
/// <summary>
/// 实例化数据类
/// </summary>
/// <param name="index">索引</param>
/// <param name="name">名字</param>
/// <param name="data">自定义数据</param>
public UIDropdownMenuData(int index, string name = "", object data = null)
{
Index = index;
Name = name;
Data = data;
}
}
```
--------------------------------------------------------
//这个类的作用就是防止inspector显示不了item的UI挂件
```
using UnityEditor;
[CustomEditor(typeof(UIDropdownMenuItem))]
public class UIDropdownMenuItemEditor : Editor
{
}
```
____________________________
完事 !