Creating own ListView

PS:TaskView 的Image去掉,背景才会变透明
Let's create own ListView, for this we need to write three classes:

  • item class to keep data
  • component class to display item, derived from ListViewItem
  • ListView class derived from ListViewCustom
 
For example it will be Task View, which will be display tasks, each task have name and progress. Download TaskView package.
 
Item class will have two fields: Name and Progress. Mark class as [System.Serializable] to edit Items in Editor.
namespace UIWidgetsSamples.Tasks {
[System.Serializable]
public class Task {
public string Name; public int Progress;
}
}
 
Component class should display this item, so it will be using Text to display name, Progressbar to display progress and implement IViewData<Task> to set it values.
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;
}
}
}
 
And combine those classes to ListView.
using UIWidgets;
namespace UIWidgetsSamples.Tasks {
public class TaskView : ListViewCustom<TaskComponent,Task> {
}
}
 
Now it's time to GameObject for ListView.
Here is step by step guide, but there is a faster way using ListViewIcons as source GameObject and redone it to you needs.
Create empty GameObject, rename it to TaskView and set size 400x300.
Add empty GameObject to TaskView, rename it to ScrollRect and set anchors to stretch and ampty space at the right side for scrollbar.
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.
Add Scrollbar to ScrollRect, set direction Bottom to Top, anchors to stretch right and change size and position parametres.
Add empty GameObject to ScrollRect, rename it to Viewport, set anchors to stretch, add Image and Mask components, disable Mask Show Mask Graphic.
This restricts ListView items to the shape of the Viewport.
Mask can be replaced with RectMask2D.
Add empty GameObject to Viewport, rename it to List, set anchors to stretch, Pivot.Y = 1, add EasyLayout and Content Size Fitter components.
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.
Add Image GameObject to List, rename it to DefaultItem, set anchors to top stretch, set height, set empty space from left and right.
Add Text GameObject to DefaultItem, rename it to Name, set anchors to stretch, set right = 200, this will be space for Progressbar.
Set Text vertical alignment to middle.
Add Progressbar GameObject to DefaultItem, set anchors to middle right, position it to right side.
Now we can add Task Component to DefaultItem and attach Name and Progressbar gameobjects to component fields.
Attach Scrollbar GameObject to ScrollRect Vertical Scrollbar.
Finally we add Task View component to TaskView GameObject.
Attach ScrollRect GameObject to ScrollRect field.
Attach List GameObject to Container field.
Attach DefaultItem GameObject to DefaultItem field.
Let's add some items to Task View.
And run scene.
Items displayed with correcty, but does not have any visual feedback to interactions. Let's fix it.
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
Change DefaultItem Image color to transparent.
Change DefaultItem Text color to 255, 215, 115, 255.
Now run scene again and check interactions.
Background color changes on selection and mouse over.
Now add Text 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;
}
//==========================
}

 
Now text also change color with interactions.
TaskView is completed, but we can add some features.
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;
}
}
}
 
Add new item.
Run scene, and check if items sorted by name.
If we change Task.Progress value, it will not be displayed, but we can implement it.
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;
}
}
 
Now we need to add event support in Task Component.
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;
}
}
}
 
And we need test script to check how it works.
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);
}
}
}
}
 
Create button and attach test script to it.
Add AddTask() to OnClick event.
And check how it's works.
原文出处:https://ilih.ru/unity-assets/UIWidgets/docs/Manual.Creating_own_ListView/  但是和原文有写区别,可能是包用的不一样导致的
源码地址:http://code.taobao.org/svn/UIWigetsTestDemo/trunk

Unity WidgetsUI CreateTaskView Demo的更多相关文章

  1. Unity ---WidgetsUI CreateTileView Demo

    以下教程基于:WidgetsUI 第三方扩展包 WidgtsUI 官网文档地址:https://ilih.ru/unity-assets/UIWidgets/docs/ 1.创建一个空GameObje ...

  2. Unity制作FPS Demo

    等到把这个Unity FPS Demo[僵尸杀手]完成后再详细补充一下,使用Unity制作FPS游戏的经历,今天做个标识.

  3. Unity VR-播放demo模型后无法移动视角

    资源源于:小意思VR 唉..可怜一下自己,这个问题百度google也不知道怎么搜,没搜出来,在群里问出来的. 当时感觉自己Unity有问题..(就是因为自己啥也不会看不懂) 按右键.或者WASD视角都 ...

  4. 扩展Unity的方法

    写更少代码的需求 当我们重复写一些繁杂的代码,或C#的一些方法,我们就想能不能有更便捷的方法呢?当然在unity中,我们对它进行扩展. 对unity的类或C#的类进行扩展有以下两点要注意: 1.这个类 ...

  5. Unity手游之路<一>C#版本Protobuf

    http://blog.csdn.net/janeky/article/details/17104877 个游戏包含了各种数据,包括本地数据和与服务端通信的数据.今天我们来谈谈如何存储数据,以及客户端 ...

  6. Unity WebSocket(仅适用于WebGL平台)

    !!!转载注明:http://www.cnblogs.com/yinlong1991/p/unity_ylwebsocket.html Unity WebSocket 使用 1.下载 YLWebSoc ...

  7. 支付宝Unity

    原地址:http://blog.csdn.net/sgnyyy/article/details/20444627 说明:支付宝Android的SDK接入只有一个接口,付费. 1. Android代码的 ...

  8. Unity 3(二):Unity在AOP方面的应用

    本文关注以下方面(环境为VS2012..Net Framework 4.5以及Unity 3): AOP简介: Interception using Unity示例 配置文件示例 一.AOP简介 AO ...

  9. Unity Shader学习笔记 - 用UV动画实现沙滩上的泡沫

    这个泡沫效果来自远古时代的Unity官方海岛Demo, 原效果直接复制3个材质球在js脚本中做UV动画偏移,这里尝试在shader中做动画并且一个pass中完成: // Upgrade NOTE: r ...

随机推荐

  1. Intellij IDEA 自动清除无效 import 和 清除无效 import 的快捷键 ctrl+alt+o

    快捷键 ctrl+alt+o 自动清除的配置方法 可以settings-general-auto import-java项,勾选optimize imports on the fly,在当前项目下会自 ...

  2. vcs+Makefile实现简单的testbench

    网络上找的文章,实现了一遍. 步骤如下: 1. 创建verilog代码, 包括8位加法器代码和testbench代码. adder8.v module adder8 ( input clk, inpu ...

  3. [Python设计模式] 第6章 衣服搭配系统——装饰模式

    github地址:https://github.com/cheesezh/python_design_patterns 题目 设计一个控制台程序,可以给人搭配嘻哈风格(T恤,垮裤,运动鞋)或白领风格( ...

  4. VTK拾取网格模型上的可见点

    消隐与Z-Buffer 使用缓冲器记录物体表面在屏幕上投影所覆盖范围内的全部像素的深度值,依次访问屏幕范围内物体表面所覆盖的每一像素,用深度小(深度用z值表示,z值小表示离视点近)的像素点颜色替代深度 ...

  5. SED单行脚本快速参考(Unix 流编辑器)(转)

    sed.sourceforge.net被封杀,特在此处贴上官方的sed 使用说明文档 SED单行脚本快速参考(Unix 流编辑器) 2005年12月29日 英文标题:USEFUL ONE-LINE S ...

  6. SNF快速开发平台3.0之BS页面展示和九大优点-部分页面显示效果-Asp.net+MVC4.0+WebAPI+EasyUI+Knockout

    一)经过多年的实践不断优化.精心维护.运行稳定.功能完善: 能经得起不同实施策略下客户的折腾,能满足各种情况下客户的复杂需求. 二)编码实现简单易懂.符合设计模式等理念: 上手快,见效快.方便维护,能 ...

  7. JPA+Hibernate 3.3 ——第一个JPA程序

    所需要的最小的jar包(注意:jar包所在路径不能含有空格或中文) hibernate3.jarhibernate-cglib-repack-2.1_3.jarslf4j-api-1.5.2.jarj ...

  8. 【Tomcat】Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析

    这个分为两个部分的系列文章研究了 Apache Tomcat 服务器的系统架构以及其运用的很多经典设计模式.第 1 部分 分析了 Tomcat 的工作原理,第 2 部分将分析 Tomcat 中运用的许 ...

  9. Android开发(十二)——头部、中部、底部布局

    参考: [1] http://www.thinksaas.cn/group/topic/82898/ [2] http://***/Article/12399 其实RadioGroup不好使,不能图片 ...

  10. 如何快速学习Scala

    大数据学习过程中,会学习非常多的技术,但SCALA无疑是必不可少,那我们在大数据技术的学习过程中,如何快速的认识scala,并且学习它,感谢科多大数据公司的余老师提供的详细素材,本人整理成章,希望对你 ...