Skip to content

Instantly share code, notes, and snippets.

@radiatoryang
Created April 9, 2015 03:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radiatoryang/f7438068e71d17a14faa to your computer and use it in GitHub Desktop.
Save radiatoryang/f7438068e71d17a14faa to your computer and use it in GitHub Desktop.
Unity C# code for grabbing a JPG (or PNG) off the internet and bringing into Unity
using UnityEngine;
using System.Collections;
// full reference: http://docs.unity3d.com/ScriptReference/WWW.html
// usage: place this on a gameObject with Mesh Filter and Mesh Renderer (e.g. a Quad, a Plane, a Cube)
// NOTE: this does NOT work in Web Player (browser security sandbox)
public class DemoWebcam : MonoBehaviour {
// demo URL: http://207.251.86.238/cctv446.jpg (NYC DOT webcam, 14th st @ 6 av)
// list of NYC DOT webcams: http://nyctmc.org/multiview2.php
public string jpegUrl = "http://207.251.86.238/cctv446.jpg";
// Start can be used as a coroutine
IEnumerator Start() {
// create a new "WWW" object that will fetch the web data
WWW webRequest = new WWW( jpegUrl );
// wait until the web data has finished downloading
yield return webRequest;
// you could also see whether (WWW.isDone == true) to see if it has finished yet
// treat the web data like an image (only works for .JPG, .PNG)
renderer.material.mainTexture = webRequest.texture;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment