unity编写树形结构的文件管理页面

项目中需要实现点击"+"按钮展开对应分类下的所有训练科目,再次点击"--"按钮将对应分类下的训练科目隐藏并收起整个面板。对此,编写一个类,将其挂载到树形结构的父类上,代码如下:

csharp 复制代码
using UnityEngine;
using UnityEngine.UI;

public class TreeViewParent:MonoBehaviour
{
	public Button controlBtn;//树形展开/收回按钮
	public Sprite[] controlBtnSprite;//按钮切换的样式
	public Transform itemPanel;//树形子分支的父类
	
	public float width;//宽度
	public float itemHeight;//子分支的高度

	void Start()
	{
		controlBtn.onClick.AddListener(onControlBtnClicked);
	}
	
	void onControlBtnClicked()
	{
		if(controlBtn.GetComponent<Image>().sprite == controlBtnSprite[0])
		{
			controlBtn.GetComponent<Image>().sprite = controlBtnSprite[1];
			foreach(Transform item in itemPanel)
				item.gameObject.SetActive(true);
			transform.GetComponent<RectTransform>().sizeDelta = new Vector2(width, itemHeight * (itemPanel.childCount + 1));
		}
		else
		{
			controlBtn.GetComponent<Image>().sprite = controlBtnSprite[0];
			foreach(Transform item in itemPanel)
				item.gameObject.SetActive(false);
			transform.GetComponent<RectTransform>().sizeDelta = new Vector2(width, itemHeight);
		}
	}
}
相关推荐
在路上看风景16 分钟前
31. Unity 异步加载的底层细节
unity
wxin_VXbishe34 分钟前
C#(asp.net)学员竞赛信息管理系统-计算机毕业设计源码28790
java·vue.js·spring boot·spring·django·c#·php
天人合一peng2 小时前
Unity中做表头时像work中整个调整宽窄
unity
bugcome_com12 小时前
零基础入门C#:一篇搞懂核心知识点
c#
小李也疯狂14 小时前
Unity 中的立方体贴图(Cubemaps)
unity·游戏引擎·贴图·cubemap
牛掰是怎么形成的14 小时前
Unity材质贴图引用陷阱:包体暴涨真相
unity·材质·贴图
呆呆敲代码的小Y14 小时前
【Unity工具篇】| 超实用工具LuBan,快速上手使用
游戏·unity·游戏引擎·unity插件·luban·免费游戏·游戏配置表
EQ-雪梨蛋花汤14 小时前
【Unity优化】Unity多场景加载优化与资源释放完整指南:解决Additive加载卡顿、预热、卸载与内存释放问题
unity·游戏引擎
我的offer在哪里14 小时前
用 Unity 从 0 做一个「可以玩的」游戏,需要哪些步骤和流程
游戏·unity·游戏引擎
泡泡茶壶ᐇ15 小时前
Unity游戏开发入门指南:从零开始理解游戏引擎核心概念
unity·游戏引擎