IEnumerator Run() { _started = true; LoadItem item = null; while (true) { if (_items.Count > 0) { item = _items[0]; _items.RemoveAt(0); } else { break; } if (_pool.ContainsKey(item.url)) { //Debug.Log("hit " + item.url); NTexture texture = (NTexture)_pool[item.url]; texture.refCount++; if (item.onSuccess != null) { item.onSuccess(texture); } continue; } WWW www = new WWW(_basePath + item.url + ".ab"); yield return(www); if (string.IsNullOrEmpty(www.error)) { AssetBundle bundle = www.assetBundle; if (bundle == null) { Debug.LogWarning("Run Window->Build FairyGUI example Bundles first."); if (item.onFail != null) { item.onFail(www.error); } continue; } #if UNITY_5 NTexture texture = new NTexture(bundle.LoadAllAssets <Texture2D>()[0]); #else NTexture texture = new NTexture((Texture2D)bundle.mainAsset); #endif texture.refCount++; bundle.Unload(false); _pool[item.url] = texture; if (item.onSuccess != null) { item.onSuccess(texture); } } else { if (item.onFail != null) { item.onFail(www.error); } } } _started = false; }
public void OnUpdate() { int handled = 0; while (_items.Count > 0) { LoadItem item = _items[0]; _items.RemoveAt(0); NTexture ntex; if (_pool.ContainsKey(item.url)) { Log.Info("hit " + item.url); ntex = (NTexture)_pool[item.url]; ntex.refCount++; if (item.onSuccess != null) { item.onSuccess(ntex); } } else { try { Bitmap bm = new Bitmap(_basePath + item.url); Texture tex = new Texture(bm.Width, bm.Height, bm.GetPixels()); bm.Dispose(); ntex = new NTexture(tex); ntex.refCount++; if (item.onSuccess != null) { item.onSuccess(ntex); } } catch (Exception err) { //Log.Warning("load texture '" + item.url + "' failed."); ntex = NTexture.Empty; if (item.onFail != null) { item.onFail(err.Message); } } _pool[item.url] = ntex; } handled++; if (handled == WORKLOAD_PER_FRFAME) { break; } } float now = Engine.Timer.GetCurrTime(); if (now - _lastCheckPool > POOL_CHECK_TIME) { _lastCheckPool = now; int cnt = _pool.Count; if (cnt > MAX_POOL_SIZE) { ArrayList toRemove = null; foreach (DictionaryEntry de in _pool) { string key = (string)de.Key; NTexture texture = (NTexture)de.Value; if (texture.refCount == 0) { if (toRemove == null) { toRemove = new ArrayList(); } toRemove.Add(key); texture.Dispose(); //Log.Info("free icon " + de.Key); cnt--; if (cnt <= 8) { break; } } } if (toRemove != null) { foreach (string key in toRemove) { _pool.Remove(key); } } } } }
IEnumerator Run() { _started = true; LoadItem item = null; while (true) { if (_items.Count > 0) { item = _items[0]; _items.RemoveAt(0); } else { break; } if (_pool.ContainsKey(item.url)) { //Debug.Log("hit " + item.url); NTexture texture = (NTexture)_pool[item.url]; texture.refCount++; if (item.onSuccess != null) { item.onSuccess(texture); } continue; } string url = _basePath + item.url + ".ab"; #if UNITY_5_4_OR_NEWER #if UNITY_2018_1_OR_NEWER UnityWebRequest www = UnityWebRequestAssetBundle.GetAssetBundle(url); #else UnityWebRequest www = UnityWebRequest.GetAssetBundle(url); #endif #if UNITY_2017_2_OR_NEWER yield return(www.SendWebRequest()); #else yield return(www.Send()); #endif if (!www.isNetworkError && !www.isHttpError) { AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www); #else WWW www = new WWW(url); yield return(www); if (string.IsNullOrEmpty(www.error)) { AssetBundle bundle = www.assetBundle; #endif if (bundle == null) { Debug.LogWarning("Run Window->Build FairyGUI example Bundles first."); if (item.onFail != null) { item.onFail(www.error); } continue; } #if (UNITY_5 || UNITY_5_3_OR_NEWER) NTexture texture = new NTexture(bundle.LoadAllAssets <Texture2D>()[0]); #else NTexture texture = new NTexture((Texture2D)bundle.mainAsset); #endif texture.refCount++; bundle.Unload(false); _pool[item.url] = texture; if (item.onSuccess != null) { item.onSuccess(texture); } } else { if (item.onFail != null) { item.onFail(www.error); } } } _started = false; } IEnumerator FreeIdleIcons() { yield return(new WaitForSeconds(POOL_CHECK_TIME)); //check the pool every 30 seconds int cnt = _pool.Count; if (cnt > MAX_POOL_SIZE) { ArrayList toRemove = null; foreach (DictionaryEntry de in _pool) { string key = (string)de.Key; NTexture texture = (NTexture)de.Value; if (texture.refCount == 0) { if (toRemove == null) { toRemove = new ArrayList(); } toRemove.Add(key); texture.Dispose(); //Debug.Log("free icon " + de.Key); cnt--; if (cnt <= 8) { break; } } } if (toRemove != null) { foreach (string key in toRemove) { _pool.Remove(key); } } } } }
IEnumerator Run() { _started = true; LoadItem item = null; while (true) { if (_items.Count > 0) // 如果_items有值,取出来第一个,移除第一个 { item = _items[0]; _items.RemoveAt(0); } else // _items没值,直接退出循环,下边是新创建 { break; //退出循环 } //池里包含item.url,取出来图片,引用自增,如果有成功回调,调用一下成功的回调 if (_pool.ContainsKey(item.url)) { NTexture texture = (NTexture)_pool[item.url]; texture.refCount++; if (item.onSuccess != null) { item.onSuccess(texture); } continue; // 继续循环 } //在ab包里加载 WWW www = new WWW(_basePath + item.url + ".ab"); yield return(www); if (string.IsNullOrEmpty(www.error)) { AssetBundle bundle = www.assetBundle; if (bundle == null) { Debug.Log("bundle is null, you need Run Window -> Build FairyGUI example Bundles first."); if (item.onFail != null) { item.onFail(www.error); } continue; } #if UNITY_5 NTexture texture = new NTexture(bundle.LoadAllAssets <Texture2D>()[0]); #else NTexture texture = new NTexture((Texture2D)bundle.mainAsset); #endif texture.refCount++; bundle.Unload(false); _pool[item.url] = texture; if (item.onSuccess != null) { item.onSuccess(texture); } } else { if (item.onFail != null) { item.onFail(www.error); } } } _started = false; }