How to modify the pixels of a texture on import in the editor?

Hi,
I was wondering if it’s possible to modify the pixels of a texture on import in the editor,
not the source image, but rather the compressed file that is generated from it when it is imported in Unity.
(So that the source texture remains unchanged, but the one used in Unity is always modified)

I want to change the actual pixels, like how a normal map is created from a grayscale map, but using my own calculations. (but dooing something entirely different, the normal map generation is just an example)

I thought I would find my answer here or here, but no luck

Anyone who can help?

Also, the titel means: “How to modify the pixels of a texture, on import in the editor?” and not “How to modify the pixels of a texture on import, in the editor?”

Click the asset you’ve importet. If it’s a sprite, in the inspector you will see some options. There you can change from “compressed” to “16bit” or “True color”. You can also change the filtering between point, bilinear, and trilinear, if I’m not mistaken.
Also be sure the “max size” is greater than the size of your asset.

That’s clearly not what I’m asking.

You already mentioned the AssetPostprocessor and TextureImporter. These are two classes you will need. You should also look at the Texture2D class, specifically the get an set pixels functions.

Note: Not tested!

using UnityEngine;
using UnityEditor;
     
public class TexturePostprocessor : AssetPostprocessor
{
    public void OnPostprocessTexture(Texture2D t)
    {
        Color[] pixelArray = t.GetPixels();
        // Do something with the pixelArray...
        t.SetPixels(pixelArray);
    }
}

EDIT: Loot at the example on the OnPostProcessor(Texture2D) script reference for an example.
EDIT2: It looks like if you have MipMapping on, then you’ll need to adjust the pixels for each MipMapped texture. The example shows two ways of doing that.
EDIT3: Any script with “using UnityEditor;” should be put in an “Editor” folder somewhere. Doesn’t matter where.

I already know about the get and set Pixels functions,
but I was under the impression that there s more that needs to be done to make it work.

I mean, just getting the texture and modifying the pixels only lasts untill you restart unity, right?
and I’m not even sure it would keep these changes when making a build.

But perhaps I’m just assuming too much, and maybe it really is that simple,
I’ll test it

thanks for the reply

EDIT:
looking at at that link you provided (http://docs.unity3d.com/Documentation/ScriptReference/AssetPostprocessor.OnPostprocessTexture.html)it) it really does seem to be that simple
(silly me, always assuming it can’t be that easy)

thanks man

I’m not 100% sure how this works. I think the process goes like this.

  • Give Unity an image file
  • Unity imports the image file, and makes a separate Texture2D and saves the Texture2D to disk.
  • Unity will never look at the source file ever again. It will always use the Texture2D it put on the disk instead.
  • Unity then gives us a direct reference to the Texture2D saved to disk. Changing this Texture2D will change the Texture2D saved to disk.
  • If the original source file is modified, goto 1.

When building a project, Unity doesn’t even include the original image file. The generated Texture2D is put in the build instead.

I’ve also noticed prefabs work like this. If I have a reference to a prefab, and modify the prefab, the .prefab on disk gets marked as modified.