Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Therazerproject · Mar 29, 2017 at 02:47 PM · 2d

How to save a texture2d into a PNG?

I am using a Perlin noise based map generator for my game and I found a texture that I would like to export is as a PNG ![alt text][1]to make some more refined edits. If you were wondering why I am not using the one you see, it's because I need a very precise image, down to the pixel. Bellow you can find my code and thanks in advance:


using UnityEngine; using System.Collections;

public static class TextureGenerator {

 public static Texture2D TextureFromColourMap(Color[] colourMap, int width, int height) {
     Texture2D texture = new Texture2D (width, height);
     texture.filterMode = FilterMode.Point;
     texture.wrapMode = TextureWrapMode.Clamp;
     texture.SetPixels (colourMap);
     texture.Apply ();
     return texture;
 }


 public static Texture2D TextureFromHeightMap(float[,] heightMap) {
     int width = heightMap.GetLength (0);
     int height = heightMap.GetLength (1);

     Color[] colourMap = new Color[width * height];
     for (int y = 0; y < height; y++) {
         for (int x = 0; x < width; x++) {
             colourMap [y * width + x] = Color.Lerp (Color.black, Color.white, heightMap [x, y]);
         }
     }

     return TextureFromColourMap (colourMap, width, height);
 }

}

------ Not code----- [1]: /storage/temp/90671-capture.png

capture.png (42.0 kB)
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

5 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by Dave-Carlile · Mar 29, 2017 at 03:22 PM

Texture2D.EncodeToPNG will convert the texture into PNG data and returns an array of bytes. You can then use File.WriteAllBytes to save the byte array to a file.

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
5

Answer by naufalazzmy · Nov 14, 2018 at 06:10 AM

beacuse you want to save a PNG format, first, check if your texture has transparancy or not, because texture have many different format , for your case, try this code

  //first Make sure you're using RGB24 as your texture format
         Texture2D texture = new Texture2D(width, height, TextureFormat.RGB24, false);
          
      //then Save To Disk as PNG
      byte[] bytes = texture.EncodeToPNG();
                  var dirPath = Application.dataPath + "/../SaveImages/";
                  if(!Directory.Exists(dirPath)) {
                      Directory.CreateDirectory(dirPath);
                  }
                  File.WriteAllBytes(dirPath + "Image" + ".png", bytes);
 


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
2

Answer by FlightOfOne · Jan 13, 2018 at 11:14 PM

This is what I came up with by building upon the answer @Dave-Carlile provided.

      public static void SaveTextureAsPNG(Texture2D _texture, string _fullPath)
         {
             byte[] _bytes =_texture.EncodeToPNG();
             System.IO.File.WriteAllBytes(_fullPath, _bytes);
             Debug.Log(_bytes.Length/1024  + "Kb was saved as: " + _fullPath);
         }





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
2

Answer by Norite113 · Dec 30, 2020 at 07:09 AM

use this is working just fine i know im late :D

 private void SaveTexture(Texture2D texture)
     {
         byte[] bytes = texture.EncodeToPNG();
         var dirPath = Application.dataPath + "/RenderOutput";
         if (!System.IO.Directory.Exists(dirPath))
         {
             System.IO.Directory.CreateDirectory(dirPath);
         }
         System.IO.File.WriteAllBytes(dirPath + "/R_" + Random.Range(0, 100000) + ".png", bytes);
         Debug.Log(bytes.Length / 1024 + "Kb was saved as: " + dirPath);
 #if UNITY_EDITOR
         UnityEditor.AssetDatabase.Refresh();
 #endif
     }


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 Sir-G · Sep 12, 2020 at 01:57 AM

Not to necro a post but this code works like a treat but I was wondering if there is a way to increase the size of the PNG that is output say 250x250 and can you make it transparent.

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

9 People are following this question.

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

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

2D Animation does not start 1 Answer

Add collision to raycasted object? 2 Answers

Image not display current slot location while add new item. 0 Answers

Position gui button over 2d sprite 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