102 lines
3.2 KiB
C#
102 lines
3.2 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
/// <summary>
|
||
/// 针对UIWidget UIGeometry 数据buffer缓存
|
||
/// </summary>
|
||
public static class CachedGeometries
|
||
{
|
||
#region//私有变量
|
||
//UV
|
||
private static Dictionary<int, BetterList<BetterList<Vector2>>> cachedListsOfVector2List = new Dictionary<int, BetterList<BetterList<Vector2>>>();
|
||
//顶点
|
||
private static Dictionary<int, BetterList<BetterList<Vector3>>> cachedListsOfVector3List = new Dictionary<int, BetterList<BetterList<Vector3>>>();
|
||
//颜色
|
||
private static Dictionary<int, BetterList<BetterList<Color32>>> cachedListsOfColorList = new Dictionary<int, BetterList<BetterList<Color32>>>();
|
||
//总缓存大小
|
||
private static int allCacheSize = 0;
|
||
//未使用的缓存大小
|
||
private static int unUseCacheSize = 0;
|
||
#endregion
|
||
|
||
#region//公有函数
|
||
public static void Free(BetterList<Vector3> verts)
|
||
{
|
||
Free(cachedListsOfVector3List, verts, 12);
|
||
}
|
||
public static void Free(BetterList<Vector2> verts)
|
||
{
|
||
Free(cachedListsOfVector2List, verts, 8);
|
||
}
|
||
public static void Free(BetterList<Color32> cols)
|
||
{
|
||
Free(cachedListsOfColorList, cols, 16);
|
||
}
|
||
public static void Get(int count, ref BetterList<Vector3> verts)
|
||
{
|
||
Get(count, cachedListsOfVector3List, ref verts, 12);
|
||
}
|
||
public static void Get(int count, ref BetterList<Vector2> verts)
|
||
{
|
||
Get(count, cachedListsOfVector2List, ref verts, 8);
|
||
}
|
||
public static void Get(int count, ref BetterList<Color32> 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<T>(Dictionary<int, BetterList<BetterList<T>>> cache, BetterList<T> source, int size)
|
||
{
|
||
if (source == null || source.buffer == null)
|
||
return;
|
||
var count = FormatCount(source.buffer.Length);
|
||
BetterList<BetterList<T>> list = null;
|
||
if (!cache.TryGetValue(count, out list))
|
||
{
|
||
list = new BetterList<BetterList<T>>();
|
||
cache.Add(count, list);
|
||
}
|
||
unUseCacheSize += count * size;
|
||
list.Add(source);
|
||
}
|
||
|
||
private static void Get<T>(int vertexCount, Dictionary<int, BetterList<BetterList<T>>> cache, ref BetterList<T> source, int size)
|
||
{
|
||
var count = FormatCount(vertexCount);
|
||
BetterList<BetterList<T>> 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<T>(count);
|
||
}
|
||
}
|
||
#endregion
|
||
}
|