Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Is there a way to create a mesh UV image in Runtime?

Discussion in 'General Graphics' started by chealin, May 18, 2023.

  1. chealin

    chealin

    Joined:
    Sep 10, 2017
    Posts:
    72
    hello

    Like Editor's "UV Layout"
    I want to save UV image of Mesh in Runtime

    Is there a way ?

    upload_2023-5-18_13-36-41.png

    [Unity Editor - UV Layout] <-- Like This Image
    upload_2023-5-18_13-35-47.png
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,355
    I think you'd have to render it yourself. Use the mesh API
    https://docs.unity3d.com/ScriptReference/Mesh.html
    Loop through the triangles array in your Mesh.
    Every three ints in triangles is a vertex index, look up the uv and then use a line renderer to draw lines connecting three points. The location of the points will be based on the uvs.
    After you're done with the whole array, render all of the lines to a render texture.
    copy the render texture to a texture2d and write it out to file using Texture2D.EncodeToPNG
     
    chealin likes this.
  3. chealin

    chealin

    Joined:
    Sep 10, 2017
    Posts:
    72
    As you said, I drew and drew UV points.
    thank you!..

    Code (CSharp):
    1.  
    2.     public Texture2D ExportMeshUVLayout(Mesh mesh, int width, int height)
    3.     {
    4.         // Create a new texture to store the UV layout
    5.         Texture2D uvLayoutTexture = new Texture2D(width, height);
    6.         // Get the UVs of the mesh
    7.         Vector2[] uvs = mesh.uv;
    8.         // Scale the UVs to fit within the texture dimensions
    9.         for (int i = 0; i < uvs.Length; i++)
    10.         {
    11.             uvs[i] = new Vector2(uvs[i].x * width, uvs[i].y * height);
    12.         }
    13.         // Clear the texture to black
    14.         Color[] colors = new Color[width * height];
    15.         for (int i = 0; i < colors.Length; i++)
    16.         {
    17.             colors[i] = Color.black;
    18.         }
    19.         uvLayoutTexture.SetPixels(colors);
    20.         int[] tris = mesh.triangles;
    21.         for (int i = 0; i < tris.Length; i += 3)
    22.         {
    23.             DrawLine(uvLayoutTexture, uvs[tris[i]], uvs[tris[i + 1]]);
    24.             DrawLine(uvLayoutTexture, uvs[tris[i + 1]], uvs[tris[i + 2]]);
    25.             DrawLine(uvLayoutTexture, uvs[tris[i + 2]], uvs[tris[i]]);
    26.         }
    27.         // Apply the changes to the texture
    28.         uvLayoutTexture.Apply();
    29.        
    30.         // Save the texture as a PNG file
    31.         byte[] pngData = uvLayoutTexture.EncodeToPNG();
    32.         System.IO.File.WriteAllBytes(Application.dataPath + "/UVLayout"+gameObject.name+".png", pngData);
    33.         // Return the generated texture
    34.         return uvLayoutTexture;
    35.     }
     
    kdgalla likes this.