NGUI的数据绑定

时间:2023-03-09 22:30:29
NGUI的数据绑定

ngui 的binding搜索结果 少之甚少 。即便去作者的youtube也收获不大 。

开发工作者更关心的是 数据的绑定,而不是一个显示控件简单属性的绑定。

说白了就是告诉用户 怎么绑定model吧 。

废话少说

原理依旧是使用组件 propertyBinding

1 addComponent-propertyBinding

2 在propertyBinding 面板设置

  source:2-1源对象

              2-2设置源对象的哪个属性

  target:2-3需要根据源来更新的对象

      2-4 绑定的属性

  direction:设置绑定方向

3 制作源对象 需要挂一个Model.cs类

using UnityEngine;
using System.Collections;
/// <summary>
/// 绑定测试
/// Naiking Q258529531
/// </summary>
public class UserModel : MonoBehaviour { // Use this for initialization
void Start ()
{
vo.hp = ;
vo.name = "naiking";
} public string name
{
get { return vo.name; }
set { vo.name = value; }
}
public int hp
{
get { return vo.hp; }
set { vo.hp = value; }
}
private VO vo=new VO();
// Update is called once per frame
void Update () { }
}

4 把model类 挂到一个空的gameObject上

5 新建一个Label 然后添加propertybinding

源:gameobject

UserModel.hp

target:把lable拖进来

text

然后弄2个按钮 一个是改变model的hp 一个是改变model的name

using System.Security.Permissions;
using UnityEngine;
using System.Collections; public class sTet : MonoBehaviour { // Use this for initialization
public UIButton btn;
public UserModel model;
void Start ()
{
btn.GetComponent<UIEventListener>().onClick = changM;
} private void changM(GameObject go)
{
model.hp += ;
Debug.Log(model.hp.ToString());
}
// Update is called once per frame
void Update () { }
}