• 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 FreeTimeDev · Nov 01, 2014 at 05:38 PM · texturetexture2druntimealpha

Difficulty loading alpha textures at runtime

I'm using the following excerpt to load an image from a file at run time:

 if(detail != customOld)
             {
                 print ("It is a custom detail to build");
                 Texture2D tex = new Texture2D(1,1, TextureFormat.ARGB32, false);
                 byte[] bytes = System.IO.File.ReadAllBytes(dir+detail+".png");
                 print (dir+detail+".png");
 
                 tex.LoadImage(bytes);
                 tex.wrapMode = TextureWrapMode.Clamp;
                 tex.Compress (true);
 
                 customDetail.GetComponent<SpriteRenderer>().sprite = Sprite.Create(tex, new Rect(0f,0f,128f,128f),new Vector2(.5f,.5f),256f);
                 customDetail.name = detail;
 
                 customDetail.GetComponent<Detail>().detailName = detail;
                 customOld = detail;
 
                 
 
                 
                 
             }
             detailToBuild = customDetail;

It works, except seemingly when the pictures have alpha. After I changed the texture being loaded to one that was solid the texture loaded as expected. After I changed the original texture to have no transparency the result was a white square. However, the texture that did load correctly shows all of the detail.

What gives?

Edit:

alt text White background -- shows White square.

alt text Transparent background -- shows nothing.

wellwhite.png (9.3 kB)
welltrans.png (10.7 kB)
Comment
Add comment · Show 2
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 Nebukam · Nov 02, 2014 at 05:10 AM 0
Share

Try using WWW.LoadImageIntoTexture to load your texture (use DXT5 ins$$anonymous$$d of DXT1 from the the provided example) ins$$anonymous$$d of System.IO, and see if it solves your issue.

avatar image FreeTimeDev · Nov 02, 2014 at 06:56 PM 0
Share
 tex = new Texture2D(128,128, TextureFormat.DXT5, false);
         var www = new WWW(dir+"/"+myTile+".png");
         yield return www;
         www.LoadImageIntoTexture(tex);

Had the same results. I've also tried doing what's below because I thought maybe mipmaps had something to do with it.

 Texture2D tex = new Texture2D(128,128, TextureFormat.DXT5, false);                                byte[] img = System.IO.File.ReadAllBytes(dir+"/"+myTile+".png");                                Debug.Log(dir+"/"+myTile+".png");                                tex.LoadImage(img);                                tex.ReadPixels(new Rect(0,0,tex.width,tex.height), 0,0);                                tex.wrap$$anonymous$$ode = TextureWrap$$anonymous$$ode.Clamp;                                tex.Compress (true);
                                 tex.Apply();

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.

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

27 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

Related Questions

Create a custom texture from code? 1 Answer

Changing the alpha channel of a sprite using the new Unity sprite system 1 Answer

Changing Part Of Texture/Material 1 Answer

Changing the alpha on a texture 1 Answer

Raycast project to texture at runtime 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