Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
7
Question by DavidDebnar · Apr 28, 2012 at 08:57 AM · savestandalonepnghdd

Saving Texture2D as a PNG image to HDD in Standalone build.

Hey guys! Do you know about any good way to encode and save a PNG image to local HDD in a Standalone build? I know it isn't possible in WebPlayer, but it hopefully is in Standalone.

  • David

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

4 Replies

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

Answer by Fabkins · Apr 28, 2012 at 02:42 PM

Here you go:

  import System.IO;
       
  function SaveTextureToFile( texture: Texture2D,fileName)
  {
     var bytes=texture.EncodeToPNG();
     var file = new File.Open(Application.dataPath + "/"+fileName,FileMode.Create);
     var binary= new BinaryWriter(file);
     binary.Write(bytes);
     file.Close();
  }

Call it using:

  var myTexture: Texture2D;
  
  function Startup()
  {
      SaveTextureToFile( myTexture,"picture.png");
  }

Enjoy :)

  • NOTE: The texture has to be readable. If its a texture that you've made should be ok but if you have imported then you need to make it readable.

Comment
Add comment · Show 6 · 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 DavidDebnar · Apr 29, 2012 at 07:25 PM 3
Share

You sir, are awesome and have all my applauds :). Thanks a lot!

avatar image artificialfool · Jan 29, 2014 at 06:11 PM 0
Share

hole in one

avatar image Andy-Block · Aug 05, 2014 at 07:59 PM 3
Share

A slightly shorter version, in case it is useful for anyone:

System.IO.File.WriteAllBytes("picture.png", myTexture.EncodeToPNG());

avatar image drudiverse · Aug 19, 2014 at 09:23 AM 2
Share
     System.IO.File.WriteAllBytes(Application.dataPath + "/"+"picture5886.png", texture2.EncodeToPNG());    //app path n1!    
avatar image danieldourado_2 · Jun 29, 2015 at 01:40 PM 0
Share

There is no: var file = new File.Open(...) It is a static method, so you call: var file = File.Open(...)

Show more comments
avatar image
1

Answer by shacharoz · Dec 07, 2020 at 10:56 PM

 string file_path = "PATH/FILENAME.PNG";
 byte[] filedata = texture2d.EncodeToPNG();
 using (var fs = new FileStream(file_path, FileMode.Create, FileAccess.Write))
 {
       fs.Write(fileData, 0, fileData.Length);
 }
 
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
avatar image
0

Answer by devilkkw · Apr 28, 2012 at 11:45 AM

 Application.CaptureScreenshot("Screenshot.png");


thath save file named Screenshot.png in a folder where u have executable. Best way is making function for multiple screenshot for don't overwrite exiting shot.and i use thath on conditional function with a keyboard press.

Comment
Add comment · Show 2 · 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 drudiverse · Jun 26, 2014 at 09:43 AM 0
Share

Thanks, interesting answer, it doesnt save a png of a texture, only of a screencap... just a note to say it didnt work with your example from the unity reference, which doesnt work, i had to do:

Application.CaptureScreenshot("Assets/savedmeshes/cappedscreens/ " + "Screenshot2.png");

you wuold have to do:

Application.CaptureScreenshot("Assets/ " + "Screenshot2.png");

avatar image drudiverse · Jun 26, 2014 at 09:55 AM 0
Share

did this too oddly engouh:

 function sshot()//:Texture2D
 { 
 // $$anonymous$$ake a new texture of the right size and
 // read the camera image into it.
 var tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
 yield  WaitForEndOfFrame();
 tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
 tex.Apply();
 var bytes = tex.EncodeToPNG();
 
 SaveTextureToFile(tex,"alcove.png");
 //Destroy (tex);
 
 //return tex;
 }
 
  
  function SaveTextureToFile( texture: Texture2D,fileName)
  {
     var bytes=texture.EncodeToPNG();
     var file = new File.Open(Application.dataPath + "/"+fileName,File$$anonymous$$ode.Create);
     var binary= new BinaryWriter(file);
     binary.Write(bytes);
     file.Close();
  }
  

avatar image
0

Answer by karun · Jan 16, 2019 at 08:27 AM

thanking you

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

8 People are following this question.

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

Related Questions

Create .png file from script, then import as asset 1 Answer

How to save a RenderTexture to a png from a different camera? 1 Answer

Best method to save a multi-dimensional array as a file? 1 Answer

Save a downloaded image 1 Answer

IsolatedStorageException: Could not find a part of the path 1 Answer


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