WebCamTexture在Unity3D中不显示
本文关键字:显示 Unity3D WebCamTexture | 更新日期: 2023-09-27 18:18:20
我试图在现场显示网络摄像头输入,使用WebCamTexture
。我已经创建了一个带有默认纹理的精灵,并将以下脚本附加到它上面:
public class CameraTexture : MonoBehaviour {
void Start () {
WebCamTexture webcamTexture = new WebCamTexture();
renderer.material.mainTexture = webcamTexture;
webcamTexture.Play();
}
}
但是,当我运行场景时(在PC上),相机输入不显示,只有默认纹理。以下是我所知道的:
-
webcamTexture
不为空,设备已正确找到 - 相机设备工作正常
- 其他应用程序没有阻塞摄像头
为什么摄像头输入不显示?
我自己找到了解决办法。
webcamTexture
不能在雪碧上显示。我不得不创建一个GUITexture,然后我附加了相同的脚本,一切都很好。
这实际上并不完全正确,我已经创建了一个基于Camera Capture Kit的小示例,这是一个资产,用于渲染Native Camera feed (iOS/Android)并拍摄快照/照片到纹理-这使得渲染到Sprite成为可能。
下面是如何渲染到精灵 的示例http://stackoverflow.com/questions/30190333/add-renderable-webcamtexture-in-spriterender-unity2d/36889806#36889806
本质上你需要做的是确保相机的输入设置在自定义着色器中的次要材质纹理单元上,然后你创建一个带有空白白色纹理的精灵,并给精灵渲染器一个对纹理的引用。
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);