Updating from 2017 to 2019 and UV issues

I was hoping to upgrade my game WarPlan from Unity 2017 to 2019 but I hit a snag. I use a single png image and select the UV corners of the image to choose the unit on the map. In 2017 it works nicely In 2019 I get this slant.

I have searched the internet and I can’t find a solution after 75m. The strange part is that the background map uses the same UV shifting method and as you can see it works fine except it is for a hexagon mesh. The counters are a quad object. I’ve been slowly transforming the objects and code as I get better with Unity to more effective methods. You can buy WarPlan at Matrix Games or on Steam.

void ChangeUV(int tileTypeX, int tileTypeY) {

        Renderer r = GetComponent<Renderer>();
        r.material.mainTexture = images[1];

        int textureWidth = r.material.mainTexture.width;
        int textureHeight = r.material.mainTexture.height;
        int tileColumns = textureWidth / MapManager.TileWidth;
        int tileRows = textureHeight / MapManager.TileHeight;
      
        float U0 = (float)(tileTypeX - 1) / tileColumns;
        float V0 = (float)(tileRows - tileTypeY) / tileRows;
        float U1 = (float)(tileTypeX) / tileColumns;
        float V1 = (float)(tileRows - tileTypeY + 1) / tileRows;
        float U2 = (float)(tileTypeX) / tileColumns;
        float V2 = (float)(tileRows - tileTypeY) / tileRows;
        float U3 = (float)(tileTypeX - 1) / tileColumns;
        float V3 = (float)(tileRows - tileTypeY + 1) / tileRows;

        Mesh mesh = GetComponent<MeshFilter>().mesh;
        Vector3[] vertices = mesh.vertices;
        Vector2[] uvs = new Vector2[vertices.Length];
        uvs = mesh.uv;

        uvs[1] = new Vector2(U0, V0);
        uvs[0] = new Vector2(U1, V1);
        uvs[2] = new Vector2(U2, V2);
        uvs[3] = new Vector2(U3, V3);

        mesh.uv = uvs;
    }

So I finally figured this out on my own. Nowhere could I find an answer that said things were changed.

Unity 2017 and Unity 2019 seem to look at the quad UVs in a different way. I don’t know why they changed this but they did. Maybe I am mistaken and it is a setting. But I tried everything and couldn’t find my answer.

Eventually I just started from scratch with an experiment in Unity 2019 building a UV quad based on Unity’s quad information. Then I could see the differences. When the new procedures were put in Unity 2017 version of the game the units came back the mess above.

This article shows the UV positions for a quad in Unity 2019.

This is the code change I did. It uses a large sprite where the counters are each 90 x 76 tile size.
0, 0 on the sprite is the lower left corner in Unity.

    //**************************************************
    //Changes the UV of a specific tile in the texture
    //                kind (X), owner (Y) Unity reads lower left of the image for UV
    void ChangeUV(int tileTypeX, int tileTypeY) {
        Renderer r = GetComponent<Renderer>();

        float tileColumns = r.material.mainTexture.width / MapManager.TileWidth;
        float tileRows = r.material.mainTexture.height / MapManager.TileHeight;

        Mesh mesh = GetComponent<MeshFilter>().mesh;
        Vector2[] uvs = new Vector2[mesh.vertices.Length];

        float tileUVSizeVertical = 1f / tileRows;
        float tileUVSizeHorizontal = 1f / tileColumns;
        float UVLeft = (tileTypeX - 1f) / tileColumns;
        float UVRight = UVLeft + tileUVSizeHorizontal;
        float UVBottom = (tileRows - tileTypeY) / tileRows;
        float UVTop = UVBottom + tileUVSizeVertical;

        uvs[0] = new Vector2(UVLeft, UVBottom);
        uvs[2] = new Vector2(UVLeft, UVTop);
        uvs[1] = new Vector2(UVRight, UVBottom);
        uvs[3] = new Vector2(UVRight, UVTop);

        mesh.uv = uvs;
    }
1 Like