• 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 RealMTG · Mar 05, 2017 at 10:23 AM · texture2dalphatransparentpixels

Check if every pixel of a texture is transparent

Hello

I am currently trying to check if a texture is fully transparent. Basically, check if there's absolutely nothing on the texture. I am generating these textures at runtime and it can happen that there's nothing on the texture because of the lack of renderers on an object, and if there's nothing there, I want to take care of it in some other way. But I don't have any idea how I could check every pixel of a texture to see if it's 100% transparent.

Any help with this would be greatly appreciated!

Thanks

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

1 Reply

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

Answer by AurimasBlazulionis · Mar 05, 2017 at 10:58 AM

just do a for loop:

 bool IsTransparent(Texture2D tex) {
     for (int x = 0; x < tex.width; x++)
         for (int y = 0; y < tex.height; y++)
             if (tex.GetPixel(x, y).a != 0)
                 return false;
     return true;
 }

If you also need to check if all color values are 0, then just change the if statement to if (tex.GetPixel(x, y).Equals(new Color(0,0,0,0))). Sure, you could define the color 0 0 0 0 as a variable and use it so no stack/heap allocation occurs.

Comment
Add comment · Show 3 · 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 RealMTG · Mar 05, 2017 at 11:03 AM 0
Share

Thank you for your help! It worked perfectly! I honestly didn't have a clue about GetPixel. I must have missed it both while testing in Unity and when checking the docs. :S

avatar image austinborden · Apr 20, 2018 at 07:27 PM 1
Share

Ins$$anonymous$$d of calling GetPixel in a nested for loop, call GetPixels or GetPixels32 before the loop and reference that array of pixels for better performance.

avatar image Bunny83 austinborden · Apr 20, 2018 at 08:06 PM 1
Share

It does not only increase the performance dramatically but also simplifies the iteration since you just have one big array you have to deal with. I'm even wondering since he's generating the texture at runtime he should already have such an array when generating the texture.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

68 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

Related Questions

Slowly fades from opaque to alpha 2 Answers

Remove white edges on texture help 2 Answers

Unity 3.5 New texture2D not transparent 2 Answers

Is it possible to determine a color of a pixel under if that pixel is under a game object? 1 Answer

GUI.DrawTexture - Slightly Transparent? 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