Unity - UIWidgets 6. 显示列表
为了更贴近游戏实际ui的效果和使用环境, 从而讨论上一节遗留的问题, 列表显示是必不可少的
参考
修改之前的HomeRoute,
private Widget CreateListTest()
{
ListView listView = ListView.builder(
scrollDirection: Axis.vertical,
itemExtent: 20,
itemCount: 100,
itemBuilder: (context, index) =>
{
return new Text(data: index.ToString());
}
);
return listView;
}
private Widget GetWidget()
{
Scaffold scaffold = new Scaffold(
appBar: new AppBar(
title: new Text("首页")
),
body: CreateListTest()
);
return scaffold;
}
创建了一个列表, 显示从0到99的数字, 每个列表项高度为20(逻辑高度), 效果如下

通过UIWidgets Inspector可以看到列表项是循环回收的(即看不到的列表项不作为一个UI节点存在), 但这个节点大概已经嵌套了四五十层... 对性能不太信任
显示背包道具
下面要做的是模拟一个显示背包道具的操作, 一个简单的列表显示玩家背包中的每种道具的名字和一句说明(为简化, 道具通通不可叠加)
即提前准备好state, 暂时没有action
首先我准备了几个道具
// Item.cs
using System.Collections.Generic;
namespace Data
{
// 道具的数据类, 非常简单
public class Item
{
public int id;
public string name;
public string description;
// 临时模拟一个配置表
public static Dictionary<int, Item> Table = new Dictionary<int, Item>()
{
{ 1, new Item { id = 1, name = "木棍", description = "一根没什么用的木棍" } },
{ 2, new Item { id = 2, name = "石头", description = "一块没什么用的石头" } },
{ 3, new Item { id = 3, name = "干草", description = "一把没什么用的干草" } },
};
}
}
然后给主角的背包里塞了10个木棍, 10个石头, 5个干草
// GlobalState.cs
using System.Collections.Generic;
namespace UI
{
public class GlobalState
{
public List<int> Items;
public GlobalState()
{
Items = new List<int>
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3,
};
}
}
}
修改列表显示
// HomeRoute.cs
private Widget CreateListTest()
{
var widget = new StoreConnector<GlobalState, List<int>>(
converter: (state) => state.Items,
builder: (context, list, dispatcher) => ListView.builder(
itemExtent: 20,
itemCount: list.Count,
itemBuilder: (context2, index) =>
{
int itemId = list[index];
Data.Item data = Data.Item.Table[itemId];
return new Text(data: $"道具名称: {data.name}, 说明: {data.description}");
}
)
);
return widget;
}
这么写从直觉上不太对, 但目前成功了

修改背包道具
参照前面的写法给增加两个功能: 点击道具列表项使用\获取一个新道具
为了更直观删除了大部分道具
GlobalState.cs
using System.Collections.Generic;
namespace UI
{
public class GlobalState
{
public List<int> Items;
public GlobalState()
{
UnityEngine.Debug.Log("创建了新的GlobalState");
Items = new List<int>
{
1, 2, 3,
};
}
public GlobalState(List<int> items)
{
UnityEngine.Debug.Log("创建了新的GlobalState, 传递了items");
Items = items;
}
}
}
// Actions.cs
namespace UI
{
public class UseBagItemAction
{
public int positionIndex;
}
public class GetItemAction
{
public int itemId;
}
}
// HomeRoute.cs
public static GlobalState Reducer(GlobalState state, object action)
{
if (action is UseBagItemAction)
{
int posId = ((UseBagItemAction)action).positionIndex;
// 这里参照了Redux规范, 复制一份新的再修改, 下同
List<int> items = new List<int>(state.Items);
items.RemoveAt(posId);
return new GlobalState(items);
}
if (action is GetItemAction)
{
int itemId = ((GetItemAction)action).itemId;
List<int> items = new List<int>(state.Items);
items.Add(itemId);
return new GlobalState(items);
}
return state;
}
private Widget CreateListTest()
{
var showList = new StoreConnector<GlobalState, List<int>>(
converter: (state) => state.Items,
builder: (context, list, dispatcher) => ListView.builder(
//itemExtent: 20,
itemCount: list.Count,
itemBuilder: (context2, index) =>
{
int itemId = list[index];
Data.Item data = Data.Item.Table[itemId];
return new RaisedButton(
child: new Text(data: $"{index} - 道具名称: {data.name}, 说明: {data.description}"),
onPressed: () =>
{
dispatcher.dispatch(new UseBagItemAction { positionIndex = index });
}
);
}
)
);
var btn = new StoreConnector<GlobalState, object>(
converter: (state) => null,
builder: (context, _, dispathcer) => new RaisedButton(
child: new Text("获得一根木棍"),
onPressed: () =>
{
dispathcer.dispatch(new GetItemAction { itemId = 1 });
}
)
);
var widget = new Column(
children: new List<Widget> {
// 画面溢出时直接报错, 限制一下高度
new Container(
child: showList,
height: 400
),
btn,
}
);
return widget;
}

使用三个道具, 获得三个木棍, 没什么问题
之后就需要讨论ListItem更复杂化\动效\效率优化\代码重构等等问题了, 和这次的主题没有关系
Unity - UIWidgets 6. 显示列表的更多相关文章
- OpenGL中glVertex、显示列表(glCallList)、顶点数组(Vertex array)、VBO及VAO区别
OpenGL中glVertex.显示列表(glCallList).顶点数组(Vertex array).VBO及VAO区别 1.glVertex 最原始的设置顶点方法,在glBegin和glEnd之间 ...
- NeHe OpenGL教程 第十二课:显示列表
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- SharePoint 2013 自定义翻页显示列表项
项目需求:自定义开发一个能分页显示列表项的小部件,允许左右翻页,能根据用户权限来显示管理链接等. 效果如下: 技术要求:使用sharepoint rest API 来获取列表项,这样性能高,能够快速响 ...
- Android BLE与终端通信(二)——Android Bluetooth基础科普以及搜索蓝牙设备显示列表
Android BLE与终端通信(二)--Android Bluetooth基础搜索蓝牙设备显示列表 摘要 第一篇算是个热身,这一片开始来写些硬菜了,这篇就是实际和蓝牙打交道了,所以要用到真机调试哟, ...
- [OpenGL] 斯坦福兔子与显示列表
1.调整桌子的大小. 在OpenGL绘制长方体,能够通过函数: glutSolidCube(Size) 绘制得到的是一个正方体,再利用缩放矩阵使其变成长方体.使得桌子 ...
- .NET MVC+ EF+LINQ 多表联查VIEW显示列表
1.VIEW 页面显示代码 <link href="~/Content/bootstrap.css" rel="stylesheet" /> < ...
- Draw Call(Unity 5中显示为SetPass calls
Draw Call(Unity 5中显示为SetPass calls
- 利用JqGrid结合ashx显示列表之一
最近项目决定运用JqGrid列表控件显示相关数据,以前接触比较多还是easyui和Ext.Net的列表控件,文章简单写的小实例进行一个总结: 1:引入相关的JS及CSS文件,JqGrid目前可以利用J ...
- 字符图元 & 显示列表
[字符图元] 1.typeface(字样),即设计风格,如Courier等. 2.font(字体),如10磅Courier斜体. 3.monspace即为等宽字体,proportional为非等宽字体 ...
- 尝试使用Osg共享渲染描述表(HGLRC)实现多线程编译显示列表--总结
在realize()前打开预编译选项指令: osg::DisplaySettings::instance()->setCompileContextsHint(true); mpr_osgv ...
随机推荐
- Github秒变VSCode在线编辑器
在仓库页面 在网页地址中github后面加上 1s 即可
- RTC+AI|“即智”数智人创新内容生产体验,为企业降本增效再提速
号称"史上最卷"的618年中大促落下帷幕,几大电商巨头在直播投入和短视频内容建设上持续加码,短视频+直播电商的营销模式成为618期间的主要输出. 以某美妆专场直播间为例,主播现场手 ...
- ISP图像处理—紫边Purple Fringing
图像紫边存在数码相机.监控摄像头等数字成像图像,使用设备在逆光.大光圈条件下拍摄图像的高反差区域容易出现紫边,解决图像自编问题有助设备得到完美图像. 紫边成因分析和确定有助与紫边消除的图像处理算法研究 ...
- 离线自动化部署CDH
离线CDH集群自动化部署工具 离线CDH集群安装与部署的自动化脚本工具,简单支持「离线一键装机」. 脚本将对系统配置做出一定修改,使用前请务必确认当前服务器无其他人员.任务使用,以免造成不必要的麻烦, ...
- debian 防火墙命令 nft
参考链接:nftables # which nft /usr/sbin/nft # dpkg -S /usr/sbin/nft nftables: /usr/sbin/nft # dpkg -L nf ...
- .net通用RSA加密工具类
目前最流行的加密算法莫过于RSA了,以下是我们.net/.net core C#生成环境用的RSA加密工具类,在此分享给大家. using System; using System.IO; using ...
- 无linux基础也能熟练掌握git的基本操作
git是一个用来管理项目的工具,它的远程仓库有github.gitee.gitlab代码托管中心,既可以用于个人共享代码,又可以用于团队进行项目的协作与发布,那么我们一起来了解一下git该如何使用~ ...
- golang1.21新特性速览
经过了半年左右的开发,golang 1.21 在今天早上正式发布了. 这个版本中有不少重要的新特性和变更,尤其是在泛型相关的代码上. 因为有不少大变动,所以建议等第一个patch版本也就是1.21.1 ...
- 痞子衡嵌入式:AppCodeHub - 一站网罗恩智浦MCU应用程序
近日,恩智浦官方隆重上线了应用程序代码中心(Application Code Hub,简称 ACH),这是恩智浦 MCUXpresso 软件生态的一个重要组成部分.痞子衡之所以要如此激动地告诉大家这个 ...
- C#利用Refit实现JWT自动续期
前言 笔者之前开发过一套C/S架构的桌面应用,采用了JWT作为用户的登录认证和授权.遇到的唯一问题就是JWT过期了该怎么办?设想当一个用户正在进行业务操作,突然因为Token过期失效,莫名其妙地跳转到 ...