表示层
ZProRx 架构也是类似MVC的分层,这一节主要介绍如何实现表示层与数据层的绑定。
目前只支持与Untiy表示层(UGUI/GameObject)的绑定。
绑定 Unity Bind
把属性与Unity元素进行Bind 这部分提供了如何与UI能进行自动化的绑定,动态更新等机制,同时对一些常用框架的支持,比如上接UGUI、向下与UniRX、ZECS等架构对接。同时也支持创建动态UIItem,比如对一些列表的支持等。当然这里的可视化绑定也适用于3D表现。 需求实例(以下面率土为例,列举常用UI Item)
可以分解出如下类型的属性,以及对应的UI表现。
- 等级/稀有度:int/byte类型,使用小星星作为表现
- 血条/兰条:float类型,进度条表现,也会有数值显示,参考包括最大值、颜色。其实就是做为不同的Prefab但是UI类是一个。
- 数值属性:比如攻击力防御值等属性。会显示附加值即“()”里的内容。类型的附加值,如果固定可通过Attribute的方式进行定义,当然也可以使用下面介绍的复合类型。
- ICON:类型左侧卡片上的攻击距离的表现,由一个图标和数值组成。
- 复合类型:即左侧卡片做为一个整体属性,其本身又由子属性组成。如下代码所示,武器类包括子属性。
public class Weapon {
[PropertyDescription("power", "a power data")]
public ZProperty<float> power = new ZProperty<float>();
}
public class Person {
public string TestID;
[PropertyDescription("等级", "玩家的等级信息")]
public ZProperty<int> rank = new ZProperty<int>();
[PropertyDescription("血量", "玩家的名字")]
[PropertyUIItemRes("Test/Image")]
public ZProperty<int> blood = new ZProperty<int>();
[PropertyDescription("weapon007", "a power weapon")]
public ZProperty<Weapon> weapon = new ZProperty<Weapon>();
}
如下图,可以看我们在定义表示层时,通过名字与ZProperty 属性进行绑定。(后续考虑自动生成表示层、以及表示层的Check功能)
Unity 支持
GameObject.Find("Monster/Arm/Hand")
MonoBehaviour 的以下行为支持,目前只支持前三个
Awake()
Start()
Update()
FixedUpdate()
LateUpdate()
OnGUI()
OnDisable()
OnEnable()
OnDestroy()
Event 绑定
目前只支持与Button进行绑定。 对于3D View需要支持可点击的3D对象(控件), 需要支持RTClickable
脚本的动态绑定。 RTClickable
如果想要生效,需要依赖EventSystem和Physics RayCaster组件绑定Camera上。 详细的应用参考EventSystem应用于3D GameObject
对于Unity Event的绑定,大概分两种情况,一种是UGUI相关,由ZViewBuildTools 进行绑定,比如 Button,通过
btn.onClick.AddListener(()=> {
prop.Invoke();
});
其是通过对UnityEvent
的操作。
一种对于GameObject的三维相关事件,比如 基于EventTrigger的单击事件等。这里未来还需要对加回 UniRX.Trigger的支持。
控件
定义基本的丰富的预制件,如下
- ZUIInputPropertyItem: 用于获取用户输入,支持Placeholder,并能与ZProperty属性自动绑定。
Data为预留的子结点,用于显示其Title,由[PropertyDescription] Attribute进行定义。
支持以下共通的预留的子结点名
public static class ZViewCommonObject{
public static string Name = "Name";
public static string Description = "Description";
public static string Data = "Data";
public static string Icon = "Icon";
//public static string Count = "Count";
public static string Bar = "Bar";
public static string Root = "Root";
public static string Items = "Items";
public static string Model = "Model";
public static string Minute = "Minute";
public static string Second = "Second";
}
更多的控件请参考项目,如下为Unity Package中的Demo 展示 ,列举也目前支持的大部分预制。
相关链接:
项目开源地址:https://github.com/bennychao/ZProRx.Lib
下载安装:(ZP.Lib.Server 是ZRroRx的核心库,后续其它功能库陆续开放中)
- .Net CLI
>dotnet add package ZP.Lib.Server --version 1.0.3
- 或 VS 2019 Nuget 包管理中搜索"ZP.Lib.Server"并安装(推荐)
ZProRx.Lib Unity Package 1.0.3 (用于在Untiy中使用)下载地址:
https://github.com/bennychao/ZProRx.Lib/releases/
https://github.com/bennychao/ZProRx.Lib/Publish/ZProRx.Lib.package
下一节