x


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.

more ▼

asked Apr 13 '10 at 08:01 PM

gotmilk gravatar image

gotmilk
45 3 4 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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!)

more ▼

answered Apr 13 '10 at 08:24 PM

duck gravatar image

duck ♦♦
40.9k 92 148 415

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 :)

Apr 13 '10 at 09:44 PM Peter G

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. Much neater.

Apr 13 '10 at 10:09 PM duck ♦♦

Thank you. Worked like a charm :)

Apr 14 '10 at 04:16 PM gotmilk

Why is while(true) never breaked?

Jan 11 '11 at 02:15 AM brandonc

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).

Jan 11 '11 at 12:37 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3443
x211
x204
x39

asked: Apr 13 '10 at 08:01 PM

Seen: 2344 times

Last Updated: Apr 13 '10 at 08:01 PM