//单个资源加载
        ResourceItem _LoadImmediate(string fileName, ResourceType resourceType)
        {
            //没有该资源,加载
            if (!mLoadedResourceItem.ContainsKey(fileName))
            {
                //资源大小
                int         assetBundleSize = 0;
                byte[]      binary          = ResourceCommon.getAssetBundleFileBytes(fileName, ref assetBundleSize);
                AssetBundle assetBundle     = AssetBundle.LoadFromMemory(binary);
                if (!assetBundle)
                {
                    DebugEx.LogError("create assetbundle " + fileName + "in _LoadImmediate failed");
                }

                Object asset = assetBundle.LoadAsset(fileName);
                if (!asset)
                {
                    DebugEx.LogError("load assetbundle " + fileName + "in _LoadImmediate failed");
                }
                //调试用
                ResourceItem ru = new ResourceItem(assetBundle, assetBundleSize, asset, fileName, resourceType);
                //添加到资源中
                mLoadedResourceItem.Add(fileName, ru);
                return(ru);
            }
            else
            {
                return(mLoadedResourceItem[fileName]);
            }
        }
Пример #2
0
        public static IEnumerator _CreateFromMemory(string path, Handle_CreateFromMemory handle)
        {
            int size = 0;
            AssetBundleCreateRequest bundleRequest = AssetBundle.LoadFromMemoryAsync(ResourceCommon.getAssetBundleFileBytes(path, ref size));

            yield return(bundleRequest);

            handle(bundleRequest, size);
        }
        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();
                }
            }
        }