- Home /
Load the sprite property of a png file from Application.persistentDataPath
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.
Answer by yummy81 · Jan 28, 2018 at 07:51 PM
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;
}
Oh my god... Thank you so much you have no idea how long this took me thank you!!
thank you. It worked for me.
I was taking a screenshot at one scene and saving it to Application.dataPath + "//Screenhot.png"
string path = Application.dataPath + "//Screenhot.png";
After that I need to call that saved image at another scene and at that time I used your solution. I called the image at that "next scene" and directly made.
SquareSprite.GetComponent().sprite = last_screenshot_save; and resize it for my usage by desired ratios of 0.2f.
SquareSprite.transform.localScale = new Vector3(0.2f, 0.2f, 0);
thank you again.
Your answer
Follow this Question
Related Questions
I need to set the path for saving the pictures with an application for Android and IOS 2 Answers
Resources.Load folders path? 2 Answers
Change a sprite from url 0 Answers
File.Delete problems with Application.CaptureScreenshot 2 Answers
Using Texture2D.GetPixels() to take a screenshot and then show it on an Image - iOS problems. 1 Answer