How can we convert the Texture2D type to "Sprite" using the script (not in Editor)? It seems that the Texture2D retrieved from file is by default Normal Map. I need to convert the TextureType to "Sprite".

Texture2D texture = LoadPNG("D:/download.jpg");

LoadPNG method code:

 public static Texture2D LoadPNG(string filePath)

    Texture2D tex = null;
    byte[] fileData;

    if (File.Exists(filePath))
        fileData = File.ReadAllBytes(filePath);
        tex = new Texture2D(2, 2);
        tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
        Debug.Log("File Exists!");
        Debug.LogError("File not exists!");
    return tex;

It seems that this texture is set as "Default" or "Normal" Type when imported from file and then assigned to Material dynamically. As the particular shader I'm using, works best if the Texture Type is set to Sprite (I tested it by manually placing the texture file in Project and then assigning the texture to Material).

I need to change Type of Texture to Sprite before assigning it to the material. I've seen options like "AssetImporter" but it only works in Editor.

The effect of the shader (which takes two texture types

a) Inverted & Blurred Texture

b) GrayScale Texture

And then returns a Sketch Effect to the object. I convert the original image to inverted & blurred, grayscale texture using some image processing code (C#, Unity -> working around with GetPixels and SetPixels)...

Output with Defualt Texture Type The effect with "Default Texture Type"

Output with Sprite Texture Type The effect with "Sprite Texture Type"


1 Answer 1


As I'd mentioned in the comments earlier, a Sprite in Unity isn't really a "type" of texture. It's a slice of texture data.

The "Sprite (2D and UI)" import mode you see is a bit of a shortcut to both import the Texture2D texture with some default settings suitable for sprite use AND create one or more Sprite objects sliced from it, as one bundled asset (hence the expansion arrow in the Project folder).

So, we just need to figure out which of those "Sprite (2D and UI)" default settings is different from what you're getting when you load your texture by code.

tex = new Texture2D(2, 2);

This creates a texture with mipmaps by default. When you import a Sprite into Unity, it defaults to not including mipmaps. So, this is a likely source of the difference you're seeing.

Without mipmaps, when we draw the texture at less than native size, we'll tend to see aliasing artifacts - edges will look sharper than normal, and in high detail areas we might get noisy speckling. Mipmaps solve this by blurring together adjacent pixels of the texture when rendering at reduced size, but for your case that doesn't seem to be the effect you want. In your second image, slight mipmap aliasing could be what's giving you sharper detail and more speckling in the hair.

So, try loading your image without mipmaps:

public static Texture2D LoadPNG(string filePath, bool generateMipMaps = false)

// ... 

    tex = new Texture2D(2, 2, TextureFormat.RGBA32, generateMipMaps);

Does that make the dynamically loaded texture behave more like your Sprite asset?

Just beware that turning off mipmaps can produce some very distracting artifacts if you're scaling the image down significantly, or if it's going to move or be displayed in perspective. You might instead want to adjust your image filtering code to give it a sharper response even when working on mipmap filtered input.

  • \$\begingroup\$ Yes... It just gave the desired output... In fact a bit more detailed.. (just a bit) \$\endgroup\$ May 25, 2019 at 20:09
  • \$\begingroup\$ Alright... I'll try to work on the improvement of filtering code if there are some notable distractions. Thanks :) \$\endgroup\$ May 25, 2019 at 20:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .