Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question How to change the UV tiling in a unity script

Discussion in 'Testing & Automation' started by unity_E5793FB0862C79340753, Mar 14, 2023.

  1. unity_E5793FB0862C79340753

    unity_E5793FB0862C79340753

    Joined:
    Feb 18, 2023
    Posts:
    1
    I am doing a game in unity and I have made a script that I run at editor time to merge numerous ProBuilder Meshes which are all side by side (they are areas of a board) into one single ProBuilder Mesh which is the final board. However, that step alone does not change the way the texture appears, and I have a repeated texture covering each previously separated mesh instead of one texture applying to the overall newly built mesh.

    I have found a solution to that problem, which is to open the UV editor in ProBuilder's menu while the new mesh is selected, select all faces, and change the tiling values to X=0.125, and Y=0.125 in the UV Auto mode. I am now trying to reproduce that action programmatically, but I have not found a way.

    So, how is it possible to reproduce the action of changing the UV tiling by a script? Thanks a lot.

    I have stored the merged object in ProBuilderMesh combinedMesh and I tried to access its UVs with combinedMesh.GetUVs(0, uvs); as well as combinedMesh.uv;, but I did not manage from there to change those UVs to reproduce what I am currently manually doing and didn't find how to do so on the internet.

    One of the attempts I was the most hopeful for was this one and while it didn't return any error it did not change the way the texture appears on the Mesh:
    Code (CSharp):
    1. combinedMesh.GetUVs(0, uvs);
    2.  
    3. for(int i=0; i<uvs.Count; i++)
    4. {
    5.       uvs[i] = uvs[i] * 0.125f;
    6. }
    7.  
    8. combinedMesh.SetUVs(0, uvs);
    9. combinedMesh.RefreshUV(combinedMesh.faces);
     
    Last edited: Mar 15, 2023