Created
August 6, 2017 11:41
-
-
Save tarukosu/c9e783d45fa2e6c61849abe0a7d4a28c to your computer and use it in GitHub Desktop.
GetWebTexture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.Networking; | |
using System.Collections.Specialized; | |
using System.Linq; | |
using UnityEngine.UI; | |
public class GetWebTexture : MonoBehaviour | |
{ | |
public string Server = "http://localhost:32768/"; | |
public float WaitTime = 2; | |
public bool RenderAll = true; | |
void Start() | |
{ | |
} | |
public void OpenPage(string keyword) | |
{ | |
StartCoroutine(GetTexture(keyword)); | |
} | |
IEnumerator GetTexture(string keyword) | |
{ | |
NameValueCollection parameters = new NameValueCollection | |
{ | |
{ "url", "http://www.google.com/search?&sourceid=navclient&btnI=I&q=" + keyword}, | |
{ "wait", WaitTime.ToString() }, | |
{ "width", "1200" }, | |
{ "render_all", RenderAll ? "1" : "0" } | |
}; | |
string uri = Server + "render.jpeg" + ToQueryString(parameters); | |
using (UnityWebRequest www = UnityWebRequest.GetTexture(uri)) | |
{ | |
yield return www.Send(); | |
if (www.isError) | |
{ | |
Debug.Log(www.error); | |
} | |
else | |
{ | |
Texture myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture; | |
var width = myTexture.width; | |
var height = myTexture.height; | |
var rectTransform = GetComponent<RectTransform>(); | |
if (rectTransform) | |
{ | |
var size = rectTransform.sizeDelta; | |
rectTransform.sizeDelta = new Vector2(size.x, size.x * height / width); | |
} | |
var rawImage = GetComponent<RawImage>(); | |
if (rawImage) | |
{ | |
rawImage.texture = myTexture; | |
} | |
} | |
} | |
} | |
private string ToQueryString(NameValueCollection nvc) | |
{ | |
return "?" + string.Join("&", nvc.AllKeys.Select(key => string.Format("{0}={1}", WWW.EscapeURL(key), WWW.EscapeURL(nvc[key]))).ToArray()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment