先上效果图
本文利用VerticalLayoutGroup组件动态生成目录,目录数据由文件目录转换而成。无需其他设置。只需将TreeMenuCheckBox脚本挂载到某个对象上 然后指定属性面板的值既可。

直接上代码 一共两个脚本 TreeMenuCheckBox负责创建目录 UIMenuClick负责点击事件的监听
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UIMenuClick : MonoBehaviour ,IPointerUpHandler,IPointerDownHandler,IDragHandler,IBeginDragHandler,IEndDragHandler{
public GameObject Group = null;
public GameObject Identifying = null;
public System.Action<RectTransform> Refresh;
private bool IsDrag = false;
public void OnBeginDrag(PointerEventData eventData)
{
IsDrag = true;
GetParntScrollRect().OnBeginDrag(eventData);
}
public void OnDrag(PointerEventData eventData)
{
GetParntScrollRect().OnDrag(eventData);
}
public void OnEndDrag(PointerEventData eventData)
{
IsDrag = false;
GetParntScrollRect().OnEndDrag(eventData);
}
public void OnPointerDown(PointerEventData eventData)
{
}
public void OnPointerUp(PointerEventData eventData)
{
if (IsDrag)
return;
if (Group!=null)
{
Group.SetActive(!Group.activeSelf);
RefreshIdentifying();
Refresh?.Invoke(Group.GetComponent<RectTransform>());
}
}
public static UnityEngine.UI.ScrollRect scrollRect;
public UnityEngine.UI.ScrollRect GetParntScrollRect()
{
if (scrollRect==null)
scrollRect = GameObject.FindObjectOfType<TreeMenuCheckBox>().GetComponent<UnityEngine.UI.ScrollRect>()

本文介绍如何使用Unity3D的VerticalLayoutGroup组件动态生成树状菜单,通过脚本TreeMenuCheckBox实现目录结构的创建,配合UIMenuClick脚本处理菜单点击事件,无需额外设置即可实现菜单的折叠与展开。
最低0.47元/天 解锁文章
2305

被折叠的 条评论
为什么被折叠?



