Unity Video Remake: Webcam Textures

Posted by on Dec 23, 2011 in Featured, Unity | 37 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.

Checkout this script in action Webcam Example

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

If you’d like more help or a better example please donate any amount you wish and i’ll get to it..

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?

[ad]

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

      • hey
        is this possible (reading the textures of friends webcam ) using the networking ability of unity?
        if it isnt can u please give me some pointers that how should i do it?

  3. 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?

    i am still having the same issue can you kindly guide me how to build with out flash :( i m new at this tool

  4. Great! Thanks for the example! It worked out of the box!

  5. MissingComponentException: There is no ‘Renderer’ attached to the “GameObject” game object, but a script is trying to access it.
    You probably need to add a Renderer to the game object “GameObject”. Or your script needs to check if the component is attached before using it.
    Webcam+c__Iterator0.MoveNext () (at Assets/GAME PLAY/Scripts/Game Play/Scene Scripts/Webcam.cs:14)
    UnityEngine.GUI:BeginWindows(Int32, IDList, Int32)

    please tell me how can i resolve this error?

    • You probably need to add a Renderer to the game object “GameObject”. Or your script needs to check if the component is attached before using it.

  6. If i have two webcam connected to my mac, like : a USB cam and the defualt isight camera, how can i choose wich one use?

    • You can determine which webcam to use by setting the Device Name when you do the following:
      webcamTexture = new WebCamTexture(“DeviceName”);

      To check what devices you have you can use:

      void Start ()
      {
      WebCamDevice[] devices = WebCamTexture.devices;
      for( int i = 0 ; i < devices.length ; i++ )
      Debug.Log(devices[i].name);
      }

  7. Thank you very much! How would you get this to work on multiples object (with the same texture) like you said above (i.e., one texture, several planes)? I have tried, but it only works on one GameObject at a time

    • To get this to work on multiple objects you need to assign the webcamTexture to the mainTexture on the object you want it on.
      for example you could do something like the script below this makes it possible to add all the objects you want the webcam texture in the Inspector

      Webcam On Multiple Objects

  8. Hey! Bedankt / thank you so much for this script, this is exactly what I was looking for! Seriously, the whole webcam thing is a big deal for what I’m trying to make, so that makes you awesome. My lack of programming skills are seriously slowing me down so this kind of stuff just saves so much time for me.

    Now to figure out how to get some alpha channels in there lol

  9. Thanks for the script, very handy!

  10. Thanks!!! good work. i just had a go with Unity4 and it only shows a logo at the webcam, couldnt see it nice like in your webplayer version !

    • As soon as I get my hands on Unity4 which will be some time soon, i’ll give the script an update.

  11. Good work.
    I do have one question I hope you can answer.

    I work on mobile devices, and while it works as a charm, rendering the videofeed to an object in 3D space is undesirable (due to different aspect ratios).
    Is there any way to use the feed on GUI objects, e.g. a GUI.Label or GUI.DrawTexture?
    This, for me at least, would be much more convenient, since you don’t have to worry about camera angles/lens width, rotations, different aspects ratios, etc, but can just apply and size the videofeed easily, using screen coordinates.

    I hope I didn’t make a mess of the question and hope you have an answer you are willing to share.

    Best regards,
    – Jonas

    • it may be possible to use GetPixels() on the WebcamTexture and convert it to an image for use in the GUI, only I don’t think a mobile will be able to do it fast enough (depends on hardware) it’s probably easier to scale your 3d mesh to the correct aspect ratio

  12. Does anyone know how to instead of web cam use a xbox kinect as a camera ?

  13. Hey Kriz,

    I was hoping to use your webcam script in a little module I’m creating with Unity but I keep getting a 403 Forbidden error when I try to access it. “You don’t have permission to access /wp-content/plugins/download-monitor/download.php on this server.”
    Have you taken down the script? If so, could I please have permission to use it one more time?

    • Just found the supported scripts on the unity website! Thanks anyways!

      • just now noticed the problem thnx for the notification.. Fixed the problem :)

    • should be fixed now :)

  14. Hi I would like to try the scripts that you created but the link seems broken? I can’t download the scripts and the example was not working either. Hope I got a chance to test it. Thanks!

    • Scripts are back up again download script was broken so fixed

  15. Hi, I’ve recently been trying to do something similar but for some reason I can’t get a webcamtexture to render as a guitexture (I’m trying to have a camera feed in the top corner of my application.) I wanted to look at your script for help in finding out what I’m doing wrong, but it seems it’s no longer hosted, or I can’t access it for whatever reason. Could you please rehost the script or perhaps email me the .txt file?

    Thank you so much, it would be a huge help to me!

    • Scripts are back up

      • They’re back down. Can you update the links please?

  16. Hi all Please could someone tell me where to add the code above to my unity project to enable the camera in web build. please please please.

    Would really appreciate it. Its driving me nuts

    • create a C# script and just replace the above code over the void Start() method..

  17. Hi, thanks for your scripts, they were really helpful, I’ve just a little problem, I have different scenes in the game, one uses the webcam in a texture, it all runs perfectly, but if I change to another scene and then come back to the one I was using, the webcam doesn’t function anymore, but not like it gave me back any errors, so I close the program, start it up again, and everything functions normally. I think the webcam just stays ON even when I change scenes, is there a way to turn it off and on anytime I want? (for example, everytime I change scenes). Thanks

    • Forget it, Problem resolved. Using webCamTexture.Stop(); before changing scene did the trick. Thanks.

  18. Could you please rehost the script if that’s still possible?

  19. Download (webcam.txt) not working, Can you put this again (sorry for my english)

    • Removed the link and put it up on github :)

  20. Thanks ;-)

Trackbacks/Pingbacks

  1. Unity Webcam Example | iKriz.nl - [...] in Unity free version to show it’s possible without having the pro version. This uses the webcam script I …

Leave a Reply