Unity实现QQ列表折叠菜单
程序员文章站
2023-02-19 15:47:54
本文实例为大家分享了unity实现qq列表折叠菜单的具体代码,供大家参考,具体内容如下主要用到了gui的自动布局功能,verticallayoutgroup,注意childcontrolssize 和...
本文实例为大家分享了unity实现qq列表折叠菜单的具体代码,供大家参考,具体内容如下
主要用到了gui的自动布局功能,verticallayoutgroup,注意childcontrolssize 和childforceexpand属性设置为 width
效果:
实现代码:
/// <summary> /// 折叠菜单 /// </summary> public class foldpanel : monobehaviour { [serializefield] private gameobject panelitem; // 折叠页 [serializefield] private titleitem titleitem; [serializefield] private dataitem dataitem; public list<folddata> datalist = new list<folddata>(); private void start() { create(); } public void create() { for (int i = 0; i < datalist.count; i++) { // 创建标题 titleitem title = instantiate(titleitem).getcomponent<titleitem>(); title.settitle(datalist[i].titlename); title.transform.setparent(this.transform); // 创建子折叠面板 gameobject panel = instantiate(panelitem); panel.transform.setparent(this.transform); // 260是折叠页的宽度,30dataitem的高度 panel.getcomponent<recttransform>().sizedelta = new vector3(260,30 * datalist[i].data.count); title.setfoldpanel(panel); panel.setactive(false); // 创建折叠页数据 for (int j = 0; j < datalist[i].data.count; j++) { dataitem item = instantiate(dataitem).getcomponent<dataitem>(); item.transform.setparent(panel.transform); item.setinfo(datalist[i].data[j]); } } } } [system.serializable] public class folddata { public string titlename; public list<itemdata> data; } [system.serializable] public class itemdata { public string username; //public string imagename; public sprite imagename; }
titleitem.cs
public class titleitem : monobehaviour,ipointerclickhandler { [serializefield] private text title; [serializefield] private transform arrow; public bool isfold = true; // 是否是折叠状态 public transform foldpanel; public void onpointerclick(pointereventdata eventdata) { if (isfold) { isfold = false; arrow.dorotate(vector3.zero, 0.1f); if (foldpanel != null) { foldpanel.gameobject.setactive(true); foldpanel.doscaley(1, 0.1f); } } else { isfold = true; arrow.dorotate(new vector3(0, 0, 90), 0.1f); if (foldpanel != null) { foldpanel.doscaley(0, 0.1f).oncomplete(() => { foldpanel.gameobject.setactive(false); }); } } } public void settitle(string _titlename) { title.text = _titlename; } public void setfoldpanel(gameobject panel) { foldpanel = panel.transform; } }
列表的数据来源是在界面上手动配置的,当然如果想要读取本地或者服务器的数据也是可以的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读