- Home /
Translating from javascript into C# (web camera stream code)
Hello guys. I want to have a web camera stream in my project. I found the code and it is working perfectly. I would like to reproduce that code in C# but i lack the skills. Can anyone help out ???
// Continuously get the latest webcam shot from outside "Friday's" in Times Square // and DXT compress them at runtime var url = "";
function Start () { // Create a texture in DXT1 format renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false); while(true) { // Start a download of the given URL var www = new WWW(url);
// wait until the download is done
yield www;
// assign the downloaded image to the main texture of the object
www.LoadImageIntoTexture(renderer.material.mainTexture);
}
}
Thanks.
Answer by duck · Apr 13, 2010 at 08:24 PM
Something like this should do it. (name your script "WebCam" to match the class).
using UnityEngine; using System.Collections;
public class WebCam : MonoBehaviour {
public string url = "";
void Start()
{
renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
StartCoroutine(UpdateCam());
}
IEnumerator UpdateCam()
{
while (true)
{
Debug.Log("reloading webcam");
WWW www = new WWW(url);
yield return www;
www.LoadImageIntoTexture((Texture2D)renderer.material.mainTexture);
}
}
}
(Edit - now tested, and bugs fixed!)
Nice code. 2 small errors. 1. You declare the variable www twice, once in the parentheses and once inside the method so you should take out the one in parentheses. 2. You need to cast mainTexture into Texture2D. Besides that perfect :)
Thanks for the pointers - fixed in the example now. Also I changed to to yield directly using the www rather than yielding null while polling www.isDone. $$anonymous$$uch neater.
because the code repeated loads the next camera frame. There's a "yield" inside the loop, so the code doesn't block the rest of your app (it releases control to the rest of your program while waiting for the next WWW to complete).
Your answer
Follow this Question
Related Questions
Translate this statement from .js to C# 2 Answers
converting Photon stream to JS 2 Answers
Translate C# into Js 1 Answer
Transform.Translate change distance and speed 1 Answer
Translate Unity-scripts to uScript 1 Answer