Created
April 9, 2015 03:21
-
-
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
This file contains hidden or 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; | |
// 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