Skip to content

Commit

Permalink
Updated code to UnityWebRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
bionicl committed Apr 25, 2019
1 parent f9cf5b8 commit d788b64
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Assets/MainScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -25699,7 +25699,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: -2.4958083e-10}
m_AnchoredPosition: {x: 0, y: -0.0000021741748}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 1}
--- !u!114 &1371052592
Expand Down Expand Up @@ -26384,7 +26384,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 275.22113, y: 60}
m_AnchoredPosition: {x: 134.06734, y: 60}
m_SizeDelta: {x: 0, y: 150}
m_Pivot: {x: 1, y: 1}
--- !u!114 &1433522477
Expand Down
26 changes: 18 additions & 8 deletions Assets/Scripts/GoogleMapDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.Networking;

public class GoogleMapDisplay : MonoBehaviour {
public static GoogleMapDisplay instance;
Expand Down Expand Up @@ -154,25 +155,34 @@ public void ChangeMapZoom() {
IEnumerator DownloadMap(Action<Sprite, OneRequest> action, OneRequest request) {
string url = ReturnApiUrl(request);

using (WWW www = new WWW(url)) {
yield return www;
Sprite sprite = Sprite.Create(www.texture,
UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
yield return www.SendWebRequest();

if (www.isNetworkError || www.isHttpError) {
Debug.LogError("DOWNLOAD MAP ERROR: " + www.error);
} else {
Texture2D texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
Sprite sprite = Sprite.Create(texture,
new Rect(0, 0, 640, 640),
new Vector2(0.5f, 0.5f));

action.Invoke(sprite, request);
}
}

IEnumerator DownloadMapRetina(Action<Sprite, OneRequest> action, OneRequest request) {

string url = ReturnApiUrl(request);

using (WWW www = new WWW(url)) {
yield return www;
Sprite sprite = Sprite.Create(www.texture,
UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
yield return www.SendWebRequest();

if (www.isNetworkError || www.isHttpError) {
Debug.LogError("DOWNLOAD MAP RETINA ERROR: " + www.error);
} else {
Texture2D texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
Sprite sprite = Sprite.Create(texture,
new Rect(0, 0, 1280, 1280),
new Vector2(0.5f, 0.5f), 200);

action.Invoke(sprite, request);
}
}
Expand Down
29 changes: 12 additions & 17 deletions Assets/Scripts/GooglePhotosApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,24 +366,19 @@ void AddPhotosToTimeLineItems(GooglePhotosResponse response) {
//}
}
public IEnumerator DownloadImage(string url, Image targetImage) {
// Start a download of the given URL
var www = new WWW(url);
// wait until the download is done
yield return www;
// Create a texture in DXT1 format
Texture2D texture = new Texture2D(www.texture.width, www.texture.height, TextureFormat.DXT1, false);

// assign the downloaded image to sprite
www.LoadImageIntoTexture(texture);
Rect rec = new Rect(0, 0, texture.width, texture.height);
Sprite spriteToUse = Sprite.Create(texture, rec, new Vector2(0.5f, 0.5f), 200);

targetImage.sprite = spriteToUse;

www.Dispose();
www = null;
loadPhotosButtonText.text = "Load photos";
UnityWebRequest www = UnityWebRequestTexture.GetTexture(url);
yield return www.SendWebRequest();

if (www.isNetworkError || www.isHttpError) {
Debug.LogError("DOWNLOAD GOOGLE IMAGE ERROR: " + www.error);
} else {
Texture2D texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
Sprite sprite = Sprite.Create(texture,
new Rect(0, 0, texture.width, texture.height),
new Vector2(0.5f, 0.5f), 200);
targetImage.sprite = sprite;
loadPhotosButtonText.text = "Load photos";
}

}
}

0 comments on commit d788b64

Please sign in to comment.