Exemplo n.º 1
0
    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;
    }
Exemplo n.º 2
0
    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);
                }
            }
        }
    }
}
Exemplo n.º 3
0
    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)  // 如果_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;
    }
Exemplo n.º 5
0
    IEnumerator Run()
    {
        started = true;

        LoadItem tempItem = null;

        while (true)
        {
            if (items.Count > 0)
            {
                tempItem = items[0];
                items.RemoveAt(0);
            }
            else
            {
                break;
            }

            if (pool.ContainsKey(tempItem.url))
            {
                NTexture tempTexture = (NTexture)pool[tempItem.url];
                if (tempTexture == null)
                {
                    pool.Remove(tempItem.url);
                }
            }

            if (pool.ContainsKey(tempItem.url))
            {
                yield return(null);

                NTexture tempTexture = (NTexture)pool[tempItem.url];
                tempTexture.refCount++;

                if (tempItem.onSuccess != null)
                {
                    tempItem.onSuccess(tempTexture);
                }

                continue;
            }

            AssetBundle bundle = AssetBundle.LoadFromFile(Util.GetTargetAssetBundlesPath(tempItem.url));
            yield return(null);

            if (bundle != null)
            {
                NTexture texture = new NTexture(bundle.LoadAllAssets <Texture2D>()[0]);
                texture.refCount++;
                pool[tempItem.url] = texture;
                assetBundleList.Add(tempItem.url, bundle);

                try
                {
                    if (tempItem.onSuccess != null)
                    {
                        tempItem.onSuccess(texture);
                    }
                }
                catch
                {
                    started = false;
                }
            }
            else
            {
                Debug.LogError("<color=#FF0000>加载资源失败 -----> " + tempItem.url + "</color>");
            }
        }
        started = false;
    }
Exemplo n.º 6
0
    IEnumerator Run()
    {
        _started = true;
        LoadItem item = null;

        while (true)
        {
            if (_item.Count > 0)
            {
                item = _item[0];
                _item.RemoveAt(0);
            }
            else
            {
                break;
            }
            if (_pool.ContainsKey(item.url))
            {
                NTexture texture = _pool[item.url] as NTexture;
                texture.refCount++;
                if (item.onSuccess != null)
                {
                    item.onSuccess(texture);
                    continue;
                }
            }
            UnityWebRequest        unityWebRequest = new UnityWebRequest(item.url);
            DownloadHandlerTexture handlerTexture  = new DownloadHandlerTexture(true);
            unityWebRequest.downloadHandler = handlerTexture;
            yield return(unityWebRequest.SendWebRequest());

            if (string.IsNullOrEmpty(unityWebRequest.error))
            {
                Texture2D texture  = handlerTexture.texture;
                NTexture  nTexture = new NTexture(texture);
                _pool[item.url] = nTexture;
                item.onSuccess?.Invoke(nTexture);
            }
            else
            {
                item.onFail?.Invoke(unityWebRequest.error);
            }
            //WWW www = new WWW(item.url);
            //yield return www;
            //if (string.IsNullOrEmpty(www.error))
            //{
            //    Texture2D image = new Texture2D(4, 4, TextureFormat.ARGB32, false);
            //    www.LoadImageIntoTexture(image);
            //    NTexture texture = new NTexture(image);
            //    _pool[item.url] = texture;

            //    if (item.onSuccess != null)
            //        item.onSuccess(texture);
            //    //  Debug.Log(texture.width+"");
            //}
            //else
            //{
            //    if (item.onFail != null)
            //        item.onFail(www.error);
            //}
        }
        _started = false;
    }