Skip to content

Instantly share code, notes, and snippets.

@tarukosu
Created August 6, 2017 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarukosu/c9e783d45fa2e6c61849abe0a7d4a28c to your computer and use it in GitHub Desktop.
Save tarukosu/c9e783d45fa2e6c61849abe0a7d4a28c to your computer and use it in GitHub Desktop.
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