最在在工作过程经常会使用到Grid Layout Group 组件,在使用的过程中发现对Grid Layout Group应用都是差不多的,主要的区别是显示的Item内容不同。所以就想把Item抽离成BaseItem,然后在用MyGrid继承Grid Layout Group。代码如下:
BaseItem
using UnityEngine;
using System.Collections;
public abstract class BaseItem : MonoBehaviour {
/// <summary>
/// 给Item传递数据
/// </summary>
/// <param name="date"></param>
public abstract void setDate(object date);
}
MyGrid
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MyGrid : GridLayoutGroup {
/// <summary>
/// 保存你要显示的item的类名
/// </summary>
private string itemName;
public void Init(string name)
{
itemName = name;
}
/// <summary>
/// 把数据传递给item
/// </summary>
/// <typeparam name="T">操作item的业务类</typeparam>
/// <param name="value">item的业务数据</param>
public void SetValue<T>(object[] value)
{
//隐藏grid下的item
HideChild(this.gameObject);
//如果grid下的item数量小于数据要显示的item,则生成item
if (gameObject.transform.childCount < value.Length)
{
for (int i = gameObject.transform.childCount; i < value.Length; i++)
{
GameObject item = CreateChild(i);
item.AddComponent(typeof(T));
}
}
//把数据赋值给每个item
for (int i = 0; i < value.Length; i++)
{
GameObject go = transform.FindChild((i + 1).ToString()).gameObject;
BaseItem item = go.GetComponent(typeof(T)) as BaseItem;
go.SetActive(true);
item.setDate(value[i]);
Debug.Log(item.name);
}
}
/// <summary>
/// 生成子对象
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
private GameObject CreateChild(int index)
{
//这里为里方便,我直接从Resources加载
GameObject go = Resources.Load(itemName) as GameObject;
GameObject item = Instantiate(go);
item.name = (index + 1).ToString();
item.transform.parent = this.gameObject.transform;
item.SetActive(false);
return item;
}
/// <summary>
/// 隐藏所有子对象
/// </summary>
/// <param name="go"></param>
private void HideChild(GameObject go)
{
for (int i = 0; i < go.transform.childCount; i++)
{
GameObject obj = go.transform.FindChild((i + 1).ToString()).gameObject;
obj.SetActive(false);
}
}
}
下面是具体的测试内容
Item1:item的业务类
using UnityEngine;
using System.Collections;
public class Item1 {
public string name;
public string age;
public string gender;
}
BtnItem:继承BaseItem
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
public class BtnItem : BaseItem
{
private Text NameText;
private Text AgeText;
private Text GenderText;
private Button btn;
void Awake()
{
NameText = this.gameObject.transform.FindChild("NameText").gameObject.GetComponent<Text>();
AgeText = this.gameObject.transform.FindChild("AgeText").gameObject.GetComponent<Text>();
GenderText = this.transform.FindChild("GenderText").gameObject.GetComponent<Text>();
btn = this.transform.FindChild("btn").gameObject.GetComponent<Button>();
}
void Start()
{
}
public override void setDate(object date)
{
Item1 item = (Item1)date;
//Debug.Log (item.name);
//Debug.Log (item.age);
//Debug.Log (item.gender);
NameText.text = item.name;
AgeText.text = item.age + "";
GenderText.text = item.gender;
btn.onClick.AddListener(OnClick);
}
private void OnClick()
{
Debug.Log("我是item:" + this.name);
}
}
Test:测试类
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class Test : MonoBehaviour {
List<Item1> list = new List<Item1>();
// Use this for initialization
void Start () {
GetValue();
//对Grid进行初始化
gameObject.GetComponent<MyGrid>().Init("Item1");
//对grid进行赋值
gameObject.GetComponent<MyGrid>().SetValue<BtnItem>(list.ToArray());
}
// Update is called once per frame
void Update () {
}
/// <summary>
/// 获取测试数据
/// </summary>
private void GetValue()
{
string str = Resources.Load("T1").ToString();
string[] strs = str.Split('\n');
for (int i = 0; i < strs.Length; i++)
{
string[] data = strs[i].Split('|');
Item1 item = new Item1();
item.name = data[0];
item.age = data[1];
item.gender = data[2];
list.Add(item);
}
}
}