Unity Video Remake: Webcam Textures

Posted by on Dec 23, 2011 in Featured, Unity | 4 comments

Unity Video Remake: Webcam Textures

A while back I posted something about video capture in Unity with a sad plea for beta access to version 3.5 which is now finally in public beta, so I decided to do a new post.

Setting up a Webcam Texture in Unity

Now as you may have seen in the Script Reference (the only place to actually find something about a WebcamTexture) there is no Object like a RenderTexture for a WebCamTexture which makes it a tad more harder to work with (read: not as easy to drag and drop in to materials).

Quickest way to get this working is to drop the following script on an object.

Webcam Script (425)

Keep in mind that this in not the best way for multiple objects unless they all have the same material with the script on one object.
You can only have one WebCamTexture per webcam!

By the way.. Once 3.5 goes fully public I’ll put up a working example to give you a starting point, and so you can see yourself (if you have a camera connected of course) so keep an eye on my site ;)

Once you’ve got the camera running it’s possible to do all kinds of things.. I Look forward to the future of Webcams in Unity all over the internets. ;)

Issues: Cannot use web cam, since the user has not authorized this!

In your attempt at getting the webcam running you may have come across the following error: (I did at least)

Cannot use web cam, since the user has not authorized this!
UnityEngine.WebCamTexture:Play()

You can squash this little one by changing your build platform to Standalone Mac/PC. Go to Build Settings -> Select “PC and Mac Standalone” then click that little button at the bottom left “Switch Platform”, this, I think is something new.

The whole reason for this is because you probably have the Web player platform setup by default. This requires the user to first give permission to use their webcam because of privacy reasons. If you want to do this then you need to request the users permission by using the following piece of code:

IEnumerator Start()
{
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
if(Application.HasUserAuthorization(UserAuthorization.WebCam)) Debug.Log("Authorized");
}

Same goes for UserAuthorization.Microphone. When you use a standalone platform the authorization is no longer needed.

Issues: The type or namespace name `WebCamTexture’ could not be found.

Thanks to solehome for commenting on this one. The public beta 3.5 seems to have an error when using WebCamTextures in Flash builds.
If you choose to build on a flash platform the following errors occurs:

error CS0246: The type or namespace name `WebCamTexture’ could not be found.
Are you missing a using directive or an assembly reference?

Error building Player because scripts had compiler errors
Solution to this problem… euhm dont build flash?

4 Comments

  1. The webcam feature seems not work for flash build.
    If chosing flash platform to build, errors occurs as:

    Assets/camBind.cs(6,17): error CS0246: The type or namespace name `WebCamTexture’ could not be found. Are you missing a using directive or an assembly reference?
    Error building Player because scripts had compiler errors

    • Indeed had the same problem when building for flash, I guess that needs a bug submit. Web player and standalone work fine though.

  2. Really great job !

    I have a question.

    For this time, I can see my own webcam, but can we imagine to see my webcam and the webcam of my friend ?

    Of course, me and my friend will load the Unity file on our own Internet Browser.

    Let me know.

    Thanks

    • For that to happen you would need to write your webcam texture to a location online or stream it via a network connection, and at the same time read the texture of your friends webcam.
      Its possible to do but it wont be as fast. The intention for this script is really local webcam usage only. But your free to do whatever you like with it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>