117 lines
3.5 KiB
C#
117 lines
3.5 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Thousandto.Core.Base;
|
|
|
|
namespace Thousandto.Plugins.Common
|
|
{
|
|
public class AnyItemCache
|
|
{
|
|
// 已经使用列表
|
|
private List<GameObject> _use = new List<GameObject>();
|
|
// 废弃的列表
|
|
private List<GameObject> _discard = new List<GameObject>();
|
|
private int _size = 10; // 默认容量
|
|
private GameObject _Res = default(GameObject); // 资源对象
|
|
Transform _DelGrid = null;
|
|
|
|
// 条目模板对象, 废弃条目根节点, 初始/扩展个数
|
|
public AnyItemCache(GameObject res, Transform delGrid, int size = 10)
|
|
{
|
|
if (res != null && delGrid != null)
|
|
{
|
|
_size = size;
|
|
_Res = res;
|
|
_DelGrid = delGrid;
|
|
CreateItem();
|
|
}
|
|
}
|
|
|
|
// 创建条目对象
|
|
private void CreateItem()
|
|
{
|
|
GameObject go = null;
|
|
for (int i = 0; i < _size; ++i)
|
|
{
|
|
go = GameObject.Instantiate(_Res) as GameObject;
|
|
go.name = "0";
|
|
go.SetActive(false);
|
|
go.transform.parent = _DelGrid;
|
|
_discard.Add(go);
|
|
}
|
|
//Debug.LogWarning("创建缓冲区");
|
|
}
|
|
// 获得可用条目
|
|
public GameObject Get()
|
|
{
|
|
if (_discard.Count <= 0)
|
|
{
|
|
CreateItem(); // 再创建指定数量并添加到废弃列表
|
|
}
|
|
GameObject result = null;
|
|
result = _discard[0];
|
|
_use.Add(_discard[0]);
|
|
_discard.RemoveAt(0);
|
|
return result;
|
|
}
|
|
// 回收指定条目
|
|
public void Free(ulong id)
|
|
{
|
|
GameObject go = null;
|
|
for (int i = 0, len = _use.Count; i < len; ++i)
|
|
{
|
|
go = _use[i];
|
|
ulong _id = 0;
|
|
if (ulong.TryParse(go.name, out _id) == true && _id == id)
|
|
{
|
|
go.transform.parent = _DelGrid;
|
|
go.name = "0";
|
|
go.SetActive(false);
|
|
_discard.Add(go);
|
|
_use.RemoveAt(i);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
// 回收所有使用的条目
|
|
public void AllFree()
|
|
{
|
|
GameObject go = null;
|
|
for (int i = 0, len = _use.Count; i < len; ++i)
|
|
{
|
|
go = _use[i];
|
|
go.transform.parent = _DelGrid;
|
|
go.name = "0";
|
|
go.SetActive(false);
|
|
UIToggle ts = go.transform.GetComponent<UIToggle>();
|
|
if (ts != null)
|
|
{
|
|
ts.value = false;
|
|
}
|
|
UIButton btn = go.transform.GetComponent<UIButton>();
|
|
if (btn != null)
|
|
{
|
|
btn.onClick.Clear();
|
|
}
|
|
_discard.Add(go);
|
|
}
|
|
_use.Clear();
|
|
}
|
|
|
|
// 释放所有资源
|
|
public void Destroy()
|
|
{
|
|
//for (int i = 0, len = _use.Count; i < len; ++i)
|
|
//{
|
|
// GameObject.Destroy(_use[i]);
|
|
//}
|
|
//_use.Clear();
|
|
//for (int i = 0, len = _discard.Count; i < len; ++i)
|
|
//{
|
|
// GameObject.Destroy(_discard[i]);
|
|
//}
|
|
//_discard.Clear();
|
|
}
|
|
}
|
|
}
|