我现在已经编码了很多月亮,但是我对 Unity 和 C # 非常陌生。目前,我正在尝试创建一个脚本来显示相机提要,当按下按钮时,将该提要静止,以便我可以在其上运行 OCR 算法。我从这里获取了大部分代码(以及代码注释):Can I take a photo in Unity using the device's camera?
我遇到的问题是,我不断得到这个错误的统一,阻止我甚至运行代码:
资产 / 脚本 / WebCam.cs (22,42):错误 CS0103:名称“webCamTexture”在当前上下文中不存在
第 22 行位于我的代码的 TakePhoto () 函数中。这似乎是一个范围问题,但据我所知,由于我对 C # 的掌握非常有限,“webCamTexture”变量在类范围内。因此,它应该可以在任何类 WebCam 的方法中访问。但正如我之前所说,我对 C # 和 Unity 仍然很陌生,所以也许我只是缺少一些东西。
可能值得注意的是,对于那些只是 C # 人员并且以前没有使用过 Unity 的人来说,start()函数应该在应用程序启动时立即运行。因此,从理论上讲,我希望在执行 TakePhoto()函数时,“webCamTexture”变量应该始终具有非空值。
任何帮助将不胜感激。这里有问题的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class WebCamScript : MonoBehaviour
{
public RawImage rawimage;
private WebCamTexture webcamTexture;
private void Start()
{
Debug.Log("Hello World!: WebCamScript Start()");
if (webcamTexture == null)
{
webcamTexture = new WebCamTexture();
}
rawimage.material.mainTexture = webcamTexture;
if (!webcamTexture.isPlaying)
{
webcamTexture.Play();
}
}
public IEnumerator TakePhoto() // Start this Coroutine on some on click
{
// NOTE - you almost certainly have to do this here:
yield return new WaitForEndOfFrame();
// it's a rare case where the Unity doco is pretty clear,
// http://docs.unity.com/ScriptReference/WaitForEndOfFrame.html
// be sure to scroll down to the SECOND long example on that doco page
Texture2D photo = new Texture2D(webcamTexture.width, webcamTexture.height);
photo.SetPixels(webcamTexture.GetPixels());
photo.Apply();
// //Encode to a PNG
// byte[] bytes = photo.EncodeToPNG();
// //Write out the PNG. Of course you have to substitute your_path for something sensible
// File.WriteAllBytes(your_path + "photo.png", bytes);
}
}
更新:是一个错字。看起来我所需要的只是一双新鲜的眼睛。谢谢大家。话虽如此,我仍然有一些问题让我挠头。当我将其链接到我的按钮时,我仍然没有看到我的“takePhoto”功能。有什么想法吗?我更新了上面的代码以反映其当前状态。参见照片:missing script in drop down
如果这是你的确切代码,你定义了一个 webcamTexture 变量,但你正在尝试使用 webCamTexture:注意大写 / 小写,保持你的命名一致:)
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(57条)