Пример #1
0
 // 加载场景
 public ResourceAsyncOperation LoadLevel(string fileName, LoadLevelFinishEventHandle handle, string archiveName = "Level")
 {
     if (UsedAssetBundle)
     {
         string fullName = ArchiveManager.Instance.GetPath(archiveName, fileName);
         if (mLoadedResourceUnit.ContainsKey(fullName))
         {
             DebugEx.LogError("load same level twice : " + fullName);
             return(null);
         }
         else
         {
             ResourceAsyncOperation operation = new ResourceAsyncOperation(ERequestType.LOAD_LEVEL);
             mAllRequests.Enqueue(new Request(fullName, EResourceType.LEVEL, handle, ERequestType.LOAD_LEVEL, operation));
             return(operation);
         }
     }
     else
     {
         ResourceAsyncOperation operation = new ResourceAsyncOperation(ERequestType.LOAD_LEVEL);
         mAllRequests.Enqueue(new Request(fileName, EResourceType.LEVEL, handle, ERequestType.LOAD_LEVEL, operation));
         return(operation);
     }
 }
Пример #2
0
        private IEnumerator _LoadLevel(string path, LoadLevelFinishEventHandle handle, EResourceType resourceType, ResourceAsyncOperation operation)
        {
            if (UsedAssetBundle)
            {
                AssetInfo sceneAssetInfo = mAssetInfoManager.GetAssetInfo(path);
                // 获取该资源总大小(包括依赖资源)
                operation.mAllDependenciesAssetSize = mAssetInfoManager.GetAllAssetSize(sceneAssetInfo);

                // 加载依赖资源
                foreach (int index in sceneAssetInfo.mDependencies)
                {
                    AssetInfo dependencyAsset     = mAssetInfoManager.GetAssetInfo(index);
                    string    dependencyAssetName = dependencyAsset.Name;

                    ResourceUnit unit = _LoadImmediate(dependencyAssetName, EResourceType.LEVEL);
                    operation.mLoadedDependenciesAssetSize += unit.AssetBundleSize;
                }

                // 加载场景的AssetBundle
                int         sceneAssetBundleSize = 0;
                byte[]      binary      = FileUtil.GetAssetBundleFileBytes(path, ref sceneAssetBundleSize);
                AssetBundle assetBundle = AssetBundle.LoadFromMemory(binary);
                if (!assetBundle)
                {
                    DebugEx.LogError("create scene assetbundle " + path + " failed !!!");
                }

                operation.mLoadedDependenciesAssetSize += sceneAssetBundleSize;

                AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(FileUtil.GetFileName(path, false));
                operation.asyncOperation = asyncOperation;
                yield return(asyncOperation);

                HandleResponse();

                operation.asyncOperation = null;
                operation.mComplete      = true;
                operation.mResource      = null;
                if (handle != null)
                {
                    handle();
                }
            }
            else
            {
                ResourceUnit   level          = new ResourceUnit(null, 0, null, path, resourceType);
                string         sceneName      = FileUtil.GetFileName(path, true);
                AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName);
                operation.asyncOperation = asyncOperation;
                yield return(asyncOperation);

                HandleResponse();

                operation.asyncOperation = null;
                operation.mComplete      = true;
                if (handle != null)
                {
                    handle();
                }
            }
        }