Modify pixels per unit of generated sprite (via script)

I just started learning Unity, so maybe I am approaching this problem from a wrong direction.

I am using 16x16 pixels wide sprites, and I set the “Pixels Per Unit” field in the Import Settings to 16 pixels. If I left it on 100 the sprites would not render correctly (they would collapse into tiny images).

From these 16x16 sprites I generate a (4x4) “map”, and above this map I’d like to draw a (3x3) grid.91943-goal.png

I try to do this by creating a Texture2D object, modifying its pixels, converting it to a Sprite object and attaching it to a Sprite Renderer.

private void InitSprite()
    {
        // columns = 3, rows = 3, cellSize = 16
        Texture2D texture = new Texture2D(columns * cellSize,
            rows * cellSize, 
            TextureFormat.ARGB32, 
            false);
            
        for (int i = 0; i < texture.width; i++)
        {
            for (int j = 0; j < texture.height; j++)
            {
                if ((i + 1) % cellSize == 0 ||
                    (j + 1) % cellSize == 0 ||
                    i == 0 || j == 0)
                {
                    texture.SetPixel(i, j, EDGE_COLOR);
                } else
                {
                    texture.SetPixel(i, j, Color.clear);
                }
            }
        }

        texture.Apply();

        Sprite sprite = Sprite.Create(texture, 
            new Rect(0, 0, texture.width, texture.height), 
            new Vector2(0.5f, 0.5f));
        
        GetComponent<SpriteRenderer>().sprite = sprite;
    }

However, since I can’t modify the sprite’s pixel per unit (which is 100 by default), it will not render in the way I want.

91944-current.png

(It doesn’t render the whole grid, nor it is in the correct position, but that’s not the question, I can figure that out)

I have been looking at ways to solve this:

Using TextureImporter? I have found examples online that derives from the AssetPostprocessor class, and overrides the OnPreprocessTexture method. But how do I hook up this class to modify every texture settings? All I found on the subject was

In a production pipeline
AssetPostprocessors should always be
placed in pre-built dll’s in the
project instead of in scripts.

Do I put the script in the Editor folder and Unity will detect that? Do I put my Editor folder in the Assets folder? Or the TextureImporter is for the textures from the Assets folder only?

I’m sorry if these are stupid questions, but I only started Unity a few days ago, and I have been researching online for hours now.

I’m open for suggestions to other approaches as well.

Hi @csirmazbendeguz,

I stumbled on this when I was looking at a similar issue, the problem is that when you’re creating your Sprite @ line 27 you need to pass the pixelsPerUnit with it like so,

Sprite sprite = Sprite.Create(texture, 
             new Rect(0, 0, texture.width, texture.height), 
             new Vector2(0.5f, 0.5f), 100 );

This will create the sprite and set it’s pixelsperunit to whatever you like, in your case 100.

Cheers,

Arc