Unity Video Remake: Webcam Textures

iKriz Avatar





last modified on

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)

[sourcecode]Cannot use web cam, since the user has not authorized this!

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:

[sourcecode lang=”csharp”]
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:

[sourcecode]error CS0246: The type or namespace name `WebCamTexture’ could not be found.[/sourcecode]

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?



43 responses to “Unity Video Remake: Webcam Textures”

  1. solehome Avatar

    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

    1. iKriz Avatar

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

  2. Kealkeal Avatar

    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.


    1. iKriz Avatar

      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.

      1. Usama Javed Avatar

        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?

        Usama Javed
  3. […] in Unity free version to show it’s possible without having the pro version. This uses the webcam script I created earlier on a simple plane. Share […]

  4. Mohsin Avatar

    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

  5. rodripf Avatar

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

  6. farahsana Avatar

    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?

    1. iKriz Avatar

      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.

  7. Eduardo Rodriguez Avatar

    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?

    Eduardo Rodriguez
    1. iKriz Avatar

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

  8. tohm Avatar

    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

    1. iKriz Avatar

      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

  9. Florian Alexander Veltman Avatar

    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

    Florian Alexander Veltman
  10. Stephan Avatar

    Thanks for the script, very handy!

  11. predatflaps Avatar

    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 !

    1. iKriz Avatar

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

  12. Jonas Avatar

    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

    1. iKriz Avatar

      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

  13. cvile Avatar

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

  14. Thorsteinn Avatar

    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?

    1. Thorsteinn Avatar

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

      1. iKriz Avatar

        just now noticed the problem thnx for the notification.. Fixed the problem 🙂

    2. iKriz Avatar

      should be fixed now 🙂

  15. al Avatar

    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!

    1. iKriz Avatar

      Scripts are back up again download script was broken so fixed

  16. Mark Avatar

    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!

    1. iKriz Avatar

      Scripts are back up

      1. Chris Avatar

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

  17. Martin Avatar

    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

    1. iKriz Avatar

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

      1. Shashank Singh Avatar

        can you please elaborate on the procedure to add the above code. can you help me by telling the folder to which this code should be added and all such details.
        I am totally new and using unity for my school project.

  18. Mike Avatar

    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

    1. Mike Avatar

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

  19. str Avatar

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

  20. Toni Avatar

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

    1. iKriz Avatar

      Removed the link and put it up on github 🙂

  21. Toni Avatar

    Thanks 😉

  22. jbachalo Avatar

    Get error MissingComponentException: There is no ‘Renderer’ attached to the

  23. Adrian Avatar

    Can I have an IP Camera or a go pro as a feed/strem of video?

  24. Angelos Avatar

    Is it possible to choose the webcam resolution?

    1. iKriz Avatar

      That depends on your webcam, if your webcam supports that resolution. You can only request a resolution.


Leave a Reply

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