Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
GetWebTexture.cs
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