Hello, I’m trying to access a png file called “Screenshot.png” from Application.persistentDataPath and save it’s Sprite Component to a Sprite variable. Here is my Code:
public GameObject screenshot_backround;
Sprite last_screenshot_save;
Void Start(){
ScreenCapture.CaptureScreenshot(Application.persistentDataPath + "Screenshot.png");
last_screenshot_save = (Application.persistentDataPath+ "Screenshot.png") as Sprite;
screenshot_backround.gameObject.GetComponent<SpriteRenderer>().sprite = last_screenshot_save;
}
Howw do I load my screnshot.png file from my Application.persistentDataPath as a Sprite and save it to last_screenshot_save? Hope somone can help me Thanks in advanced.
This works for me:
public GameObject screenshot_backround;
private Sprite last_screenshot_save;
private void Start()
{
string path = Application.persistentDataPath + "//Screenshot.png";
ScreenCapture.CaptureScreenshot(path);
last_screenshot_save = LoadSprite(path);
screenshot_backround.GetComponent<SpriteRenderer>().sprite = last_screenshot_save;
}
private Sprite LoadSprite(string path)
{
if (string.IsNullOrEmpty(path)) return null;
if (System.IO.File.Exists(path))
{
byte[] bytes = System.IO.File.ReadAllBytes(path);
Texture2D texture = new Texture2D(1, 1);
texture.LoadImage(bytes);
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
return sprite;
}
return null;
}
why donot work “if (System.IO.File.Exists(path))”