对响应式的数据组织结构不太熟, 直接尝试Redux走起

参考资料

Redux的基本概念

state 一个字段用于存储状态

存储state的地方称为"store", 类似Model\DataCenter等, 把所有数据(state)存进去(类似playerprefs), 不对外提供setter方法

实际类似一个全局存储字典, 但难点在于如何组织数据, 比如玩家背包里面N种道具要想办法合理的存进去, 还要方便维护

action 修改数据的指令

要修改state只能通过action. 这样的话通过跟踪action的日志可以很清晰的知道到底做了什么

reducer

把action和reducer连接起来, 实际相当于一个switch-case函数, 在复杂项目中把action划分为多个模块

和常见的MVC模式的对比

普通的MVC模式为每个不同的模块创建一个Model, 通过Controller管理, 优点是对象式存储比较符合直觉

Redux把所有数据存储到唯一一个state中, 通过action来发送指令, 优点是容易追踪操作

Redux的三个原则

  1. 单一数据源 - 整个应用的 state 被储存在一棵 object tree 中,并且这个 object tree 只存在于唯一一个 store 中。
  2. State 是只读的 - 唯一改变 state 的方法就是触发 action,action 是一个用于描述已发生事件的普通对象。
  3. 使用纯函数来执行修改 - 为了描述 action 如何改变 state tree ,你需要编写 reducers。

前两点上面已经说过, 主要说一下第三点

Reducer 只是一些纯函数,它接收先前的 state 和 action,并返回新的 state。刚开始你可以只有一个 reducer,随着应用变大,你可以把它拆成多个小的 reducers,分别独立地操作 state tree 的不同部分,因为 reducer 只是函数,你可以控制它们被调用的顺序,传入附加数据,甚至编写可复用的 reducer 来处理一些通用任务,如分页器。

这里它接收先前的 state 和 action,并返回新的 state, 第一个state指现在的状态参数, 第二个state指返回值

纯函数:

  1. 对于同一参数,返回同一结果
  2. 结果完全取决于传入的参数
  3. 不产生副作用

大概说来Reducer就是

public static Object SomeReducer(Object state, Action action)
{
// 对于static变量只使用get方法
switch (action.actionType)
{
case "SOME_ACTION":
//
state.foo = "bar";
// ... 通过某种方式划分到其他的reducer
} return ...;
}

即假设小明现在14岁(state), 过完生日要加一岁(action), 之后获取最后的年龄结果(state), 伪代码类似这样SomeFunction(14, ["ADD_AGE", 1]) -> 15

redux的官方文档很好理解

function visibilityFilter(state = 'SHOW_ALL', action) {
//...
} function todos(state = [], action) {
//...
} import { combineReducers, createStore } from 'redux'
let reducer = combineReducers({ visibilityFilter, todos })
let store = createStore(reducer)

Redux的基本概念快速看了一遍, 但真正用起来怎么样, 又是另一回事了, 特别返回state这里, 还是很迷惑的

Redux接入

熟悉Redux的概念, 接下来就是接入Redux接口. 首先考虑一个简单的应用:

界面显示一个数, 有两个按钮, 一个把数字加一, 一个把数字减一, 类似UIWidgets给的示例

按照之前statefulwidget的写法, 是这样

using System.Collections.Generic;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.widgets; namespace UI
{
public class HomeRoute : StatefulWidget
{
public override State createState()
{
return new HomeRouteState();
}
} class HomeRouteState : State<HomeRoute>
{
private int m_Count = 0; public override Widget build(BuildContext context)
{
Scaffold scaffold = new Scaffold(
appBar: new AppBar(
title: new Text("首页")
),
body: new Center(
child: new Column(
children: new List<Widget>
{
new Text(
data: m_Count.ToString(),
style: new TextStyle(
fontSize: 30
)
),
new Padding(padding: EdgeInsets.symmetric(10)),
new RaisedButton(
child: new Icon(Icons.add),
onPressed: () =>
{
setState(() =>
{
m_Count++;
});
}
),
new Padding(padding: EdgeInsets.symmetric(10)),
new RaisedButton(
child: new Icon(Icons.remove),
onPressed: () =>
{
setState(() =>
{
m_Count--;
});
}
),
}
)
)
); return scaffold;
}
}
}

如果使用redux后, 把存储在控件中的m_Count移到全局的store中, 修改操作也通过action执行.

但首先需要接入redux

参考UIWidgets提供的示例"CounterAppSample.cs"

GlobalState.cs

namespace UI
{
public class GlobalState
{
public int Count { get; private set; } public GlobalState(int count = 0)
{
Count = count;
}
}
}
Actions.cs

namespace UI
{
public class AddAction
{
public int amount;
} public class RemoveAction
{
public int amount;
}
}
HomeRoute.cs

using System.Collections.Generic;
using Unity.UIWidgets;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.Redux;
using Unity.UIWidgets.widgets; namespace UI
{
public class HomeRoute : StatelessWidget
{
public override Widget build(BuildContext context)
{
Store<GlobalState> store = new Store<GlobalState>(
reducer: Reducer,
initialState: new GlobalState()
); return new StoreProvider<GlobalState>(store, GetWidget());
} //因为Reducer签名问题, 参数为object, 可以修改源码"xxxuiwidgets\Runtime\redux\store.cs"自定义为IAction接口, 不推荐
public static GlobalState Reducer(GlobalState state, object action)
{
if (action is AddAction)
{
return new GlobalState(state.Count + ((AddAction)action).amount);
}
else if (action is RemoveAction)
{
return new GlobalState(state.Count - ((RemoveAction)action).amount);
}
else
{
// action不匹配时返回原state
return state;
}
} private Widget GetWidget()
{
Scaffold scaffold = new Scaffold(
appBar: new AppBar(
title: new Text("首页")
),
body: new Center(
child: new Column(
children: new List<Widget>
{
new StoreConnector<GlobalState, string>(
converter: (state) => state.Count.ToString(),
builder: (context, text, dispatcher) => new Text(
data: text,
style: new TextStyle(
fontSize: 30
)
),
pure: true // 这个参数不知道干嘛用的
),
new Padding(padding: EdgeInsets.symmetric(10)),
new StoreConnector<GlobalState, object>(
converter: (state) => null,
builder: (context, _, dispatcher) => new RaisedButton(
child: new Icon(Icons.add),
onPressed: () =>
{
dispatcher.dispatch(new AddAction { amount = 1 });
}
),
pure: true
),
new Padding(padding: EdgeInsets.symmetric(10)),
new StoreConnector<GlobalState, object>(
converter: (state) => null,
builder: (context, _, dispatcher) => new RaisedButton(
child: new Icon(Icons.remove),
onPressed: () =>
{
dispatcher.dispatch(new RemoveAction { amount = 1 });
}
),
pure: true
),
}
)
)
); return scaffold;
}
}
}

到现在产生的疑问

上面的代码运行起来没问题, 但在之前没完全理解的地方, 疑问出现了. 把state类起名为"GlobalState", 说明我是把这个看做存储所有全局状态的, 但参照reducer, 每次返回一个新的GlobalState类, 如果每次复制更新造成很大浪费, 似乎不太对

redux的文档中提到

注意 todos 依旧接收 state,但它变成了一个数组!现在 todoApp 只把需要更新的一部分 state 传给 todos 函数,todos 函数自己确定如何更新这部分数据。这就是所谓的 reducer 合成,它是开发 Redux 应用最基础的模式。

即有的地方是传state的一个部分过去的


function todoApp(state = initialState, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return Object.assign({}, state, {
visibilityFilter: action.filter
})
case ADD_TODO:
return Object.assign({}, state, {
todos: todos(state.todos, action) // 注意这里, 传递的是一个部分
})
case TOGGLE_TODO:
return Object.assign({}, state, {
todos: todos(state.todos, action)
})
default:
return state
}
}

在js这么搞没问题, 强类型的C#\UIWidgets中要怎么实现?

Unity - UIWidgets 5. Redux接入(一) 基本接入的更多相关文章

  1. 【Unity游戏开发】接入UWA_GOT的iOS版SDK以后无法正常出包

    一.正文 问: RT,最近有看到UWA_GOT工具新增了iOS版本的支持,于是下载了最新的工具包进行了接入测试.是按照文档直接将UWA_GOTv2.0.1_iOS.unitypackage导入进了Un ...

  2. Unity使用TUIO协议接入雷达

    本篇文章不介绍Unity.TUIO.雷达是什么以及有什么作用.刚接触TUIO的亲们,建议直接硬刚.至于刚接触Unity的亲,这边建议亲直接放弃治疗呢 下面开始正儿八经的教程 需要准备的东西 Unity ...

  3. Unity 之 Redux 模式(第一篇)—— 人物移动

    作者:软件猫 日期:2016年12月6日 转载请注明出处:http://www.cnblogs.com/softcat/p/6135195.html 在朋友的怂恿下,终于开始学 Unity 了,于是有 ...

  4. iOS Unity 项目解析

    本文旨在记录Unity 导出的iOS 项目笔记,另带接入SDK的终极方案,顺带对比Android 项目 1蓝色的目录 Data 这个就是项目的数据,每个项目不一样也就是这个目录不一样,是不是可以把这个 ...

  5. 干掉Unity3D

    我为什么想干掉Unity3D? 这个问题容我不答,每个做技术的人总有一些完美主义. 你使用u3d的过程中是不是痛并快乐着呢. 就从两个国内具有相当普遍性的痛点说起. il2cpp,unity作出了这个 ...

  6. Unity_UIWidgets - 组件Container

    Unity_UIWidgets - 组件Container Container 构造 效果 结语 QQ 今日无推荐 Unity_UIWidgets - 组件Container 上周给大家讲完了Scaf ...

  7. Unity_UIWidgets - 组件AppBar

    Unity_UIWidgets - 组件AppBar AppBar 构造 构造png观看 使用代码 使用效果 AppBar使用结束 结语 图标Icon QQ 今日无推荐 Unity_UIWidgets ...

  8. Unity_UIWidgets新手入门

    Unity_UIWidgets新手入门 Hello Everyone!好久没见了,有没有有些想念小黑呢?什么?这么想?哈哈哈哈哈哈,不过我也知道你是想了解新的知识了,才不是想我嘞. 好了,好歹也半年没 ...

  9. Unity - 接入Android SDK

    在网络上,关于Unity与Android如何进行交互,雨松MOMO大神已经有两篇文章简单介绍了如何操作(1)Unity3D研究院之打开Activity与调用JAVA代码传递参数(2)Unity3D研究 ...

  10. unity工程接入Android sdk后真机测试解锁屏后退出的解决

    unity工程接入如91.移动支付等Android sdk后,真机运行尤其是在4.0+以上坏境,往往会出现解锁屏后退出的情况,解决办法如下: 可以在AndroidManifest.xml中所有的con ...

随机推荐

  1. 理解ffmpeg

    ffmpeg是一个完整的.跨平台的音频和视频录制.转换和流媒体解决方案. 它的官网:https://ffmpeg.org/ 这里有一份中文的文档:https://ffmpeg.p2hp.com/ ff ...

  2. Reactjs学习笔记

    本篇是关于React的简介 ReactJS是Facebook推出的一款前端框架,2013年开源,提供了一种函数式编程思想,拥有比较健全的文档和完善的社区,在React16的版本中对算法进行了革新,称之 ...

  3. Codeforces Round #885 (Div. 2) A-D

    比赛链接 A 代码 #include <bits/stdc++.h> using namespace std; using ll = long long; bool solve() { i ...

  4. 介绍一个简易的MAUI安卓打包工具

    介绍一个简易的MAUI安卓打包工具 它可以帮助进行MAUI安卓的打包. 虽然也是用MAUI写的,但是只考虑了Windows版本,mac还不太会. 没什么高级的功能,甚至很简陋,它能做的,只是节省你从M ...

  5. MySQL8_SQL语法

    MySQL8_SQL语法 SQL 全称 Structured Query Language,结构化查询语言.操作关系型数据库的编程语言,定义了一套操作关系型数据库统一标准 . 一.SQL通用语法 在学 ...

  6. 学习Linux,要把握哪些重点?

    学习Linux,要把握哪些重点? 不知道有没有想学习Linux,但又把握不住学习重点,找不到合适的学习方法的小伙伴,反正我刚开始学习Linux时就像无头苍蝇似的"乱撞",没有把握住 ...

  7. 【技术积累】Linux中的命令行【理论篇】【六】

    as命令 命令介绍 在Linux中,as命令是一个汇编器,用于将汇编语言源代码转换为可执行的目标文件.它是GNU Binutils软件包的一部分,提供了一系列用于处理二进制文件的工具. 命令说明 as ...

  8. spring-mvc 系列:注解开发(SpringMVCConfig、SpringConfig、AbstractAnnotationConfigDispatcherServletInitializer详细配置)

    目录 一.创建初始化类,代替web.xml 二.创建SpringConfig配置类,代替Spring的配置文件 三.创建SpringMVC配置类,代替SpringMVC的配置文件 四.测试功能 使用配 ...

  9. MySQL 1130错误原因及解决方案

    错误:ERROR 1130: Host 'http://xxx.xxx.xxx.xxx' is not allowed to connect to thisMySQL serve 错误1130:主机x ...

  10. centos7.X安装mysql5.7 – 东凭渭水流

    1.下载mysql5.7 可以使用windows下载好后上传至Linux.网络条件好的推荐使用 wget https://dev.mysql.com/get/Downloads/MySQL-5.7/m ...