对UGUI的GridLayoutGroup组件进行拓展

最在在工作过程经常会使用到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);
        }
    }
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值