public static object GetCach(string key) { if (string.IsNullOrWhiteSpace(key)) { return(null); } try { rwLock.EnterReadLock(); KeyValuePair <string, CachItem> kv = CachPool.FirstOrDefault(c => c.Key == key); CachItem item = kv.Value; if (item == null) { return(null); } if (kv.Value.CachTime < DateTime.Now) { //CachPool.Remove(key); return(null); } return(kv.Value.CachObj); } finally { rwLock.ExitReadLock(); } }
static void recyle(CachItem item) { mCach.Remove(item.mKey); item.mKey = null; item.mRef = 0; item.mReside = ResideType.None; mPool.Add(item); }
static void traceUnload(CachItem rl) { if (rl.mKey != traceAsset) { return; } Debug.Log("unload reside=" + rl.mReside + " ref=" + rl.mRef); }
public static ResLoad get(string path, ResideType reside = ResideType.None, int v = 0) { #if CHECK_PATH checkPathCase(path); #endif CachItem it = CachItem.getFromCach(path, reside); it.mVer = v; return(new ResLoad(it)); }
internal static void clearByPath(string path) { CachItem item = null; if (mCach.TryGetValue(path, out item)) { item.release(true); } }
//---------------- ResLoad(CachItem ci) { mItem = ci; mUniqueID = mItem.mUniqueID; if (mItem.mReside == ResideType.Ref) { ++mItem.mRef; } }
public ResLoad(ResLoad rl) { mItem = rl.mItem; mUniqueID = mItem.mUniqueID; if (mItem.mReside == ResideType.Ref) { ++mItem.mRef; } }
public static ResLoad get(string path, string tag, int v = 0) { #if CHECK_PATH checkPathCase(path); #endif CachItem it = CachItem.getFromCach(path, ResideType.InGame); it.mVer = v; it.mTag = tag; return(new ResLoad(it)); }
internal static void addAssetRef(string key) { CachItem item = null; if (mCach.TryGetValue(key, out item)) { if (item.mReside != ResideType.Ref) { return; } ++item.mRef; } }
internal static void decAssetRef(string key) { CachItem item = null; if (mCach.TryGetValue(key, out item)) { if (item.mReside != ResideType.Ref) { return; } item.release(); } }
internal static List <string> getCachAssets() { List <string> ls = new List <string> (); IDictionaryEnumerator e = mCach.GetEnumerator(); while (e.MoveNext()) { CachItem it = e.Value as CachItem; ls.Add(it.mKey + ":" + it.mReside + ":" + it.mRef); } ls.Sort(delegate(string x, string y){ return(x.CompareTo(y)); }); return(ls); }
public void release() { if (mItem == null) { return; } if (mUniqueID != mItem.mUniqueID) { return; } mItem.release(); mItem = null; mParam1 = null; mParam2 = null; mParam3 = null; }
//-------------- #region cach管理 internal static CachItem getFromCach(string key, ResideType reside) { CachItem item = null; if (mCach.TryGetValue(key, out item)) { if (item.mReside != ResideType.Ref && item.mReside < reside) { item.mReside = reside; } item.mTime = Time.realtimeSinceStartup; } else { if (mPool.Count > 0) { item = mPool[0]; mPool.RemoveAt(0); } else { item = new CachItem(); } item.mKey = key; item.mReside = reside; item.mRef = 0; item.mVer = 0; item.mIsPrefab = false; item.mIsAB = false; item.mCallData = null; item.mTag = null; if (item.mReside != ResideType.None) { item.mTime = Time.realtimeSinceStartup; mCach [item.mKey] = item; } } item.mUniqueID = ++uniqueID; #if UNITY_EDITOR traceLoad(item); #endif return(item); }
internal static void clearByReside(ResideType riside) { ArrayList ls = new ArrayList(); IDictionaryEnumerator e = mCach.GetEnumerator(); while (e.MoveNext()) { CachItem it = e.Value as CachItem; if (it.mReside == riside) { ls.Add(it); } } for (int i = 0, max = ls.Count; i < max; ++i) { CachItem it = ls[i] as CachItem; it.release(true); } }
internal static void clearByTag(string tag) { ArrayList ls = new ArrayList(); IDictionaryEnumerator e = mCach.GetEnumerator(); while (e.MoveNext()) { CachItem it = e.Value as CachItem; if (tag == it.mTag) { ls.Add(it); } } for (int i = 0, max = ls.Count; i < max; ++i) { CachItem it = ls[i] as CachItem; it.release(true); } }
void loadDepends() { //图片应该都打成依赖包,这样就可以使用引用计数管理 mDepends = mManifest.GetDirectDependencies(mKey + ext); for (int i = 0, max = mDepends.Length; i < max; ++i) { mDepends[i] = mDepends[i].Remove(mDepends[i].Length - 5); CachItem ci = CachItem.getFromCach(mDepends[i], ResideType.Ref); ++ci.mRef; if (null == ci.assetBundle()) { Log.e("load ab failed path=" + mKey, Log.Tag.RES); } //有的assetbundle是全局预加载的使用的非ref模式,如shader if (ci.mReside != ResideType.Ref) { mDepends[i] = null; } } }
internal static void clearCach() { //清除cach,忽略所有引用,注意存在引用计数的资源 IDictionaryEnumerator e = mCach.GetEnumerator(); while (e.MoveNext()) { CachItem it = e.Value as CachItem; //避免ab被unload(true)导致当前显示的资源丢失 it.mReside = ResideType.None; it.innerRelease(); } mCach.Clear(); //解除引用计数关系 AssetRef[] ars = Resources.FindObjectsOfTypeAll <AssetRef> (); for (int i = 0, max = ars.Length; i < max; ++i) { if (ars[i].hasDepends()) { ars [i].setDepends(null); } } }
public static List <string> getCachAssets() { return(CachItem.getCachAssets()); }
public static void setTraceAsset(string path) { CachItem.setTraceAsset(path); }
public static void clearByTag(string tag) { CachItem.clearByTag(tag); }
public static void clearReside(ResideType riside) { CachItem.clearByReside(riside); }
public static void clearByPath(string path) { CachItem.clearByPath(path); }
internal static void addAssetRef(string path) { CachItem.addAssetRef(path); }
public static void clearCach() { CachItem.clearCach(); }
public static bool isInCach(string path) { return(CachItem.isInCach(path)); }
internal static void decAssetRef(string path) { CachItem.decAssetRef(path); }