using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 针对UIWidget UIGeometry 数据buffer缓存
///
public static class CachedGeometries
{
#region//私有变量
//UV
private static Dictionary>> cachedListsOfVector2List = new Dictionary>>();
//顶点
private static Dictionary>> cachedListsOfVector3List = new Dictionary>>();
//颜色
private static Dictionary>> cachedListsOfColorList = new Dictionary>>();
//总缓存大小
private static int allCacheSize = 0;
//未使用的缓存大小
private static int unUseCacheSize = 0;
#endregion
#region//公有函数
public static void Free(BetterList verts)
{
Free(cachedListsOfVector3List, verts, 12);
}
public static void Free(BetterList verts)
{
Free(cachedListsOfVector2List, verts, 8);
}
public static void Free(BetterList cols)
{
Free(cachedListsOfColorList, cols, 16);
}
public static void Get(int count, ref BetterList verts)
{
Get(count, cachedListsOfVector3List, ref verts, 12);
}
public static void Get(int count, ref BetterList verts)
{
Get(count, cachedListsOfVector2List, ref verts, 8);
}
public static void Get(int count, ref BetterList cols)
{
Get(count, cachedListsOfColorList, ref cols, 16);
}
public static void PrintInfo()
{
Debug.LogErrorFormat("NGUI Cacahe Info:Used {0}, Unused {1}, AllCache {2}", allCacheSize - unUseCacheSize, unUseCacheSize, allCacheSize);
}
#endregion
#region//私有函数
private static int FormatCount(int vecCount)
{
if (vecCount > 0 && vecCount % 32 == 0)
{
return vecCount;
}
else
{
return (vecCount / 32 + 1) * 32;
}
}
private static void Free(Dictionary>> cache, BetterList source, int size)
{
if (source == null || source.buffer == null)
return;
var count = FormatCount(source.buffer.Length);
BetterList> list = null;
if (!cache.TryGetValue(count, out list))
{
list = new BetterList>();
cache.Add(count, list);
}
unUseCacheSize += count * size;
list.Add(source);
}
private static void Get(int vertexCount, Dictionary>> cache, ref BetterList source, int size)
{
var count = FormatCount(vertexCount);
BetterList> list = null;
if (cache.TryGetValue(count, out list) && list.size > 0)
{
source = list[list.size - 1];
source.Clear();
list.RemoveAt(list.size - 1);
unUseCacheSize -= count * size;
}
else
{
allCacheSize += count * size;
source = new BetterList(count);
}
}
#endregion
}