Unity WidgetsUI CreateTaskView Demo
Creating own ListView
- item class to keep data
- component class to display item, derived from ListViewItem
- ListView class derived from ListViewCustom
namespace UIWidgetsSamples.Tasks {
[System.Serializable]
public class Task {
public string Name;
public int Progress;
}
}
using UnityEngine.UI;
using UIWidgets;
namespace UIWidgetsSamples.Tasks {
public class TaskComponent : ListViewItem {
public Text Name;
public Progressbar Progressbar;
// IViewData<Task> implementation
public void SetData(Task item)
{
Name.text = item.Name;
Progressbar.Value = item.Progress;
}
}
}
using UIWidgets;
namespace UIWidgetsSamples.Tasks {
public class TaskView : ListViewCustom<TaskComponent,Task> {
}
}

Add ScrollRect component to GameObject. With this ListView will be scrollable.
This and some following steps can be skipped if ScrollView gameobject available in your Unity version.


This restricts ListView items to the shape of the Viewport.
Mask can be replaced with RectMask2D.

Set EasyLayout: Layout Type = Grid; Grid Constraint = Fixed Column Count; Grid Constraint Count = 1.
Set Content Size Fitter: Vertical Fit = Preferred Size.
For Horizontal ListView you should set Grid Constraint = Fixed Row Count and Horizontal Fit = Preferred Size instead.
List will be contain items gameobjects and control layout.
You can use Vertical Layout Group or Horizontal Layout Group instead EasyLayout, but in this case changing ListView direction will be impossible in runtime.


Set Text vertical alignment to middle.




Attach ScrollRect GameObject to ScrollRect field.
Attach List GameObject to Container field.
Attach DefaultItem GameObject to DefaultItem field.



Set TaskView colors:
DefaultColor = 255, 215, 115, 255
DefaultBackgroundColor = 255, 215, 115, 0 (transparent)
HighlightedColor = 0, 0, 0, 255 (black)
HighlightedBackgroundColor = 181, 122, 36, 255
SelectedColor = 0, 0, 0, 255 (black)
SelectedBackgroundColor = 196, 156, 39, 255



Background color changes on selection and mouse over.

For this override GraphicsForeground property in component class. (And you can also override GraphicsBackground property.)
GraphicsForeground and GraphicsBackground should return array of Graphic objects for coloring.
using System.Collections;
using System.Collections.Generic;
using UIWidgets;
using UnityEngine;
public class TaskView : ListViewCustom<TaskComponent,Task> {
public static readonly System.Comparison<Task> ItemComparsion = (x, y) => x.Name.CompareTo(y.Name);
public override void Start()
{
base.Start();
DataSource.Comparison = ItemComparsion;//通过ItemComparsion这个来进行对数据进行排序
}
//改变
protected override void SetData(TaskComponent component, Task item)
{
component.SetData(item);
}
//===根据状态动态改变文本颜色
protected override void HighlightColoring(TaskComponent component)
{
base.HighlightColoring(component);
component.Name.color = HighlightedColor;
}
protected override void SelectColoring(TaskComponent component)
{
base.SelectColoring(component);
component.Name.color = SelectedColor;
}
protected override void DefaultColoring(TaskComponent component)
{
base.DefaultColoring(component);
component.Name.color = DefaultColor;
}
//==========================
}

Let's implement automatic sort for items.
ItemsComparison function compare tasks by name and used to sort items.
using UIWidgets;
namespace UIWidgetsSamples.Tasks {
public class TaskView : ListViewCustom<TaskComponent,Task> {
public static readonly System.Comparison<Task> ItemsComparison = (x, y) => x.Name.CompareTo(y.Name);
bool isStartedTaskView = false;
public override void Start()
{
if (isStartedTaskView)
{
return ;
}
isStartedTaskView = true;
base.Start();
DataSource.Comparison = ItemsComparison;
}
}
}


Replace Task Progress field with property and add OnProgressChange event.
So now we can get event when Progress value changed.
using UnityEngine;
using UnityEngine.Serialization;
using UIWidgets;
namespace UIWidgetsSamples.Tasks {
[System.Serializable]
public class Task {
public string Name;
[SerializeField]
[FormerlySerializedAs("Progress")]
int progress;
public int Progress {
get {
return progress;
}
set {
progress = value;
if (OnProgressChange!=null)
{
OnProgressChange();
}
}
}
public event OnChange OnProgressChange;
}
}
When Progress value changed will be called UpdateProgressbar function.
using UnityEngine.UI;
using UIWidgets;
namespace UIWidgetsSamples.Tasks {
public class TaskComponent : ListViewItem, IViewData<Task> {
public override Graphic[] GraphicsForeground {
get {
return new Graphic[] {Name, };
}
}
public Text Name;
public Progressbar Progressbar;
Task item;
public Task Item {
get {
return item;
}
set {
if (item!=null)
{
item.OnProgressChange -= UpdateProgressbar;
}
item = value;
if (item!=null)
{
Name.text = item.Name;
Progressbar.Value = item.Progress;
item.OnProgressChange += UpdateProgressbar;
}
}
}
public void SetData(Task item)
{
Item = item;
}
void UpdateProgressbar()
{
Progressbar.Animate(item.Progress);
}
protected override void OnDestroy()
{
Item = null;
}
}
}
It will add task and task progress will be updated every second on random value in range 1..10.
using UnityEngine;
using System.Collections;
namespace UIWidgetsSamples.Tasks {
public class TaskTests : MonoBehaviour
{
public TaskView Tasks;
public void AddTask()
{
var task = new Task(){Name = "Random Task", Progress = 0};
Tasks.DataSource.Add(task);
StartCoroutine(UpdateProgress(task, 1f, Random.Range(1, 10)));
}
IEnumerator UpdateProgress(Task task, float time, int delta)
{
while (task.Progress < 100)
{
yield return new WaitForSeconds(time);
task.Progress = Mathf.Min(task.Progress + delta, 100);
}
}
}
}
Add AddTask() to OnClick event.

Unity WidgetsUI CreateTaskView Demo的更多相关文章
- Unity ---WidgetsUI CreateTileView Demo
以下教程基于:WidgetsUI 第三方扩展包 WidgtsUI 官网文档地址:https://ilih.ru/unity-assets/UIWidgets/docs/ 1.创建一个空GameObje ...
- Unity制作FPS Demo
等到把这个Unity FPS Demo[僵尸杀手]完成后再详细补充一下,使用Unity制作FPS游戏的经历,今天做个标识.
- Unity VR-播放demo模型后无法移动视角
资源源于:小意思VR 唉..可怜一下自己,这个问题百度google也不知道怎么搜,没搜出来,在群里问出来的. 当时感觉自己Unity有问题..(就是因为自己啥也不会看不懂) 按右键.或者WASD视角都 ...
- 扩展Unity的方法
写更少代码的需求 当我们重复写一些繁杂的代码,或C#的一些方法,我们就想能不能有更便捷的方法呢?当然在unity中,我们对它进行扩展. 对unity的类或C#的类进行扩展有以下两点要注意: 1.这个类 ...
- Unity手游之路<一>C#版本Protobuf
http://blog.csdn.net/janeky/article/details/17104877 个游戏包含了各种数据,包括本地数据和与服务端通信的数据.今天我们来谈谈如何存储数据,以及客户端 ...
- Unity WebSocket(仅适用于WebGL平台)
!!!转载注明:http://www.cnblogs.com/yinlong1991/p/unity_ylwebsocket.html Unity WebSocket 使用 1.下载 YLWebSoc ...
- 支付宝Unity
原地址:http://blog.csdn.net/sgnyyy/article/details/20444627 说明:支付宝Android的SDK接入只有一个接口,付费. 1. Android代码的 ...
- Unity 3(二):Unity在AOP方面的应用
本文关注以下方面(环境为VS2012..Net Framework 4.5以及Unity 3): AOP简介: Interception using Unity示例 配置文件示例 一.AOP简介 AO ...
- Unity Shader学习笔记 - 用UV动画实现沙滩上的泡沫
这个泡沫效果来自远古时代的Unity官方海岛Demo, 原效果直接复制3个材质球在js脚本中做UV动画偏移,这里尝试在shader中做动画并且一个pass中完成: // Upgrade NOTE: r ...
随机推荐
- windows环境下面批量移动文件到指定文件夹里面
move D:批量新建文件夹\upload\20171225173033565_2052.jpg D:批量新建文件夹\1623 move D:批量新建文件夹\upload\20171225174344 ...
- js-变量定义关键字const,var,let
1.var定义的变量可以修改,如果不初始化会输出undefined,不会报错. js中最常用的关键字:基本大多数据学js时都只看到使用过var.从没相关还有其他定义 var a = 1; // var ...
- Neo4j(一)
01-windows下载与安装neo4j https://blog.csdn.net/qq_21383435/article/details/78807024 neo4j的配置文件(图文详解) htt ...
- Java 对字符串数据进行MD5/SHA1哈希散列运算
Java对字符串数据进行MD5/SHA1哈希散列运算 [java] view plain copy package cn.aibo.test; import java.security.Message ...
- C# Callback思维
方式一.用委托作为形参,把结果传回实参方式二.通过接口实现方式三.通过事件关联,适用桌面应用程序方式四.子窗体调用父窗体的函数(委托) 方式一.用委托作为形参,把结果传回实参 public parti ...
- 基于Centos体验自然语言处理 by PHP SDK
系统要求:CentOS 7.2 64 位操作系统 准备工作 获取 SecretId 和 SecretKey1 前往 密钥管理 页面获取你的 SecretId 和 SecretKey 信息,这些信息将会 ...
- 浅析Sql Server参数化查询
说来惭愧,工作差不多4年了,直到前些日子被DBA找上门让我优化一个CPU占用很高的复杂SQL语句时,我才突然意识到了参数化查询的重要性. 相信有很多开发者和我一样对于参数化查询认识比较模糊,没有引起足 ...
- 安装SQL Server For Linux(Install SQL Server)
SQL Server on Ubuntu——Ubuntu上的SQL Server(全截图) 1. 安装SQL Server 官网安装指南:https://docs.microsoft.com ...
- C#中DataTable删除多条数据
//一般情况下我们会这么删除 DataTable dt = new DataTable(); for (int i = 0; i < dt.Rows.Count; i++) { if (99 % ...
- 浅谈MySQL备份字符集的问题
1 引子 MySQL备份时选择字符集是一个难题,特别是字符集不定的业务.mysqldump默认使用utf8,而官方也推荐使用utf8.但实际上,对于中文,部分相当一部分gbk编码字符没有对应的unic ...