为了更贴近游戏实际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. 基于python+django的宠物商店-宠物管理系统设计与实现

    该系统是基于python+django开发的宠物商店-宠物管理系统.是给师妹开发的课程作业.现将源码开放给大家.大家学习过程中,如遇问题可以在github咨询作者. 演示地址 前台地址: http:/ ...

  2. Centos7下创建Oracle用户

    Centos7下创建Oracle用户 Oracle中,一个用户其实就类似于一个数据库,本次就来创建一个新用户 登录 将系统用户切换到oracle用户下 su - oracle -- 启动sqlplus ...

  3. 层叠样式表(CSS)1

    一.css的简介 1.层叠样式表的含义 层叠样式表:css是不仅是表现HTML的语言.还是进行样式修饰的语言 层叠:是对一个元素多次设置同一个样式,层层叠加覆盖,如不同的样式对一html标签进行修饰, ...

  4. Unity的BuildPlayerProcessor:深入解析与实用案例

    Unity BuildPlayerProcessor Unity BuildPlayerProcessor是Unity引擎中的一个非常有用的功能,它可以让开发者在构建项目时自动执行一些操作.这个功能可 ...

  5. 为什么大于 $3$ 的素数可以表示为 $6n\pm1$?

    我们有一个素数 \(p\),都能表示为 \(6n \pm 1\). 为什么呢? 我们设 \(p = 6n \pm k\),\(k\) 可以是 \(0, 1, 2, 3, 4, 5\). 如果 \(k\ ...

  6. 如何创建Windows 10 虚拟机

    一 ,新建Windows 10 虚拟机 1.1 创建新的虚拟机 1,点击创建新的虚拟机 2,选择典型,点击下一步 3,选择稍后安装操作系统,点击下一步. 4,操作系统选择windwos,版本选着Win ...

  7. JaCoCo助您毁灭线上僵尸代码

    一. 现状·问题 随着需求不断迭代,业务系统的业务代码突飞猛进,在你自豪于自己的代码量产出很高时,有没有回头看看线上真正的客户使用量又有多少呢? 费事费力耗费大量人力成本上线的功能,可能一年没人使用, ...

  8. Nginx反向代理服务流式输出设置

    Nginx反向代理服务流式输出设置 1.问题场景 提问:为什么我部署的服务没有流式响应 最近在重构原有的GPT项目时,遇到gpt回答速度很慢的现象.在使用流式输出的接口时,接口响应速度居然还是达到了3 ...

  9. 《SQL与数据库基础》13. 视图

    目录 视图 简介 语法 检查选项 视图更新 视图作用 本文以 MySQL 为例 视图 简介 视图(View)是一种虚拟存在的表.视图中的数据并不在数据库中实际存在,行和列数据来自定义视图的查询中使用的 ...

  10. 云服务器中Linux如何安装宝塔面板?

    作者:西瓜程序猿 主页传送门:https://www.cnblogs.com/kimiliucn 官方使用手册:https://www.kancloud.cn/chudong/bt2017/42420 ...