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 ...
随机推荐
- 简约版八股文(day2)
Redis(内存中->非关系型数据库) redis是什么,为什么要用redis redis是基于键值对的NoSQL数据库,经常用来做缓存用户直接读取数据库中的数据效率是相对比较慢的,如果把数据读 ...
- C++笔记(1)——类的基本概念知识
C#和C++的部分面向对象方式类似,只记录不同点. 定义类与关键字: 下面是C++类的定义方式与C++关键字public和private的表达方式: class Box { private: // 私 ...
- Llama2开源大模型的新篇章以及在阿里云的实践
Llama一直被誉为AI社区中最强大的开源大模型.然而,由于开源协议的限制,它一直不能被免费用于商业用途.然而,这一切在7月19日发生了改变,当Meta终于发布了大家期待已久的免费商用版本Llama2 ...
- 【题解】Educational Codeforces Round 150(CF1841)
赛时过了 A-E,然后就开摆了,为什么感觉 C 那么无厘头[发怒][发怒] 排名:25th A.Game with Board 题目描述: Alice 和 Bob 玩游戏,他们有一块黑板.最初,有 \ ...
- python连接 Basler pylon相机遇到的问题
今天使用下图程序去连接相机 以下是摄像头IP参数 电脑IP参数 在确认电脑能够ping通相机的情况下 以及检查专用软件能否访问之后 依然遇到了以下错误 经过了多番调试之后发现即使能够ping通,子网掩 ...
- 2023-7-27 WPF自定义命名空间在xaml中的使用
xaml自定义命名空间 [作者]长生 为啥要用自定义命名空间 这是常见的几种命名空间 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/ ...
- 3D相册 复仇者联盟
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- React中setState的使用与同步异步
在react中,修改状态如果直接使用this.state,不会引起组件的重新渲染,需要通过 this.setState来对组件的属性进行修改. 1.this.setState的两种定义方式 定义初始状 ...
- 桌面应用打包:pyinstaller
1 背景 在使用python开发一些小工具时,如果其他人电脑中没有python环境或者没有安装相应的第三方库,是没办法运行的,而要求对方安装又不现实,尤其是对方不是技术人员,因此如何将一个独立的pyt ...
- AVR汇编(七):位操作和MCU控制指令
AVR汇编(七):位操作和MCU控制指令 位操作指令 SBI / CBI SBI 指令用于设置I/O寄存器中的第 b 位, CBI 指令用于清除I/O寄存器中的第 b 位. 例如: SBI DDRB, ...