Unity3D利用VerticalLayoutGroup组件制作(折叠菜单、树状菜单)

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

先上效果图在这里插入图片描述
本文利用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>()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值