Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Lasutriv · Jan 20 at 05:55 PM · texturetexture2d2d sprites

2D Texture EncodeToPNG not working as expected

So my script creates a texture from a color map I've created using perlin noise. This texture is my game map. I'm wanting to save the texture as a PNG for later use. My code works for saving the PNG and I can even add the PNG onto the material but it doesn't show the texture when viewing the material in the inspector.

When I try to open the PNG from the file explorer or in the Unity file explorer to see my "game map", it's completely blank even though I can attach it to a material and see it in game. I'm assuming this is because the byte data is still there for Unity to read and display in the editor, but I'm not sure what's missing for the PNG to actually be viewable via the inspector or from a desktop image viewer.

Texture Creation:

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


Below method would be called like so: DrawTexture(TextureFromColorMap(colorMap, width, height), true);

Drawing texture and saving if desired:

 public Renderer textureRender;
 public void DrawTexture(Texture2D texture, bool saveTexture) {
         textureRender.sharedMaterial.mainTexture = texture;
         textureRender.transform.localScale = new Vector3(texture.width * 0.001f, 1, texture.height * 
                                                          0.001f);
         if (saveTexture) {
             var bytes = texture.EncodeToPNG();
             var dirPathToSave = Application.dataPath + "/Sprites/Maps/";
             string timeStamp = DateTime.Now.ToString("yyyyMMdd-HHmmssff");
 
             if (Directory.Exists(dirPathToSave)) {
                 File.WriteAllBytes(dirPathToSave + "Map-Texture-" + timeStamp + ".png", bytes);
             } else {
                 Directory.CreateDirectory(dirPathToSave);
             }
         }
     }


Thanks for any help in advance.

Comment
Add comment · Show 1
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 Lasutriv · Jan 20 at 05:33 AM 0
Share

So, it has something to do with Alpha Source. If I change the Alpha Source to "None", I can see the map in the inspector. However, the image is still completely transparent when viewing in an image viewer.

1 Reply

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

Answer by Bunny83 · Jan 20 at 07:51 PM

Well, where do your colors come from? A Color value represents an RGBA value. So what is "a" set to? Is it possible that all your color values have an alpha value of 0? We don't know what it is since your "TextureFromColorMap" method just takes a Color array and we don't know where it comes from.

Comment
Add comment · Show 1 · 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 Lasutriv · Jan 20 at 11:52 PM 0
Share

That's exactly it which makes sense as to why changing alpha transparency on the texture would then show the texture in the Unity file explorer. Attached is more information about my "regions" that contain the color arrays which are used with relation to the noise map.alt text

alpha-issue.png (108.4 kB)

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

172 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 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

Why is my texture like this? 1 Answer

Encoding metadata in a texture 1 Answer

Update Texture type to GUI during runtime 0 Answers

Splatmap and Corresponding Textures to 1 final Texture 1 Answer

Converting a RenderTexture to a Texture2D for use in a shader 2 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