257 lines
4.7 KiB
C#
257 lines
4.7 KiB
C#
// ***********************************************************************
|
|
// Assembly : Unity
|
|
// Author : Kimch
|
|
// Created :
|
|
//
|
|
// Last Modified By : Kimch
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "KUIList" company=""></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// KUIItem 对象管理(简单)
|
|
/// </summary>
|
|
public class KUIList : MonoBehaviour
|
|
{
|
|
#region Field
|
|
|
|
/// <summary>
|
|
/// 物品模版
|
|
/// </summary>
|
|
public GameObject itemTemplate;
|
|
/// <summary>
|
|
/// 物品列表
|
|
/// </summary>
|
|
private readonly List<GameObject> _itemObjects = new List<GameObject>();
|
|
|
|
#endregion
|
|
|
|
#region Property
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int count
|
|
{
|
|
get { return _itemObjects.Count; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Method
|
|
|
|
/// <summary>
|
|
/// 添加模板
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public T AddTemplate<T>(bool useTemplate = false) where T : KUIWidget
|
|
{
|
|
if (!itemTemplate && transform.childCount > 0)
|
|
{
|
|
itemTemplate = transform.GetChild(0).gameObject;
|
|
}
|
|
T result = default;
|
|
|
|
if (itemTemplate)
|
|
{
|
|
itemTemplate.SetActive(false);
|
|
result = itemTemplate.GetComponent<T>();
|
|
if (!result)
|
|
result = itemTemplate.AddComponent<T>();
|
|
}
|
|
|
|
if (useTemplate)
|
|
{
|
|
for (int i = 0; i < transform.childCount; i++)
|
|
{
|
|
var child = transform.GetChild(i);
|
|
if (child.GetComponent<T>())
|
|
{
|
|
child.gameObject.SetActive(false);
|
|
_itemObjects.Add(child.gameObject);
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public void PreCreate(int count)
|
|
{
|
|
if (itemTemplate)
|
|
{
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
var itemGO = Instantiate(itemTemplate, this.transform);
|
|
_itemObjects.Add(itemGO);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="active">默认活跃</param>
|
|
/// <returns></returns>
|
|
private KUIWidget AddObject(bool active = true)
|
|
{
|
|
if (itemTemplate)
|
|
{
|
|
var itemGO = Instantiate(itemTemplate, this.transform);
|
|
itemGO.SetActive(active);
|
|
_itemObjects.Add(itemGO);
|
|
itemGO.name = itemTemplate.name + "_" + _itemObjects.Count;
|
|
|
|
var item = itemGO.GetComponent<KUIWidget>();
|
|
if (item)
|
|
{
|
|
item.index = _itemObjects.Count - 1;
|
|
return item;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="amount"></param>
|
|
private void ActivateAvailableObjects(int amount)
|
|
{
|
|
if (_itemObjects.Count < amount)
|
|
{
|
|
amount = _itemObjects.Count;
|
|
}
|
|
|
|
for (int i = 0; i < amount; i++)
|
|
{
|
|
_itemObjects[i].SetActive(true);
|
|
}
|
|
|
|
for (int i = _itemObjects.Count - 1; i >= amount; i--)
|
|
{
|
|
_itemObjects[i].SetActive(false);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="amount"></param>
|
|
public void Clear(int amount = 0)
|
|
{
|
|
this.ActivateAvailableObjects((amount <= _itemObjects.Count) ? amount : _itemObjects.Count);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public KUIWidget GetItem()
|
|
{
|
|
var item = _itemObjects.Find(obj => !obj.activeSelf);
|
|
if (item)
|
|
{
|
|
item.SetActive(true);
|
|
return item.GetComponent<KUIWidget>();
|
|
}
|
|
else
|
|
{
|
|
return AddObject(true);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public T GetItem<T>() where T : KUIWidget
|
|
{
|
|
var item = _itemObjects.Find(obj => !obj.activeSelf);
|
|
if (item)
|
|
{
|
|
item.SetActive(true);
|
|
return item.GetComponent<T>();
|
|
}
|
|
else
|
|
{
|
|
return (T)AddObject(true);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="index"></param>
|
|
/// <returns></returns>
|
|
public KUIWidget GetItem(int index)
|
|
{
|
|
if (index < _itemObjects.Count)
|
|
{
|
|
var itemObject = _itemObjects[index];
|
|
return itemObject.GetComponent<KUIWidget>();
|
|
}
|
|
return this.AddObject(true);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="index"></param>
|
|
/// <param name="active"></param>
|
|
/// <returns></returns>
|
|
public KUIWidget GetItem(int index, bool active)
|
|
{
|
|
if (index < _itemObjects.Count)
|
|
{
|
|
var itemObject = _itemObjects[index];
|
|
itemObject.SetActive(active);
|
|
return itemObject.GetComponent<KUIWidget>();
|
|
}
|
|
return this.AddObject(active);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="amount"></param>
|
|
public void RefreshList(int amount)
|
|
{
|
|
ActivateAvailableObjects(amount);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Unity
|
|
|
|
private void Awake()
|
|
{
|
|
//if (!itemTemplate)
|
|
//{
|
|
// if (transform.childCount > 0)
|
|
// itemTemplate = transform.GetChild(0).gameObject;
|
|
//}
|
|
|
|
//if (itemTemplate)
|
|
//{
|
|
// itemTemplate.SetActive(false);
|
|
//}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Layout
|
|
|
|
private void InitLayout()
|
|
{
|
|
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|