Reputation: 97
I am making a screenshot script and when I take shot the whole canvas is being hidden. I just want to hide the camera button which is the child of the canvas and show it again after taking the screenshot. Here is my code.
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);
}
Upvotes: 0
Views: 8153
Reputation: 1228
You can access the child of a game object by its index.
You can use a code like this:
GameObject.Find("Canvas").transform.GetChild(index).gameObject.SetActive(false);
index: Index of the child transform between other children of a game object.
I do some changes in your code:
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);
}
And you can use the function with this code StartCoroutine(CaptureIt());
It's a general answer.
But especially for your question, follow these steps:
Click on this empty game object and in Inspector, click on Add Component and create a new C# script
In the script, create 2 functions as below:
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);
}
Now, in Hierarchy window, select your button and in Inspector window, in Button component click on Add to List and set your empty game object to this.
Finally, select Capture function to the button
I hope it helps you
Upvotes: 1