gpt4 book ai didi

c# - WebCamTexture 在第一秒或第二秒出现全 0

转载 作者:行者123 更新时间:2023-11-30 16:49:00 25 4
gpt4 key购买 nike

我正在使用 WebCamTexture,我在 Start 方法中启动它,然后运行另一个方法。我使用 GetPixels() 获取像素,但是,它们都显示为 (0, 0, 0)。是否有任何解决方法或我可以等待的方法(Unity 似乎使用 while 循环和 WaitForSeconds 崩溃)。这是我当前的启动方法:

void Start () {

rawImage = gameObject.GetComponent<RawImage> ();
rawImageRect = rawImage.GetComponent<RectTransform> ();

webcamTexture = new WebCamTexture();
rawImage.texture = webcamTexture;
rawImage.material.mainTexture = webcamTexture;

webcamTexture.Play();

Method ();

loadingTextObject.SetActive (false);
gameObject.SetActive (true);

}

void Method(){

print (webcamTexture.GetPixels [0]);

}

这每次都会打印 (0, 0, 0) 颜色。

最佳答案

在协程中执行网络摄像头操作,然后使用 yield return new WaitForSeconds(2); 等待 2 秒,然后调用 webcamTexture.GetPixels

void Start () {

rawImage = gameObject.GetComponent<RawImage> ();
rawImageRect = rawImage.GetComponent<RectTransform> ();

StartCoroutine(startWebCam());

loadingTextObject.SetActive (false);
gameObject.SetActive (true);

}

private IEnumerator startWebCam()
{
webcamTexture = new WebCamTexture();
rawImage.texture = webcamTexture;
rawImage.material.mainTexture = webcamTexture;

webcamTexture.Play();

//Wait for 2 seconds
yield return new WaitForSeconds(2);

//Now call GetPixels
Method();

}

void Method(){
print (webcamTexture.GetPixels [0]);
}

或者就像乔在评论部分所说的那样。等待几秒钟是不可靠的。可以等width有东西再读。把

换掉就行了
yield return new WaitForSeconds(2);

while (webcamTexture.width < 100)
{
yield return null;
}

关于c# - WebCamTexture 在第一秒或第二秒出现全 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37379223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com