In C# I’ve added a webcam using the following code:
public WebCamTexture camTexture;
then in the start:
camTexture = new WebCamTexture();
camTexture.requestedHeight = Screen.height;
camTexture.requestedWidth = Screen.width;
camTexture.Play();
W = camTexture.width;
H = camTexture.height;
Works fine in the editor, etc but deployed to a Surface RT I get:
No available webcams are found. Either there is no webcam connected, or they are all in use by other applications (like skype)
I’ve restarted and cold booted but it makes no difference, is the webcam not supported yet? If not does anyone have a link to unsupported features? Or the timescales for when they will be supported.
Made some progress, I used the debugger (which took a little effort to get going I think their might be a slight issue matching up the pdb to the modules) to enumerate the devices which worked great, then used the following code:
screenRect = new Rect(0, 0, Screen.width, Screen.height);
var devices = WebCamTexture.devices;
var deviceName = devices[0].name;
camTexture = new WebCamTexture(deviceName,320, 240, 30);
This works and creates the camera okay, but the following:
camTexture.Play();
Fails with the following error:
Error: Operation has failed with error 0x80000000e: A method was called at an unexpected time.
( [App path is here]/TaskWrapper.h:create_task::<lambda_xxx>::operator() at 93)
This was fired during the Start on a camera gameObject. Also please note replacing void Start with IEnumerable Start meant the Start did not fire at all.
While running the app, swipe from the right side of the screen to open the side menu (whatever it’s called), then ‘settings’ → ‘permissions’ → webcam.
The app I’ve been developing seems to randomly drop the permission for using the webcam, and I need to give the permission from there. After that it seems to work. Not 100% if it’s the same problem (error code) though.
Okay so I’ve made some progress with this, the following causes an error:
material.mainTexture=camTexture;
Though I believe the error is just caused by creating a WebCamTexture and having a MeshFilter or MeshRenderer on the same gameObject. The only work around I can find is using GUI.DrawTexture…which is a bit of a shame but for my circumstances I can work around it.
The other issue is you cannot use:
camTexture.GetPixels32();
This causes the error:
A first chance exception of type ‘System.NullReferenceException’ occurred in UnityEngineProxy.DLL
However you can use:
camTexture.GetPixels();
Which again is fine for me as I can just convert my code to use the float values rather than the byte values.
I’m quite sure I replied to this post but for some reason it didn’t get through.
I thought I was the only one getting the strange “realGfxError”, but I guess i’m not alone. At least to me that error appears when I initialize open the camera in a debug/release build, NOT in the editor. Usually the camera starts like it should, even if I get that error message. I haven’t tried GetPixels32, but at least GetPixels works, like with you. I believe the line in the code that triggers the error is the one where you “start” the camera, ie:
camTexture.Play();
Dunno if you’ve seen it but I posted a thread about this some time ago as well, title for the thread is “Strange problems with microphone and webcam”.
I managed to solve my issues, the thing that worked for me was:
Ensure you have an issolated script that only creates the webcamtexture and wraps up the playing etc, then have the WebCamTexture public.
After you call play wait until isPlaying returns true until you access any of the properties (GetPixels or try to set a texture to equal WebCamTexture)
Make sure you tick WebCam in the unity permissions section for windows 8, some people say you have to do this in visual studio too, for me it was automatic.
On the windows rt machine, ensure you have gone to settings in the side panel and allowed camera permissions.
This then sorted the camera issues for me, on the main it seemed to be either having a meshrenderer on the same script, or not waiting for isPlaying, and for anyone wanting the Color32[ ] you can just convert the return from GetPixels, which is not big deal, you can run it within a separate thread easily enough,
Color32[ ] cArray=new Color32[this.c.Length];
for(int i=0;i<this.c.Length;i++)
{
cArray_=this.c*;_
_ }*_ Where this.c is your array from GetPixels! So very simple! So the material.mainTexture=WebCamTexture works fine, I did it on a seperate gameobject just to be safe!