Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d Unity找到画布孩子并隐藏它_Unity3d - Fatal编程技术网

Unity3d Unity找到画布孩子并隐藏它

Unity3d Unity找到画布孩子并隐藏它,unity3d,Unity3d,我正在制作一个截图脚本,当我拍摄时,整个画布都被隐藏起来了。我只想隐藏相机按钮,它是画布的子对象,并在截图后再次显示它。这是我的密码 IEnumerator CaptureIt() { string timeStamp = System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss"); string fileName = "Screenshot" + timeStamp + ".png"; string pathToSave =

我正在制作一个截图脚本,当我拍摄时,整个画布都被隐藏起来了。我只想隐藏相机按钮,它是画布的子对象,并在截图后再次显示它。这是我的密码

IEnumerator CaptureIt()
{
    string timeStamp = System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss");
    string fileName = "Screenshot" + timeStamp + ".png";
    string pathToSave = fileName;
    GameObject.Find("Canvas").GetComponent<Canvas>().enabled = false;
    yield return new WaitForEndOfFrame();
    ScreenCapture.CaptureScreenshot(pathToSave);
    yield return new WaitForEndOfFrame();
    GameObject.Find("Canvas").GetComponent<Canvas>().enabled = true;

    Instantiate (blink, new Vector2(0f, 0f), Quaternion.identity);
}
IEnumerator CaptureIt()
{
字符串时间戳=System.DateTime.Now.ToString(“dd-MM-yyy-HH-MM-ss”);
字符串fileName=“屏幕截图”+时间戳+“.png”;
字符串路径保存=文件名;
GameObject.Find(“Canvas”).GetComponent().enabled=false;
返回新的WaitForEndOfFrame();
截图:截图截图(路径保存);
返回新的WaitForEndOfFrame();
GameObject.Find(“Canvas”).GetComponent().enabled=true;
实例化(闪烁,新向量2(0f,0f),四元数.identity);
}

您可以通过索引访问游戏对象的子对象

您可以使用如下代码:

GameObject.Find("Canvas").transform.GetChild(index).gameObject.SetActive(false);
索引:游戏对象的其他子对象之间的子变换索引

我对您的代码做了一些更改:

IEnumerator CaptureIt()
{
    string timeStamp = System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss");
    string fileName = "Screenshot" + timeStamp + ".png";
    string pathToSave = fileName;
    GameObject.Find("Canvas").GetComponent<Canvas>().transform.GetChild(0).gameObject.SetActive(false);
    yield return new WaitForEndOfFrame();
    Application.CaptureScreenshot(pathToSave);
    yield return new WaitForEndOfFrame();
    GameObject.Find("Canvas").GetComponent<Canvas>().transform.GetChild(0).gameObject.SetActive(true);

    Instantiate(blink, new Vector2(0f, 0f), Quaternion.identity);
}
  • 现在,在“层次结构”窗口中,选择您的按钮,在“检查器”窗口中,在“按钮组件”中单击“添加到列表”,并将您的空游戏对象设置为该值

  • 最后,选择按钮的捕获功能


  • 我希望它能帮助你

    将你正在显示/隐藏的对象作为一个参数传递进来,或者至少缓存它,然后你为什么不能只
    GameObject.Find(“Screenshot Button”)
    ?我也不使用GameObject.Find(“Screenshot”).GetComponent().enabled=false<代码>GameObject.Find(“屏幕截图”).SetActive(false)当我使用SetActive时,屏幕截图不起作用,它只是隐藏了按钮,没有返回。也许我需要enabled=false。我在答案中添加了一个示例代码。它对我有用。希望对你有帮助:)是的,我试过了,我得到了截图按钮,但它隐藏了按钮,没有截图。正如您所看到的,有一个WaitForEndOfFrame,我想我需要的是enabled=false,因为我正在访问一个组件。setActive在应用程序中也无法正常工作。CaptureScreenshot(pathToSave);对我来说不起作用是因为这个吗?你是否在你的按钮上添加了这个脚本,并想在点击按钮时隐藏这个按钮,然后捕获并最终再次显示?和
    screenscapture.CaptureScreenshot(路径保存)适合你吗?
    
    public void Capture()
    {
         StartCoroutine(CaptureIt());
    }
    
    IEnumerator CaptureIt()
    {
        string timeStamp = System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss");
        string fileName = "Screenshot" + timeStamp + ".png";
        string pathToSave = fileName;
        GameObject.Find("Canvas").GetComponent<Canvas>().transform.GetChild(0).gameObject.SetActive(false);
        yield return new WaitForEndOfFrame();
        Application.CaptureScreenshot(pathToSave);
        yield return new WaitForEndOfFrame();
        GameObject.Find("Canvas").GetComponent<Canvas>().transform.GetChild(0).gameObject.SetActive(true);
    
        Instantiate(blink, new Vector2(0f, 0f), Quaternion.identity);
    }