【问题标题】:Unity WebGL WebcamTexture webcam light stays on after disabling cameraUnity WebGL WebcamTexture 网络摄像头灯在禁用摄像头后一直亮着
【发布时间】:2017-04-04 16:16:25
【问题描述】:

我正在使用以下脚本来启用/禁用 WebGL 上的网络摄像头。

它在编辑器上工作正常,但在浏览器上,网络摄像头灯在禁用 WebcamTexture 后保持亮起。

它发生在 Chrome 和 Firefox 上。

有什么想法吗?

谢谢。

WebCamTexture _webcamTexture;

public void Enable()
{
    #if UNITY_EDITOR || DEVELOPMENT_BUILD
    Debug.Log("Enable");
    #endif

    _enabled = true;
}

public void Disable()
{
    #if UNITY_EDITOR || DEVELOPMENT_BUILD
    Debug.Log("Disable");
    #endif

    _enabled = false;
}

#region MONOBEHAVIOUR

void Update()
{
    if(_enabled)
    {
        if(_webcamTexture == null)
        {
            while(!Application.RequestUserAuthorization(UserAuthorization.WebCam).isDone)
            {
                return;
            }

            if (Application.HasUserAuthorization(UserAuthorization.WebCam)) 
            {
                #if UNITY_EDITOR || DEVELOPMENT_BUILD
                Debug.Log("Webcam authorized");
                #endif

                _webcamTexture = new WebCamTexture (WebCamTexture.devices[0].name);
                _webcamTexture.Play (); 
            } 
            else 
            {
                #if UNITY_EDITOR || DEVELOPMENT_BUILD
                Debug.Log("Webcam NOT authorized");
                #endif
            }   
        }
        else if (_webcamTexture.isPlaying)
        {
            if(!_ready)
            {
                if (_webcamTexture.width < 100)
                {
                    return;
                }

                _ready = true;
            }

            if(_webcamTexture.didUpdateThisFrame)
            {
                _aspectRatioFitter.aspectRatio =  (float)_webcamTexture.width / (float)_webcamTexture.height;

                _imageRectTransform.localEulerAngles = new Vector3 (0, 0, -_webcamTexture.videoRotationAngle);

                _image.texture = _webcamTexture;
            }
        }
    }
    else 
    {
        if(_webcamTexture != null) 
        {
            _webcamTexture.Stop ();
            _webcamTexture = null;

            _image.texture = null;
        }
    }
}

#endregion

【问题讨论】:

    标签: c# unity3d camera webcam unity-webgl


    【解决方案1】:

    代码在编辑器中工作的唯一原因是编辑器是为你清理一些东西的。单击停止后,即使没有调用WebCamTexture.Stop ();,相机也会自动停止。

    不幸的是,这不会发生在构建中。你必须明确地调用WebCamTexture.Stop (); 来阻止它。执行此操作的正确位置是在 Disable() 函数中。

    public void Disable()
    {
        if(_webcamTexture != null) 
        {
            _webcamTexture.Stop ();
        }
    }
    

    编辑:

    不要使用布尔变量来禁用相机,而是创建一个函数并将该函数连接到您的停止按钮。当调用该函数时,它会停止摄像头。

    public void disableCamera()
    {
        if(_webcamTexture != null) 
        {
            _webcamTexture.Stop ();
        }
    }
    

    【讨论】:

    • 感谢您的回答。我正在调用 _webcamTexture.Stop ();当 _enabled 设置为 false 时,在 Update 内部。我已经在 Disable 方法中尝试过了。应该没什么区别吧?
    • 我知道你是。那是完全错误的。 Update 函数在应用程序存在时终止。如果那个 if 语句甚至没有运行怎么办?这就是为什么你应该在OnDisable 函数而不是Update 函数中停止/结束内容。试试我的解决方案。关闭选项卡时会出现此问题?
    • 当我按下 UI 按钮时会调用 Disable 方法。我不会终止应用程序。我想在应用程序运行时启用/禁用相机。当我关闭标签时,灯会熄灭。
    • 糟糕。我明白现在是什么问题了。第一次弄错了为什么不将_webcamTexture.Stop (); 放在一个公共函数中,然后将该函数连接到一个按钮。当你按下它时,函数调用_webcamTexture.Stop ();。这比你现在拥有的要好。
    • 是的,在我将它放入 Disable 函数(由 UI 按钮调用)之前,但它是相同的。我会把它移回函数中,这是我在做的实验。不过谢谢
    猜你喜欢
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 2018-03-18
    相关资源
    最近更新 更多