为了更贴近游戏实际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. 显示列表的更多相关文章

  1. OpenGL中glVertex、显示列表(glCallList)、顶点数组(Vertex array)、VBO及VAO区别

    OpenGL中glVertex.显示列表(glCallList).顶点数组(Vertex array).VBO及VAO区别 1.glVertex 最原始的设置顶点方法,在glBegin和glEnd之间 ...

  2. NeHe OpenGL教程 第十二课:显示列表

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  3. SharePoint 2013 自定义翻页显示列表项

    项目需求:自定义开发一个能分页显示列表项的小部件,允许左右翻页,能根据用户权限来显示管理链接等. 效果如下: 技术要求:使用sharepoint rest API 来获取列表项,这样性能高,能够快速响 ...

  4. Android BLE与终端通信(二)——Android Bluetooth基础科普以及搜索蓝牙设备显示列表

    Android BLE与终端通信(二)--Android Bluetooth基础搜索蓝牙设备显示列表 摘要 第一篇算是个热身,这一片开始来写些硬菜了,这篇就是实际和蓝牙打交道了,所以要用到真机调试哟, ...

  5. [OpenGL] 斯坦福兔子与显示列表

    1.调整桌子的大小.         在OpenGL绘制长方体,能够通过函数: glutSolidCube(Size)          绘制得到的是一个正方体,再利用缩放矩阵使其变成长方体.使得桌子 ...

  6. .NET MVC+ EF+LINQ 多表联查VIEW显示列表

    1.VIEW 页面显示代码 <link href="~/Content/bootstrap.css" rel="stylesheet" /> < ...

  7. Draw Call(Unity 5中显示为SetPass calls

    Draw Call(Unity 5中显示为SetPass calls

  8. 利用JqGrid结合ashx显示列表之一

    最近项目决定运用JqGrid列表控件显示相关数据,以前接触比较多还是easyui和Ext.Net的列表控件,文章简单写的小实例进行一个总结: 1:引入相关的JS及CSS文件,JqGrid目前可以利用J ...

  9. 字符图元 & 显示列表

    [字符图元] 1.typeface(字样),即设计风格,如Courier等. 2.font(字体),如10磅Courier斜体. 3.monspace即为等宽字体,proportional为非等宽字体 ...

  10. 尝试使用Osg共享渲染描述表(HGLRC)实现多线程编译显示列表--总结

    在realize()前打开预编译选项指令: osg::DisplaySettings::instance()->setCompileContextsHint(true);    mpr_osgv ...

随机推荐

  1. pod setup 慢 的问题

    由于更换了硬盘,重装了系统,需要重新配置环境,发现现在安装cocapods比之前坑更深了, 装环境时遇到pod setup才几kb的下载速度(即使用梯子也是巨慢),实在是没法用在网上尝试了各种方法,但 ...

  2. ubuntu22.04安装vsftp遇到的问题

    问题 FileZilla连接文件服务器时出现"无法读取文件目录",随后出现"20秒后无活动,连接超时"."无法连接到服务器"文件目录无法读取 ...

  3. 即构微信小程序直播组件是什么?有哪些功能?哪些小程序类目可以使用?

    即构直播助手是微信官方认证的微信小程序插件,为开发者提供便捷.强大的微信小程序音视频直播服务. 即构直播助手除了包含微信小程序下的音视频推拉流能力,还支持iOS.Android.Windows.Web ...

  4. 封装一个可以左右滑动的Blazor组件

    为什么要封装组件 最近写MAUI Blazor的时候,总是苦于对移动端没有什么好的支持,没有一个能左右滑动的tab切换组件. 既然没有,那就自己封装一个. 简单了解轮播图.tab切换的库之后,决定使用 ...

  5. TCP超时分析

    参考链接: Linux 建立 TCP 连接的超时时间分析 Linux 建立 TCP 连接的超时时间分析 Linux 系统默认的建立 TCP 连接的超时时间为 127 秒. 2 分 7 秒即 127 秒 ...

  6. 2022-1-11 控件学习4 ItemControl、ListBox、ComboBox

    ItemControl itemControl前台 ItemControl后台 ItemControl一般是竖直排列的,如果需要很想排列需要使用,也可以使用 UniformGrid Columns=& ...

  7. 一个批处理,解决你重装python第三方模块的烦恼~(1.0版本)

    @echo offpip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simplepython -m pip insta ...

  8. DBSCAN聚类

    一.概述   DBSCAN(Density-Based Spatial Clustering of Applications with Noise)是一种基于密度的聚类算法,簇集的划定完全由样本的聚集 ...

  9. [golang]使用gopsutil获取系统信息

    前言 在python中有个psutil库用于获取系统信息,而go语言也有一个类似的库--gopsutil,功能差不多. 项目地址:https://github.com/shirou/gopsutil ...

  10. 利用pytorch准备数据集、构建与训练、保存与加载CNN模型

    本文的主要内容是利用pytorch框架与torchvision工具箱,进行准备数据集.构建CNN网络模型.训练模型.保存和加载自定义模型等工作.本文若有疏漏.需更正.改进的地方,望读者予以指正,如果本 ...