Пример #1
0
        private void handleRequest()
        {
            //使用assetbundle打包功能
            if (UsedAssetBundle)
            {
                mCurrentRequest = mAllRequests.Dequeue();

                //相对Asset的完整资源路径
                string fileName = mCurrentRequest.mFileName;

                switch (mCurrentRequest.mRequestType)
                {
                case ResourceRequestType.LOAD:
                {
                    switch (mCurrentRequest.mResourceType)
                    {
                    case ResourceType.ASSET:
                    case ResourceType.PREFAB:
                    {
                        if (mLoadedResourceItem.ContainsKey(fileName))
                        {
                            //(mLoadedResourceItem[fileName] as ResourceItem).addReferenceCount();

                            mCurrentRequest.mResourceAsyncOperation.mComplete = true;
                            mCurrentRequest.mResourceAsyncOperation.mResource = mLoadedResourceItem[fileName] as ResourceItem;

                            if (null != mCurrentRequest.mHandle)
                            {
                                mCurrentRequest.mHandle(mLoadedResourceItem[fileName] as ResourceItem);
                            }
                            handleResponse();
                        }
                        else
                        {
                            //传入相对路径名称
                            //StartCoroutine(_load(fileName, mCurrentRequest.mHandle, mCurrentRequest.mResourceType, mCurrentRequest.mResourceAsyncOperation));
                        }
                    }
                    break;

                    case ResourceType.LEVELASSET:
                    {
                        DebugEx.LogError("do you real need a single level asset??? this is have not decide!!!", ResourceCommon.DEBUGTYPENAME);
                    }
                    break;

                    case ResourceType.LEVEL:
                    {
                        DebugEx.LogError("this is impossible!!!", ResourceCommon.DEBUGTYPENAME);
                    }
                    break;
                    }
                }
                break;

                case ResourceRequestType.UNLOAD:
                {
                    if (!mLoadedResourceItem.ContainsKey(fileName))
                    {
                        DebugEx.LogError("can not find " + fileName, ResourceCommon.DEBUGTYPENAME);
                    }
                    else
                    {
                        //(mLoadedResourceItem[fileName] as ResourceItem).reduceReferenceCount();
                    }
                    handleResponse();
                }
                break;

                case ResourceRequestType.LOADLEVEL:
                {
                    StartCoroutine(_loadLevel(fileName, mCurrentRequest.mHandleLevel, ResourceType.LEVEL, mCurrentRequest.mResourceAsyncOperation));
                }
                break;

                case ResourceRequestType.UNLOADLEVEL:
                {
                    if (!mLoadedResourceItem.ContainsKey(fileName))
                    {
                        DebugEx.LogError("can not find level " + fileName, ResourceCommon.DEBUGTYPENAME);
                    }
                    else
                    {
                        //(mLoadedResourceItem[fileName] as ResourceItem).reduceReferenceCount();

                        if (null != mCurrentRequest.mHandleUnloadLevel)
                        {
                            mCurrentRequest.mHandleUnloadLevel();
                        }
                    }
                    handleResponse();
                }
                break;
                }
            }
            //不使用打包
            else
            {
                mCurrentRequest = mAllRequests.Dequeue();

                switch (mCurrentRequest.mRequestType)
                {
                case ResourceRequestType.LOAD:
                {
                    switch (mCurrentRequest.mResourceType)
                    {
                    case ResourceType.ASSET:
                    case ResourceType.PREFAB:
                    {
                        //暂时不处理,直接使用资源相对路径
                        //StartCoroutine(_load(mCurrentRequest.mFileName, mCurrentRequest.mHandle, mCurrentRequest.mResourceType, mCurrentRequest.mResourceAsyncOperation));
                    }
                    break;

                    case ResourceType.LEVELASSET:
                    {
                        DebugEx.LogError("do you real need a single level asset??? this is have not decide!!!", ResourceCommon.DEBUGTYPENAME);
                    }
                    break;

                    case ResourceType.LEVEL:
                    {
                        DebugEx.LogError("this is impossible!!!", ResourceCommon.DEBUGTYPENAME);
                    }
                    break;
                    }
                }
                break;

                case ResourceRequestType.UNLOAD:
                {
                    handleResponse();
                }
                break;

                case ResourceRequestType.LOADLEVEL:
                {
                    StartCoroutine(_loadLevel(mCurrentRequest.mFileName, mCurrentRequest.mHandleLevel, ResourceType.LEVEL, mCurrentRequest.mResourceAsyncOperation));
                }
                break;

                case ResourceRequestType.UNLOADLEVEL:
                {
                    if (null != mCurrentRequest.mHandleUnloadLevel)
                    {
                        mCurrentRequest.mHandleUnloadLevel();
                    }
                    handleResponse();
                }
                break;
                }
            }
        }
Пример #2
0
        private IEnumerator _loadLevel(string path, HandleFinishLoadLevel handle, ResourceType resourceType, ResourceAsyncOperation operation)
        {
            //使用assetbundle打包
            if (UsedAssetBundle)
            {
                //根据场景名称获取asset信息
                AssetInfo sceneAssetInfo = mAssetInfoManager.GetAssetInfo(path);
                //获取该包总大小
                operation.mAllDependencesAssetSize = mAssetInfoManager.GetAllAssetSize(sceneAssetInfo);

                //获取依赖的asset的索引
                foreach (int index in sceneAssetInfo.mDependencys)
                {
                    //根据索引获取依赖的Asset
                    AssetInfo depencyAsset     = mAssetInfoManager.GetAssetInfo(index);
                    string    depencyAssetName = depencyAsset.mName;

                    //加载场景依赖assetbundle
                    ResourceItem unit = _LoadImmediate(depencyAssetName, ResourceType.LEVEL);
                    operation.mLoadDependencesAssetSize += unit.AssetBundleSize;
                }

                //加载场景assetbundle
                int         scenAssetBundleSize = 0;
                byte[]      binary      = ResourceCommon.getAssetBundleFileBytes(path, ref scenAssetBundleSize);
                AssetBundle assetBundle = AssetBundle.LoadFromMemory(binary);
                if (!assetBundle)
                {
                    DebugEx.LogError("create scene assetbundle " + path + "in _LoadImmediate failed");
                }

                //添加场景大小
                operation.mLoadDependencesAssetSize += scenAssetBundleSize;

                AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(ResourceCommon.getFileName(path, false));
                operation.asyncOperation = asyncOperation;
                yield return(asyncOperation);

                handleResponse();

                operation.asyncOperation = null;
                operation.mComplete      = true;
                operation.mResource      = null;

                if (null != handle)
                {
                    handle();
                }
            }
            //不使用
            else
            {
                ResourceItem level = new ResourceItem(null, 0, null, path, resourceType);

                //获取加载场景名称
                string         sceneName      = ResourceCommon.getFileName(path, true);
                AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName);
                operation.asyncOperation = asyncOperation;
                yield return(asyncOperation);

                handleResponse();

                operation.asyncOperation = null;
                operation.mComplete      = true;

                if (null != handle)
                {
                    handle();
                }
            }
        }