u3d对象池
来源:互联网 发布:mac 怎么卸载jenkins 编辑:程序博客网 时间:2024/06/06 13:23
无聊,发一贴代码,对象池
using UnityEngine;using System.Collections;using System.Collections.Generic;/// <summary>/// Cache with LRU/// </summary>/// <typeparam name="T"></typeparam>public class MemoryCache<T> :IEnumerable{ protected class NodeInfo<T> { public string key; public T obj; } List<NodeInfo<T>> cacheQueue; int cacheSize;/// <summary> /// Cache with LRU/// </summary>/// <param name="size">cache size</param> public MemoryCache(int size){ cacheQueue = new List<NodeInfo<T>>(); cacheSize = size; } /// <summary> /// reset cache size /// </summary> /// <param name="size"></param> public void SetCacheSize(int size) { cacheSize = size; } /// <summary> /// try get obj from cache /// </summary> /// <param name="key"></param> /// <returns></returns> public T GetFromCache(string key) { var cacheObj = cacheQueue.Find(n => { return n.key == key; }); if (cacheObj == null) { return default(T); } else { cacheQueue.Remove(cacheObj); cacheQueue.Insert(0, cacheObj); return cacheObj.obj; } } /// <summary> /// add or update cache /// </summary> /// <param name="key"></param> /// <param name="obj"></param> public void Put(string key,T obj) { var cacheObj = cacheQueue.Find(n => { return n.key == key; }); if (cacheObj == null) { var newNode = new NodeInfo<T>(); newNode.key = key; newNode.obj = obj; cacheQueue.Insert(0, newNode); } else { cacheObj.obj = obj; cacheQueue.Remove(cacheObj); cacheQueue.Insert(0, cacheObj); } Clear(cacheSize); } /// <summary> /// remove node by key /// </summary> /// <param name="key"></param> public void Remove(string key) { var cacheNode = cacheQueue.Find(n => { return n.key == key; }); if (cacheNode!=null) { cacheQueue.Remove(cacheNode); } } /// <summary> /// loop remove item until less than hold num /// </summary> /// <param name="hold"></param> public void Clear(int hold) { if (hold<0) { hold = 0; } while (cacheQueue.Count > hold) { cacheQueue.RemoveAt(cacheQueue.Count - 1); } //Resources.UnloadUnusedAssets(); } public int GetCacheSize() { return cacheQueue.Count; } public void PrintAll() { for (int i = cacheQueue.Count-1; i >= 0; i--) { var node = cacheQueue[i]; Debug.Log(node.key+":"+node.obj); } } public IEnumerator GetEnumerator() { return new MCEnum(this.cacheQueue); } protected class MCEnum : IEnumerator { List<NodeInfo<T>> cacheQueue; public MCEnum(List<NodeInfo<T>> cacheQueue) { this.cacheQueue = cacheQueue; } int iter = -1; public object Current { get { if (iter < cacheQueue.Count && iter >= 0) { return cacheQueue[iter].obj; } return null; } } public bool MoveNext() { iter++; if (iter < cacheQueue.Count) { return true; } return false; } public void Reset() { iter = -1; } }}
0 0
- u3d对象池
- u3d的对象检测
- u3d
- u3d
- u3d
- u3d
- u3d
- u3d
- u3d
- u3d
- U3D中对象的自动销毁
- U3D 对象响应鼠标点击事件
- U3D,鼠标控制摄像头和游戏对象
- u3d 地形 U3d terrain
- U3D 相关
- U3D背包
- U3d学习
- U3D记事本
- 打印log,使用类名作为tag
- 支付宝即时到账接口
- MySQL索引的索引长度问题
- tomcat源码(三) Connector
- Linux下配置eclipse 开发c/c++环境
- u3d对象池
- springMVC -- 整合UEditor(富文本编辑器)
- Gulp returns error: EPERM: operation not permitted, chmod <directoryName>问题解决办法
- J2EE模式--拦截过滤器模式
- PHP读取csv文件的内容
- NSURLSession前台下载/后台下载/断点下载的代理执行路径(二)
- 'adb' 不是内部或外部命令,也不是可运行的程序或批处理文件
- Trafodion ETL工具odb进行数据load方法及性能
- Qt Charts - 让数据可视化更简单