WebCamTexture not showing in Unity3D?
I am trying to display a webcam login on stage using WebCamTexture
. I created a Sprite with some default texture and connected the following script to it:
public class CameraTexture : MonoBehaviour {
void Start () {
WebCamTexture webcamTexture = new WebCamTexture();
renderer.material.mainTexture = webcamTexture;
webcamTexture.Play();
}
}
But when I run the scene (on PC), the camera input is not shown, only the default texture. Here's what I know:
-
WebCamTexture
is not null, the device was found correctly. Device - works correctly.
- other apps do not block the camera
Why is the camera input not showing?
source to share
Actually this is not entirely correct, I created a small example based on the Camera Capture Kit, which is an asset for rendering the Native Camera (iOS / Android) channel and takes a snapshot / photo for a texture, which allows it to be attributed to a sprite.
Here is an example of how you can render a sprite
http://stackoverflow.com/questions/30190333/add-renderable-webcamtexture-in-spriterender-unity2d/36889806#36889806
Basically you need to make sure the camera input is set to the secondary material block of the texture in the custom shader, then you create a sprite with an empty white texture and you give the sprite renderer a reference to the texture.
source to share
Texture2D texty = new Texture2D (webcamTextures.width, webcamTextures.height, TextureFormat.RGB24, false);
texty.SetPixels (webcamTextures.GetPixels());
texty.Apply ();
Rect newRect = new Rect (0f, 0f, texty.width, texty.height);
Vector2 pivot = new Vector2 (0.5f, 0.5f);
imageUI.sprite = Sprite.Create (texty, newRect, pivot, 100f);
source to share