Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
1
Question by Julien-Lynge · Aug 23, 2011 at 05:29 PM · texture2druntimealpha

Determine if a Texture2D has alpha

I'm trying to determine if a Texture2D has an alpha channel. Currently I'm just using the shotgun method:

 bool hasAlpha = (texture.format == TextureFormat.Alpha8 || texture.format == TextureFormat.ARGB4444 || texture.format == TextureFormat.ARGB32 || texture.format == TextureFormat.DXT5 || texture.format == TextureFormat.PVRTC_RGBA2 || texture.format == TextureFormat.PVRTC_RGBA4 || texture.format == TextureFormat.ATC_RGBA8);

This, of course, is a bad way to do things. It's brittle, and will break if Unity adds other format types in later releases. It's also overkill, since the user is going to be loading the texture with a WWW call or from disk so the formats will be limited.

Is there a creative way I'm not thinking of to test if the texture has alpha or not? Perhaps some way to sample the pixels, or some property I haven't seen?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Sep 16, 2015 at 10:16 AM

When you load an image with the WWW class, you actually can only get the formats RGB24 (for jpegs) or ARGB32 (for pngs) as you can read over here: WWW.texture.

The real question is if you just want to know the source format or if the texture actually has transparent pixels. An ARGB32 image can still be opaque.

I don't think there's a way to determine the exact source format with only using the Unity API. The Unity editor supports much more file formats than the runtime. As i mentioned above it all comes down to what you need this information for. If you want to figure out if an image has transparent pixels you can simply use GetPixels and iterate through all pixels and see if the alpha of any pixel is less than 1.0. If it is the image contains alpha values. If all alpha values are 1.0 the image is fully opaque so you can simply treat it as an image without alpha values, even when it actually has some.

Here's an extension method for the Texture2D class.

 public static class Texture2D_Extension
 {
     public static bool HasAlpha(Color[] aColors)
     {
         for(int i = 0; i < aColors.Length; i++)
             if (aColors[i].a < 1f)
                 return true;
         return false;
     }
     
     public static bool HasAlpha(this Texture2D aTex)
     {
         return HasAlpha(aTex.GetPixels());
     }
 }

If you have this class somewhere in your project you can simply do:

 Texture2D texture;
 // ...
 if (texture.HasAlpha())
 {
     // texture has transparent pixels
 }


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 J.D. · Aug 23, 2011 at 08:09 PM

You could try using the TextureImporter class. Its an Editor Class (read about). It has a boolean method that returns if the source texture has alpha channel.

TextureImporter.DoesSourceTextureHaveAlpha();

The Unity documentation will teach more.

Good Luck!

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 Julien-Lynge · Aug 23, 2011 at 08:14 PM 0
Share

J.D.,

Thanks for the answer. I guess I should have specified that these textures are being loaded at runtime (this is for a data visualization tool). Still, I'll be happy to take a look at the TextureImporter class out of sheer curiosity.

avatar image mgear · Sep 13, 2015 at 07:35 PM 0
Share

For me DoesSourceTextureHaveAlpha() returns true, even if the texture format is set to RGB24..

Although the texture does contain alpha, so true is correct, but I wont be able to read the alpha if the format is wrong.. so I still need to check the actual format also?

avatar image
0

Answer by Raveler · Aug 25, 2021 at 03:27 PM

For those who look here: there is a way simpler solution: you can use GraphicsFormatUtility.HasAlphaChannel(texture.graphicsFormat) to easily see if there's an alpha channel available. GraphicsFormatUtility has a LOT of other useful methods as well.

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 Bunny83 · Aug 26, 2021 at 09:59 AM 0
Share

Well, be warned that the GraphicsFormatUtility is still in the Experimental namespace. Note that they deliberately did not generate a documentation for it. Also you should be aware of potential issues and the actual usecase this class is meant for. You can get some insight in the reply from Unity over here.


Also if you read my answer the question is what exact information are you interested in? In the past Minecraft made the mistake to just treat any terrain texture that has alpha transparently. So it was easy to just replace a normally opaque block texture with one that has transparency and you could look though the terrain. They have fixed it now. That's why you should ask yourself what exact information do you want to know and for what purpose.

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

Difficulty loading alpha textures at runtime 0 Answers

Changing Part Of Texture/Material 1 Answer

How to load an image at runtime via WWW correctly 1 Answer

Sprite alpha can't change during runtime 0 Answers

Painting stencil on a surface. 6 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