• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by banana111 · Jan 28, 2018 at 02:34 PM · spriteloadpathscreenshot

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
14
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image banana111 · Jan 29, 2018 at 02:10 PM 0
Share

Oh my god... Thank you so much you have no idea how long this took me thank you!!

avatar image TeamDefiant · Oct 22, 2019 at 12:51 PM 0
Share

Good work, saved me a bunch of work!

avatar image shaneK001 · Aug 12, 2020 at 05:09 PM 0
Share

Thank you!

avatar image delmasli · Jan 30, 2021 at 09:18 PM 0
Share

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.

avatar image
0

Answer by mzolghadr60 · Feb 25, 2019 at 10:41 AM

why donot work "if (System.IO.File.Exists(path))"

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Resources.Load("sprite") returning nil 0 Answers

capture the screenshot to iPhone photo gallery 2 Answers

How to get Assets path and to load an asset with out AssetsDatabase? 2 Answers

AssetDatabase current folder? 2 Answers

Accessing 4.6 GUI build-in sprites in C# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges