WebCamTexture报错

使用WebCamTexture把相机的画面显示到 RawImage 上

using UnityEngine;
using UnityEngine.UI;

public class WebCamTextureTest : MonoBehaviour
{
    public RawImage RawImage;
    
    private WebCamTexture _webCamTexture;
    private Color32[] _colorBuff;
    private Texture2D _texture2D;
    
    void Start()
    {
        WebCamDevice[] devices = WebCamTexture.devices;
        int length = devices.Length;
        if (length > 0)
        {
            _webCamTexture = new WebCamTexture();
            _webCamTexture.Play();
        }
    }
    
    void Update()
    {
        if (_webCamTexture != null && _webCamTexture.isPlaying && _webCamTexture.didUpdateThisFrame)
        {
            int width = _webCamTexture.width;
            int height = _webCamTexture.height;

            if (_colorBuff == null)
            {
                _colorBuff = new Color32[width * height];
            }
            
            _webCamTexture.GetPixels32(_colorBuff);
            
            if (_texture2D == null)
            {
                _texture2D = new Texture2D(width, height, TextureFormat.RGBA32, false);
            }
            _texture2D.SetPixels32(_colorBuff);
            _texture2D.Apply();
            RawImage.texture = _texture2D;
        }
    }
}

执行 WebCamTexture.Play() 报下面的错误
在这里插入图片描述
Could not start graph
Could not pause pControl

这两个错是因为摄像头被占用,需要检查代码中其他调用摄像头的地方

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值