Example #1
0
        private IEnumerator LoadAssetBundleFromCache(IObserver <SeekableAssetBundle> observer, AssetInfo assetInfo)
        {
            var filePath        = GetFilePath(assetInfo);
            var assetBundleInfo = assetInfo.AssetBundle;
            var assetBundleName = assetBundleInfo.AssetBundleName;

            var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

            var cryptoStream = new SeekableCryptoStream(fileStream, cryptoKey);

            var bundleLoadRequest = AssetBundle.LoadFromStreamAsync(cryptoStream);

            while (!bundleLoadRequest.isDone)
            {
                yield return(null);
            }

            var assetBundle = bundleLoadRequest.assetBundle;

            // 読み込めなかった時はファイルを削除して次回読み込み時にダウンロードし直す.
            if (assetBundle == null)
            {
                UnloadAsset(assetBundleName);

                if (cryptoStream != null)
                {
                    cryptoStream.Dispose();
                }

                if (fileStream != null)
                {
                    fileStream.Dispose();
                }

                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }

                var builder = new StringBuilder();

                builder.Append("Failed to load AssetBundle!").AppendLine();
                builder.AppendLine();
                builder.AppendFormat("File : {0}", filePath).AppendLine();
                builder.AppendFormat("AssetBundleName : {0}", assetBundleName).AppendLine();
                builder.AppendFormat("CRC : {0}", assetBundleInfo.CRC).AppendLine();

                observer.OnError(new Exception(builder.ToString()));
            }
            else
            {
                var seekableAssetBundle = new SeekableAssetBundle(assetBundle, fileStream, cryptoStream);

                observer.OnNext(seekableAssetBundle);
            }

            observer.OnCompleted();
        }
Example #2
0
            public void Dispose()
            {
                if (assetBundle != null)
                {
                    assetBundle.Unload(false);
                    assetBundle = null;
                }

                if (fileStream != null)
                {
                    fileStream.Dispose();
                    fileStream = null;
                }

                if (cryptoStream != null)
                {
                    cryptoStream.Dispose();
                    cryptoStream = null;
                }

                GC.SuppressFinalize(this);
            }