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!
UnityEngine.WebCamTexture:Play()[/sourcecode]
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");
}
[/sourcecode]
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?
[ad]
Leave a Reply