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);
}