创建一个App.cs,挂在GameObject上:
using System.Collections.Generic;
using UnityEngine;
namespace QFramework.TodoList
{
///
/// TodoApp:
/// 1. 完成、未完成
/// 2. 列表/待办事项
/// 3. 增加、删除、更改待办事项
///
/// 有一个列表,列表里多个 待办事项,待办事项能够完成未完成,内容
///
public class App : MonoBehaviour
{
public TodoList Model = new TodoList();
}
public class TodoList
{
public List mTodoItems;
}
public class TodoItem
{
public bool Completed;
public string Content;
}
}
(1)将UIRoot拖入场景中,Canvas Scaler下的分辨率设置为640x1136,然后在Design下创建一个Image,命名UITodoList,然后再创建一个Text,并为Text赋予组件UI Mark.cs;
(2)将UITodoList拖拽为预制体,鼠标右键预制体,先选择:@Res Kit - AssetBundle Mark,然后再选择@UI Kit - Creat UICode;
(3)接下来通过编辑器来把UITodoListComponent.cs这个脚本的命名空间改一下,然后点击Apply;(注意,这里操作只能改动UITodoListComponent.cs的命名空间,而UITodoList.cs的命名空间需要手动去改)
(4)运行后,UITodoList页面弹出。
(1)在App.cs中,代码修改如下:
public class App : MonoBehaviour
{
public TodoList Model = new TodoList();
void Start()
{
ResMgr.Init();//资源初始化,用于打包
UIMgr.SetResolution(640, 1136, 0);//设置分辨率
UIMgr.OpenPanel(new UITodoListData()
{
Model = Model
}) ;//打开面板
}
}
public class TodoList
{
public List mTodoItems=new List()
{
new TodoItem(){ Completed=false,Content="要结婚"},
new TodoItem(){ Completed=false,Content="要买房"},
new TodoItem(){ Completed=false,Content="要买车"}
};
}
public class TodoItem
{
public bool Completed;
public string Content;
}
(2)在UITodoList.cs中,代码修改如下:
using UnityEngine;
using UnityEngine.UI;
using System.Text;
namespace QFramework.TodoList
{
public class UITodoListData : UIPanelData
{
// TODO: Query Mgr's Data
public TodoList Model;
}
public partial class UITodoList : UIPanel
{
protected override void InitUI(IUIData uiData = null)
{
mData = uiData as UITodoListData ?? new UITodoListData();
//please add init code here
var contentText = new StringBuilder();
mData.Model.mTodoItems.ForEach(item =>
{
contentText.AppendLine(item.Content);
});
Text.text = contentText.ToString();
}
protected override void ProcessMsg (int eventId,QMsg msg)
{
throw new System.NotImplementedException ();
}
protected override void RegisterUIEvent()
{
}
protected override void OnShow()
{
base.OnShow();
}
protected override void OnHide()
{
base.OnHide();
}
protected override void OnClose()
{
base.OnClose();
}
void ShowLog(string content)
{
Debug.Log("[ UITodoList:]" + content);
}
}
}
(3)运行结果如下:
(1)首先对Hierarchy作如下布置:
(2)为Content、UITodoItem分别添加UIMark.cs,注意UITodoItem的UIMark.cs上,MarkType选择Element,CustomComponent填写自己:UITodoItem ,再隐藏一下UITodoItem,最后对UITodoList整体点击Apply即可;
(3)老规矩,鼠标右键预制体UITodoList,选择@UI Kit - Creat UICode,这时候会报一个错,修改代码UITodoList.cs如下:
protected override void InitUI(IUIData uiData = null)
{
mData = uiData as UITodoListData ?? new UITodoListData();
//please add init code here
var contentText = new StringBuilder();
mData.Model.mTodoItems.ForEach(item =>
{
UITodoItem.Instantiate()
.Parent(Content)
.LocalIdentity()
.Show();
});
}
(4)运行结果如下:
(1)编写脚本UITodoItem.cs如下:
namespace QFramework.TodoList
{
public partial class UITodoItem : UIElement
{
TodoItem mModel;
public void Init(TodoItem model)
{
mModel = model;
Content.text = model.Content;
}
private void Awake()
{
}
protected override void OnBeforeDestroy()
{
}
}
}
(2)UITodoList.cs修改如下:
protected override void InitUI(IUIData uiData = null)
{
mData = uiData as UITodoListData ?? new UITodoListData();
//please add init code here
var contentText = new StringBuilder();
mData.Model.mTodoItems.ForEach(item =>
{
UITodoItem.Instantiate()
.Parent(Content)
.LocalIdentity()
.ApplySelfTo((self)=>self.Init(item))
.Show();
});
}
(3)运行结果如下:
(4)对Completed进行标记,即添加UIMark.cs,然后Apply一下UITodoList,然后鼠标右键预制体UITodoList,选择@UI Kit - Creat UICode,这个时候脚本UITodoItemComponents.cs就自动修改了,如下所示:
(5)修改UITodoItem.cs如下:
public void Init(TodoItem model)
{
mModel = model;
Content.text = model.Content;
Completed.isOn = model.Completed;
}
(6)运行结果如下:(三个任务都已完成了)
(1)修改UITodoItem.cs如下:
namespace QFramework.TodoList
{
public partial class UITodoItem : UIElement
{
TodoItem mModel;
public void Init(TodoItem model)
{
mModel = model;
Content.text = model.Content;
Completed.isOn = model.Completed;
Completed.onValueChanged.AddListener(on =>
{
mModel.Completed = on;
SendEvent(UITodoListEvent.OnDataChanged);
});
}
private void Awake()
{
}
protected override void OnBeforeDestroy()
{
}
}
}
(2)修改UITodoList.cs如下:
using UnityEngine;
using System.Text;
using System.Linq;
namespace QFramework.TodoList
{
public class UITodoListData : UIPanelData
{
// TODO: Query Mgr's Data
public TodoList Model;
}
//UI事件类型
public enum UITodoListEvent
{
Start=QMgrID.UI,
OnDataChanged,
End
}
public partial class UITodoList : UIPanel
{
protected override void InitUI(IUIData uiData = null)
{
mData = uiData as UITodoListData ?? new UITodoListData();
//please add init code here
var contentText = new StringBuilder();
OnDataChanged();
RegisterEvent(UITodoListEvent.OnDataChanged);//注册事件
}
protected override void ProcessMsg (int eventId,QMsg msg)
{
if (eventId ==(int)UITodoListEvent.OnDataChanged)
{
OnDataChanged();
}
}
void OnDataChanged()
{
Content.DestroyAllChild();
mData.Model.mTodoItems.Where(item=>!item.Completed).ForEach(item =>
{
UITodoItem.Instantiate()
.Parent(Content)
.LocalIdentity()
.ApplySelfTo((self) => self.Init(item))
.Show();
});
}
protected override void RegisterUIEvent()
{
}
protected override void OnShow()
{
base.OnShow();
}
protected override void OnHide()
{
base.OnHide();
}
protected override void OnClose()
{
base.OnClose();
}
void ShowLog(string content)
{
Debug.Log("[ UITodoList:]" + content);
}
}
}
(3)运行结果显示为: