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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by tedd-hansen · Jan 03, 2015 at 12:49 PM · spritetexture2dtransparency

Converting transparent Texture2D to Sprite

I'm creating a Texture2D by snapshotting an object with a camera. Here is the gist of what I do: (The class is big, so this is just an extract to show the important parts.)

 _renderTexture = new RenderTexture(64, 64, 10, RenderTextureFormat.ARGB32);
 _camera.targetTexture = _renderTexture;
 RenderTexture.active = _renderTexture;
 Texture2D tex = new Texture2D(_renderTexture.width, _renderTexture.height);
 var texture2D = tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
 tex.Apply();

This works fine and I have verified it by applying the resulting Texture2D to a cube and it shows the texture on the cube. So then I try to convert the image to a sprite:

 var sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));_

This gives me a blank sprite (seen as transparent in the Unity editor). Applying the sprite to any UI.Image will make the image transparent. Manually applying any other sprite to the same image shows the sprite as expected.

I have noticed that when I use Texture2D.EncodeToPNG the image produced can only be seen by a few applications. For example Windows Explorer will show the icon (thumb view), but Windows Photo Viewer, Paint and Paint.Net will show the same image as a transparent image (correct image dimensions, but nothing visible). VLC however will show the image correctly.

So I'm thinking that the image has some properties that makes it transparent and that's why the sprite becomes transparent.

So my question is how do I properly convert a transparent Texture2D into a Sprite?

The transparency can be a cutout, fading is not necessary - so if required a workaround with some temp background color can be used.

EDIT: Alpha (transparency) is definitively the problem. I added a repaint without alpha before tex.Apply(); and the image shows up in the sprite as expected. This causes me to loose transparency. I will (temporarily) solve that by using a specific background color which I replace with transparency, though this is not the best way to go.

 // Between ReadPixels and Apply:
 for (int x = 0; x < tex.width; x++)
 {
     for (int y = 0; y < tex.height; y++)
     {
         var color = tex.GetPixel(x, y);
         tex.SetPixel(x, y, new Color(color.r, color.g, color.b));
     }
 }

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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why does adding a point light make my sprite transparent? How can I add light but keep the sprite opaque? 1 Answer

Can I convert a sprite to a texture2D or use the sprite on a GUI? 1 Answer

How to create Sprite programmatically 1 Answer

Sprite in Asset Bundles returned a Texture2D 3 Answers

Texture2d.ReadPixels produces a vertically stretched texture on iPad 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