• 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 OrenOthnay · Aug 30, 2017 at 06:55 PM · 2dui image2dtexture

2D texture loaded from file different from same texture in editor

I've encountered a strange issue when loading a .png file as a texture from a local file.

I've place side by side two Image (UI) objects. both using (identical copies of) the same file. One loaded in the editor, and one loaded with code. And they look different. Specifically, the loaded with code one (right) has some artifacts that don't exist in the original file or the one preloaded into the editor.

The .png file contains transparency.

see Image (background color added to highlight artifacts, notice artifacts on the edges):

image showing differences

I've loaded the local .png file using the following code:

 public static Sprite LoadPNGToSprite(string filepath)
 {
     Texture2D texture = LoadPNG(filepath);
     float pixelsPerUnit = 100;
     return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0), pixelsPerUnit);
 }

 private static Texture2D LoadPNG(string filePath)
 {        
     Texture2D tex = null;
     byte[] fileData;

     if (File.Exists(filePath))
     {
         fileData = File.ReadAllBytes(filePath);
         tex = new Texture2D(2, 2, TextureFormat.DXT5, false);            
         tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.            
     }
     else
     {
         Debug.LogError("could not find file: " + filePath);
     }                

     return tex;
 }

The import settings of the editor image are as follows (if that helps):

editor import settings

I'm at a loss As I have been trying to get rid of those artifacts for a few days now. If any other information is needed feel free to ask.

diff.png (8.4 kB)
importsettings.png (37.9 kB)
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

2 Replies

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

Answer by liortal · Aug 30, 2017 at 09:03 PM

Setting the created texture to use point filtering seems to give better results:

  public static Sprite LoadPNGToSprite(string filepath)
      {
          Texture2D texture = LoadPNG(filepath);
  
          texture.alphaIsTransparency = true;
          texture.wrapMode = TextureWrapMode.Repeat;
          texture.filterMode = FilterMode.Point;
  
          float pixelsPerUnit = 100;
  
          return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0), pixelsPerUnit);
      }
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 OrenOthnay · Aug 30, 2017 at 09:32 PM 0
Share

That did it! Thanks. I still wander why the default values for those fields are different for textures imported into the editor and textures loaded by code.

avatar image liortal OrenOthnay · Aug 31, 2017 at 05:47 AM 0
Share

I guess the sprite (and texture) importers store a bunch of other information that you are not initializing by default when you create a texture and then assign it to a sprite.

Also, the LoadImage method is very limited in itself (can only support a few texture formats, etc).

So, overall, this may be a Unity bug, or just the difference between the way Unity loads textures by this API. who knows. if it's interesting we can investigate further :)

avatar image
0

Answer by rigward · Feb 15, 2018 at 08:42 PM

Faced the same issue some time ago, and TextureFormat.ARGB32 + setting mipmap to false solved it for me:

 var texture = new Texture2D(512, 512, TextureFormat.ARGB32, false);
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

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

128 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

Related Questions

How to setup repeating image for 2d sprite renderer 0 Answers

2D Animation does not start 1 Answer

Unity 2D not rendering images in Canvas 1 Answer

How do I scale the Xmax value of a RectTransform 3 Answers

2D Image not appearing in game but only in scene 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