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 ...
随机推荐
- load initialize总结
load initialize 方法的区别1.调用的方式 - load 根据函数地址调用 - initialize 通过objc_msgsend调用 2.调用时刻 - load runtime 加载类 ...
- 【Python】Beautiful Soup
简介 Beautiful Soup 对象 我全部使用soup表示: Beautiful Soup 简介: 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据. ...
- Codeforces Round #882 (Div. 2) A-D
比赛链接 A 代码 #include <bits/stdc++.h> using namespace std; using ll = long long; int a[107]; int ...
- 浅析本地缓存技术-Guava Cache
1 引言 作为java开发工作者,相信大家对于guava这个工具包都不会太陌生,而对于本地缓存技术guava cache,大家在日常的工作开发中也都有所了解,接下来本文就从各个角度入手来对于Googl ...
- MAUI+MASA Blazor 兼容性测试报告及分析
目录 1. 背景 2. 目的 3. 测试目标 4. 预期结果 5. 测试策略及范围 6. 测试结果与分析 7. 附加内容 8. 结尾 1. 背景 MASA Blazor组件是一款基于Material ...
- 在Java项目中使用redisson实现分布式锁
Redisson自定义注解实现分布式锁 在Java项目中使用Redission自定义注解实现分布式锁: 添加Redission依赖项:在项目的pom.xml中添加Redission依赖项: <d ...
- Centos7下创建Oracle用户
Centos7下创建Oracle用户 Oracle中,一个用户其实就类似于一个数据库,本次就来创建一个新用户 登录 将系统用户切换到oracle用户下 su - oracle -- 启动sqlplus ...
- openpyxl 设置某列单元格样式
1 # 边框线 2 border_set = Border(left=Side(border_style='thin', color='000000'), 3 right=Side(border_st ...
- 但因热爱,愿迎万难,OpenTiny 社区增加一枚前端程序媛贡献者🎉
我们非常高兴地宣布,OpenTiny Vue Playground 正式上线! 链接:https://opentiny.github.io/tiny-vue-playground/ 在此非常感谢 xi ...
- 基于proxysql实现MySQL读写分离
前言 环境: 系统版本:CentOS 7 MySQL版本:5.7.35 MySQL主从配置略过. 安装 # 安装 yum localinstall -y ./proxysql-2.2.0-1-cent ...