private void LoadInPool(object data)
    {
        //Debug.LogWarning("LoadInPool:"+uri);
        ResourcesPool.LoadPoolData poolData = data as ResourcesPool.LoadPoolData;
        if (poolData is ResourcesPool.ErrorData)
        {
            ResourcesPool.ErrorData errorData = poolData as ResourcesPool.ErrorData;
            errorData.errorCount++;
            //if (errorData.errorCount > errorData.MaxErrorCount)
            //{
            progress   = 1;
            this.state = SimpleLoadedState.Failed;
            this.dispatchEvent(new LoadEvent(LoadEvent.Complete, this));
            return;
            //}
        }
        switch (poolData.type)
        {
        case ResourcesPool.PoolDataType.Json:
            loadedData = poolData.resouce;
            break;

        case ResourcesPool.PoolDataType.texture2D:
            loadedData = poolData.resouce;
            break;

        case ResourcesPool.PoolDataType.Prefab:
            ResourcesPool.PrefabData prefabData = poolData as ResourcesPool.PrefabData;
            if (canceled == false)
            {
                if (juseEndReturn == false)
                {
                    loadedData = prefabData.GetNew();
                }
            }
            break;

        case ResourcesPool.PoolDataType.Byte:
        default:
            loadedData = null;
            Debug.LogWarning("没有此类型的池!");
            break;
        }

        EndOnly();
    }
예제 #2
0
    private IEnumerator load(object[] arg1)
    {
        //加载成功 但是加载到的数据不对
        bool   loadedIsWrong = false;
        string path          = url;

        path = path.Replace("midea-products.oss-cn-shanghai.aliyuncs.com/", "pms.3dshome.net/");
        Debug.LogWarning("OutterLoad:" + path);
        www = new WWW(path);

        if (checkProgress)
        {
            MyTickManager.Add(Progress);
        }
        yield return(www);

        //yield return new WaitForSeconds(2);//模拟慢网速
        if (checkProgress)
        {
            MyTickManager.Remove(Progress);
        }
        //if (www == null)
        //{
        //    state = SimpleLoadedState.Failed;
        //    loadedData = null;
        //    string message = string.Format("加载文件失败:{0}", url);
        //    Debug.Log(message);
        //}
        //else {
        if (string.IsNullOrEmpty(www.error))
        {
            state = SimpleLoadedState.Success;
            switch (type)
            {
            case SimpleLoadDataType.prefabAssetBundle:

                //string realName = System.IO.Path.GetFileNameWithoutExtension(url);
                AssetBundle bundle = www.assetBundle;
                if (bundle != null)
                {
                    UnityEngine.Object[] objs = bundle.LoadAllAssets();
                    for (int i = 0; i < objs.Length; i++)
                    {
                        if (objs[i].GetType() == typeof(GameObject))
                        {
                            if (objs[i].name == modelName)
                            {
                                GameObject data = objs[i] as GameObject;
                                data.name = uri;
                                if (OnLoadprefabBeforClone != null)
                                {
                                    OnLoadprefabBeforClone(data, bringData);
                                }
                                if (justLoad == false)
                                {
                                    ResourcesPool.PrefabData prefab = resourcePool.addPrefab(httpUrl, data);
                                    if (canceled == false && justEndReturn == false)
                                    {
                                        loadedData = prefab.GetNew();
                                    }
                                }
                                //loadedData = objs[i];
                                //resourcePool.addPrefab(url, loadedData);
                            }
                        }
                    }
                    if (localHas == false && string.IsNullOrEmpty(uri) == false)
                    {
                        if (cacheManager != null)
                        {
                            cacheManager.AddCache(httpUrl, www.bytes);
                        }
                    }
                    if (justLoad == false)
                    {
                        bundle.Unload(false);
                    }
                    else
                    {
                        bundle.Unload(true);
                    }
                }
                else
                {
                    loadedIsWrong = true;
                    Debug.LogError("加载到的资源不是AssetBundle!path = " + www.url);
                }
                break;

            case SimpleLoadDataType.texture2D:
                if (localHas == false && string.IsNullOrEmpty(uri) == false)
                {
                    if (cacheManager != null)
                    {
                        cacheManager.AddCache(httpUrl, www.bytes);
                    }
                }
                if (www.assetBundle != null)
                {
                    if (justLoad == false)
                    {
                        loadedData = www.assetBundle.mainAsset as Texture2D;;
                        resourcePool.addTexture(httpUrl, loadedData);
                        www.assetBundle.Unload(false);
                    }
                    else
                    {
                        www.assetBundle.Unload(true);
                    }
                }
                else
                {
                    if (justLoad == false)
                    {
                        loadedData = www.texture;
                        resourcePool.addTexture(httpUrl, loadedData);
                    }
                }
                break;

            case SimpleLoadDataType.Json:
                string json  = System.Text.Encoding.UTF8.GetString(www.bytes);
                int    index = json.IndexOf("{");
                if (index == -1)
                {
                    index = json.IndexOf("[");
                }
                if (index > 0)
                {
                    json = json.Substring(index);
                }
                //同一个Json可能会变化 不记录上次的 因为有可能是过时的
                //if(localHas == false && string.IsNullOrEmpty(uri) == false) SaveToLocal(www.bytes);
                if (justLoad == false)
                {
                    loadedData = json;
                }
                break;

            case SimpleLoadDataType.Byte:
            default:
                loadedData = null;
                string message = string.Format("加载文件类型:{0} 失败:", type);
                Debug.Log(message);
                break;
            }
        }
        else
        {
            state      = SimpleLoadedState.Failed;
            loadedData = null;
            string message = string.Format("加载文件失败:{0} Error:{1}", www.url, www.error);
            Debug.LogWarning(message);
        }
        www.Dispose();
        //Debug.LogWarning("OnLoaded");
        if (loadedIsWrong == false)//没加载到资源 或者 加载到且正确
        {
            OnLoaded();
        }
        else
        {
            //加载资源且错误 等于加载失败
            this.state = SimpleLoadedState.Failed;
            LoadNext();
            EndOnly();
            resourcePool.LoadErrorData(this);
        }
    }