/// <summary> /// Requests a remote endpoint for uploads from the PlaFab service. /// </summary> void GetContentUploadURL(AssetBundleHelperObject asset) { var request = new GetContentUploadUrlRequest(); if (asset.BundlePlatform == AssetBundleHelperObject.BundleTypes.Android) { request.Key = "Android/" + asset.ContentKey; // folder location & file name to use on the remote server } else if (asset.BundlePlatform == AssetBundleHelperObject.BundleTypes.iOS) { request.Key = "iOS/" + asset.ContentKey; } else // stand-alone { request.Key = asset.ContentKey; } request.ContentType = asset.MimeType; // mime type to match the file #if UNITY_WEBPLAYER //UnityEngine.Deubg.Log("Webplayer does not support uploading files."); #else PlayFabAdminAPI.GetContentUploadUrl(request, result => { asset.PutUrl = result.URL; byte[] fileContents = File.ReadAllBytes(asset.LocalPutPath); PutFile(asset, fileContents); }, OnPlayFabError); #endif }
/// <summary> /// Gets the reletive file path; works acrocss Unity build targets (Web, iOS, Android, PC, Mac) /// </summary> /// <returns> The assetPath where the file can be found (will varry depending on the platform) </returns> IEnumerator GetFilePath(AssetBundleHelperObject asset) { var platformPrefix = "/"; if (asset.BundlePlatform == AssetBundleHelperObject.BundleTypes.Android) { platformPrefix = "/Android/"; // folder location & file name to use on the remote server } else if (asset.BundlePlatform == AssetBundleHelperObject.BundleTypes.iOS) { platformPrefix = "/iOS/"; } var streamingAssetPath = Application.streamingAssetsPath; //string filePath = System.IO.Path.Combine(streamingAssetPath, asset.FileName); var filePath = streamingAssetPath + platformPrefix + asset.FileName; //useful for uploading from crossplatform (iOS / Android) clients if (filePath.Contains("://")) { var www = new WWW(filePath); yield return(www); asset.LocalPutPath = www.text; } else { asset.LocalPutPath = filePath; } }
public IEnumerator DownloadAndUnpackAsset(AssetBundleHelperObject asset) { // Caching.IsVersionCached(asset.GetUrl, asset.Version) // Start a download of the given URL var www = WWW.LoadFromCacheOrDownload(asset.GetUrl, asset.Version); // wait until the download is done while (www.progress < 1) { asset.progress = www.progress; yield return(www); } if (!string.IsNullOrEmpty(www.error)) { asset.Error = www.error; Debug.LogError("HTTP ERROR:" + asset.ContentKey + "\n" + www.error); yield break; } asset.Error = ""; asset.Unpacked.ContentKey = asset.ContentKey; asset.Unpacked.PromoId = asset.FileName; asset.Bundle = www.assetBundle; var assetNames = asset.Bundle.GetAllAssetNames(); foreach (var assetName in assetNames) { var bannerUri = string.Empty; var splashUri = string.Empty; var assetNameLc = assetName.ToLower(); var isImage = assetNameLc.EndsWith(".jpg") || assetNameLc.EndsWith(".png"); if (assetName.ToLower().Contains("banner.") && isImage) { bannerUri = assetName; } else if (assetName.ToLower().Contains("splash.") && isImage) { splashUri = assetName; } if (string.IsNullOrEmpty(bannerUri) == false) { asset.Unpacked.Banner = asset.Bundle.LoadAsset <Texture2D>(bannerUri); } else if (string.IsNullOrEmpty(splashUri) == false) { asset.Unpacked.Splash = asset.Bundle.LoadAsset <Texture2D>(splashUri); } else { asset.Error += string.Format("[Err: Unpacking: {0} -- {1} ]", asset.FileName, assetName); } } asset.Bundle.Unload(false); asset.IsUnpacked = true; }
/// <summary> /// Puts the file. /// </summary> /// <param name="postUrl">Remote URL to use (obtained from GetContentUploadUrl) </param> /// <param name="payload">The file to send converted to a byte[] </param> public void PutFile(AssetBundleHelperObject asset, byte[] payload) { var request = (HttpWebRequest)WebRequest.Create(asset.PutUrl); request.Method = "PUT"; request.ContentType = asset.MimeType; if (payload != null) { Stream dataStream = request.GetRequestStream(); dataStream.Write(payload, 0, payload.Length); dataStream.Close(); } else { Debug.LogWarning(string.Format("ERROR: Byte arrry was empty or null")); return; } Debug.Log("Starting HTTP PUT: " + asset.FileName); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { Debug.Log("HTTP PUT Successful:" + asset.FileName); } else { Debug.LogWarning(string.Format("ERROR: Asset:{0} -- Code:[{1}] -- Msg:{2}", asset.FileName, response.StatusCode, response.StatusDescription)); } }
/// <summary> /// Puts the file. /// </summary> /// <param name="postUrl">Remote URL to use (obtained from GetContentUploadUrl) </param> /// <param name="payload">The file to send converted to a byte[] </param> public void PutFile(AssetBundleHelperObject asset, byte[] payload) { if (payload == null) { Debug.LogWarning("ERROR: Byte arrry was empty or null"); return; } var request = (HttpWebRequest)WebRequest.Create(asset.PutUrl); request.Method = "PUT"; request.ContentType = asset.MimeType; using (var dataStream = request.GetRequestStream()) dataStream.Write(payload, 0, payload.Length); var response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode != HttpStatusCode.OK) { Debug.LogWarning(string.Format("ERROR: Asset:{0} -- Code:[{1}] -- Msg:{2}", asset.FileName, response.StatusCode, response.StatusDescription)); } }
/// <summary> /// Requests a remote endpoint for downloads from the PlaFab service. /// </summary> void GetContentDownloadURL(AssetBundleHelperObject asset) { var request = new GetContentDownloadUrlRequest(); if (asset.BundlePlatform == AssetBundleHelperObject.BundleTypes.Android) { request.Key = "Android/" + asset.ContentKey; // folder location & file name to use on the remote server } else if (asset.BundlePlatform == AssetBundleHelperObject.BundleTypes.iOS) { request.Key = "iOS/" + asset.ContentKey; } else // stand-alone { request.Key = asset.ContentKey; } PlayFabClientAPI.GetContentDownloadUrl(request, result => { asset.GetUrl = result.URL; }, OnPlayFabError); }
public IEnumerator DownloadAndUnpackAsset(AssetBundleHelperObject asset) { Debug.Log(Caching.IsVersionCached(asset.GetUrl, asset.Version)); // Start a download of the given URL Debug.Log("HTTP GET:" + asset.ContentKey); var www = WWW.LoadFromCacheOrDownload(asset.GetUrl, asset.Version); // wait until the download is done while (www.progress < 1) { asset.progress = www.progress; yield return(www); } if (string.IsNullOrEmpty(www.error)) { asset.Error = ""; asset.Unpacked.ContentKey = asset.ContentKey; asset.Unpacked.PromoId = asset.FileName; asset.Bundle = www.assetBundle; string[] names = asset.Bundle.GetAllAssetNames(); //Debug.Log(names.ToString()); foreach (var name in names) { string bannerURI = string.Empty; string splashURI = string.Empty; Debug.Log("Unpacking:" + name); if (name.Contains("banner.png") || name.Contains("Banner.png") || name.Contains("banner.jpg") || name.Contains("banner.jpg")) { bannerURI = name; } else if (name.Contains("splash.png") || name.Contains("Splash.png") || name.Contains("splash.jpg") || name.Contains("Splash.jpg")) { splashURI = name; } if (string.IsNullOrEmpty(bannerURI) == false) { Texture2D banner = asset.Bundle.LoadAsset <Texture2D>(bannerURI); asset.Unpacked.Banner = banner; } else if (string.IsNullOrEmpty(splashURI) == false) { Texture2D splash = asset.Bundle.LoadAsset <Texture2D>(splashURI); asset.Unpacked.Splash = splash; } else { asset.Error += string.Format("[Err: Unplacking: {0} -- {1} ]", asset.FileName, name); } } asset.Bundle.Unload(false); asset.IsUnpacked = true; //this.isImageDownloaded = true; yield break; } else { asset.Error = www.error; Debug.Log("HTTP ERROR:" + asset.ContentKey); } yield break; }