public PanoDownloader() { m_UserDownloader = new UserPanoDownloader(); m_GoogleDownloader = new GooglePanoDownloader(); m_UserDownloader.OnStarted += () => OnStarted.TryInvoke(); m_UserDownloader.OnLoaded += tex => OnLoaded.TryInvoke(tex); m_UserDownloader.OnFailed += exception => OnFailed.TryInvoke(exception); m_GoogleDownloader.OnStarted += () => OnStarted.TryInvoke(); m_GoogleDownloader.OnLoaded += tex => OnLoaded.TryInvoke(tex); m_GoogleDownloader.OnFailed += exception => OnFailed.TryInvoke(exception); }
/// <summary> /// Downloads the panorama image using the known PanoID /// </summary> /// <param name="panoID">The PanoID to be downloaded</param> /// <param name="size">The <see cref="PanoSize"/> of the image to be downloaded.</param> /// <param name="onResult">Callback containing the Texture2D of the pano image</param> /// <param name="onException">Callback containing the exception when the download fails</param> public void Download(string panoID, PanoSize size, TextureFormat format, Action <Texture32> onResult = null, Action <Exception> onException = null) { var width = PanoUtility.GetUserPanoWidth(size); string url = "https://lh5.googleusercontent.com/p/" + panoID + "=w" + width; m_Running = true; OnStarted.TryInvoke(); new RestClient().ExecuteAsync(new RestRequest(url, Method.GET), out m_Handle) .Then(response => { if (!m_Running) { return; } Dispatcher.Enqueue(() => { if (response.IsSuccess()) { var texture = new Texture2D(1, 1, format, true); texture.LoadImage(response.RawBytes); var result = Texture32.FromTexture2D(texture); MonoBehaviour.Destroy(texture); texture = null; onResult.TryInvoke(result); OnLoaded.TryInvoke(result); } else { OnFailed.TryInvoke(response.GetException()); onException.TryInvoke(response.GetException()); } }); }) .Catch(exception => { if (!m_Running) { return; } onException.TryInvoke(exception); }); }
IEnumerator DownloadCo(string panoID, PanoSize size, TextureFormat format, Action <Texture32> onResult, Action <Exception> onException) { OnStarted.TryInvoke(); var uRes = PanoUtility.GetUntrimmedResolution(size); m_Texture = new Texture32((int)uRes.x, (int)uRes.y); var count = PanoUtility.GetTileCount(size); int xCount = (int)count.x; int yCount = (int)count.y; int req = xCount * yCount; int success = 0; int failed = 0; bool done = false; for (int i = 0; i < xCount; i++) { for (int j = 0; j < yCount; j++) { var x = i; var y = j; DownloadTile(panoID, x, y, size, format) .Then(tile => { success++; StitchTexture(tile, size, x, ((int)count.y - 1) - y); if (success == req) { done = true; CropTexture(size); onResult.TryInvoke(m_Texture); OnLoaded.TryInvoke(m_Texture); } }) .Catch(exception => { if (x == 0 && yCount > y) { yCount = y; req = xCount * yCount; } if (y == 0 && xCount > x) { xCount = x; req = xCount * yCount; } failed++; if (failed == req) { var thrown = new Exception("Could not download the pano image. ID or URL is incorrect."); OnFailed.TryInvoke(thrown); onException.TryInvoke(thrown); return; } if (success == req && !done) { done = true; CropTexture(size); onResult.TryInvoke(m_Texture); OnLoaded.TryInvoke(m_Texture); } }); yield return(null); } } }