布局组件也是常用的组件。首先介绍GList组件。
导出资源,
代码实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
/*
* Author:W
* List列表布局
*/
public class GListTest : MonoBehaviour {
private GComponent root;
private GList list;
private Dictionary<int, Item> itemDict = new Dictionary<int, Item>();
void Awake()
{
UIPackage.AddPackage("UI/Basics");
}
// Use this for initialization
void Start () {
root = this.GetComponent<UIPanel>().ui;
list = root.GetChild("n4").asList;
//初始化列表:数量为10
list.itemRenderer = RenderListItem;
list.numItems = 10;
//列表项点击事件注册
list.onClickItem.Add(OnClickItem);
}
/// <summary>
/// 列表项渲染
/// </summary>
/// <param name="index"></param>
/// <param name="obj"></param>
private void RenderListItem(int index, GObject obj)
{
Item item = null;
if (!itemDict.TryGetValue(index, out item))
{
item = new Item();
item.id = index;
itemDict.Add(index,item);
}
GButton bagItem = obj.asButton;
GTextField bagTitle = bagItem.GetChild("title").asTextField;
bagTitle.text = "道具" + item.id;
}
/// <summary>
/// 列表项点击事件处理
/// </summary>
/// <param name="context"></param>
private void OnClickItem(EventContext context)
{
Item curItem = null;
if (itemDict.TryGetValue(list.selectedIndex, out curItem))
{
Debug.Log("列表中,当前选中的项是=" + curItem.id);
}
}
// Update is called once per frame
void Update () {
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* Author:W
* List项的脚本
*/
public class Item {
public int id;
public void Init()
{
}
}
运行结果如下