- Home /
Image distorts when created in C#
I need to load in images at run-time, doing something similar to this:
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class SpriteCreator : MonoBehaviour {
// public string textureFilePath => Application.persistentDataPath + FileName;
void Start () {
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(File.ReadAllBytes(Application.dataPath + "/Logo_Dominoes.png"));
GetComponent<Image>().sprite = Sprite.Create(texture,
new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}
}
But some of my test images become distorted with this code. This distortion doesn't happen with all of my test files. I am attaching one of the test files that gets distorted, but I do not believe there is any issue with the test files, as they appear OK when I import them in the Unity Editor.
An Image with the sprite assigned through the editor (top) appears fine, but an Image with the sprite assigned at run-time (bottom & selected in inspector) has a subtle distortion around the words "Card Game Simulator" and "Dominoes":
Why is this subtle distortion appearing?
Answer by mossflower · Dec 29, 2017 at 04:51 AM
Since 'Alpha is Transparency' is only available in the Unity Editor and not at run-time, the solution is to do what that setting would do. One option is to edit the images beforehand. But this option doesn't really work for me, since that would put extra work on the people creating these images for me. Luckily, there is a paid solution in the asset store.
Answer by AggroBird · Dec 29, 2017 at 02:01 AM
The texture that the generated sprite is added to has the 'Alpha Is Transparency' setting set to false, which causes these artifacts. You can verify this by disabling the setting on your in-editor texture, you will see the same artifacts on both images.
Sprites that are generated through Sprite.Create() appear to have this setting off, and I couldn't find a way to enable the setting on the internal texture.
Like you said, the same thing happens if I turn off 'Alpha Is Transparency' in the Unity Editor. I tried doing texture.alphaIsTransparency = true; in my code, but that setting does seem to be ignored in code. Is this a bug? Is there any workaround?
The alphaIsTransparency is a strange editor-only variable on Unity's Texture2D class (it is missing from the documentation too I think).
When you view the generated sprite texture in the inspector, it shows that it only has three properties: Wrap mode, Filter mode and Aniso level. I guess the other properties are being ignored or don't work correctly.
Yeah, I found this: https://issuetracker.unity3d.com/issues/texture2d-dot-alphaistransparency-causes-build-to-fail Guess I know where my votes are going.
Answer by Taylor-Libonati · Dec 28, 2017 at 05:45 PM
I am not sure exactly why this would happen but it seems like the Texture2D's format is different then in the editor. When you import things in the unity editor it comes in with a bunch of default settings. You probably need to have those same settings with the dynamically loaded texture. Maybe try setting the textures filterMode before creating the sprite?
texture.filterMode = FilterMode.Bilinear; //try all three filter modes. see if you notice a diference
Filter modes didn't fix it. I went further and looked at all the import settings used in the editor and applied them all in code. Still the same.
Next, I tried loading the exact same texture in the exact same way, but I applied the texture to a material on a quad ins$$anonymous$$d of a sprite: GetComponent(Renderer)().material.mainTexture = texture; And the texture on the quad looks good! I guess it is some issue with Sprite.Create()? But I have no idea how to proceed from here.
That is really strange. I suggest filing a bug report. I am not really sure either. Sorry friend!