I need to load a png image from a screenshot then convert it to sprite and put it into an image. I have code in below but it has seen no work?
why?
string pngpath = Application.dataPath $"/Screenshot{index}.png";
Sprite sp = Resources.Load<Sprite>(pngpath);
child.GetComponent<Image>().sprite = sp;
public IEnumerator CaptureScreen()
{
// Wait till the last possible moment before screen rendering to hide the UI
yield return null;
canvan.GetComponent<Canvas>().enabled = false;
foreach (GameObject i in Ball)
{
i.GetComponent<Renderer>().enabled = false;
}
// Wait for screen rendering to complete
yield return new WaitForEndOfFrame();
// Take screenshot
ScreenCapture.CaptureScreenshot(Application.dataPath $"/Screenshot{index}.png");
// Show UI after we're done
canvan.GetComponent<Canvas>().enabled = true;
foreach (GameObject i in Ball)
{
i.GetComponent<Renderer>().enabled = true;
}
index ;
PlayerPrefs.SetInt("index", index);
}
CodePudding user response:
You are not converting the png to sprite, try this.
string pngpath = Application.dataPath $"/Screenshot{index}.png";
byte[] filedata;
filedata = System.IO.File.ReadAllBytes(pngpath);
Texture2D text = new Texture2D(2, 2);
text.LoadImage(filedata);
Rect rec = new Rect(0, 0, text.width, text.height);
Sprite sp = Sprite.Create(text, rec, new Vector2(0, 0), 0.1f);
child.GetComponent<Image>().sprite = sp;