Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how get texture uv from atlas

Discussion in '2D' started by morix_001, Nov 26, 2013.

  1. morix_001

    morix_001

    Joined:
    Dec 4, 2012
    Posts:
    4
    hi.
    i have some texture and i create atlas with Sprite packer in editor
    now i want to get each UV data in Rect in code at run-time.

    for example i have texture_1 and texture_2 and ... and texture_20 in my game and i packed these in atlas.now i want to get texture_2 uv

    thanks
     
  2. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    The sprites are stored as Rects that represent the pixels that define the sprite on the texture.
    http://docs.unity3d.com/Documentation/ScriptReference/Sprite.html

    You have to convert the pixel coords to UV values. Which is easy, just divide the coords by the dimensions of the texture.

    Code (csharp):
    1.  
    2. Rect getUVs(Sprite sprite)
    3. {
    4.   Rect UVs = sprite.rect;//It's important to note that Rect is a value type because it is a struct, so this copies the Rect.  You don't want to change the original.
    5.   UVs.x /= sprite.texture.width;
    6.   UVs.width /= sprite.texture.width;
    7.   UVs.y /= sprite.texture.height;
    8.   UVs.height /= sprite.texture.height;
    9.   return UVs;
    10. }
    11.  
    Sprite.textureRect might already contain the UVs, so check that first. Or maybe they can be calculated with textureRect and textureRectOffset. But, I don't have access to Unity at the moment to check.
     
    Last edited: Nov 26, 2013
    Havokki and Lyrcaxis like this.
  3. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    In the editor, you can access the TextureImporter of the asset to access the entire array of sprites.

    Code (csharp):
    1.  
    2. Rect[] getUVs(string path)
    3. {
    4.   Rect[] uvs;
    5.   TextureImporter importer = (TextureImporter)AssetImporter.GetAtPath(path);
    6.   uvs = new Rect[importer.spritesheet.Length];
    7.   for(int i=0;i<uvs.Length;i++)
    8.   {
    9.     uvs[i] = importer.spritesheet[i];
    10.     //change pixel coords to uvs.
    11.   }
    12.   return uvs;
    13. }
    14.  
    You only have access to TextureImporter in the editor, so you would have to store these values somewhere else from in an editor script and then access them from there at runtime.
     
  4. morix_001

    morix_001

    Joined:
    Dec 4, 2012
    Posts:
    4
    thanks.
    but when i use sprite.rect for every texture , it returns {x=0,y=0,width = ?,height = ?} and it is not the real rectangle in Atlas.
    and when i use textureRect it give me an error : UnityException: Sprite is not rectangle-packed. TextureRect is invalid.
     
  5. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Can you take a screen shot of your Texture Importing settings for me?

    And how are the Sprites arranged in the sprite atlas?
     
    Last edited: Nov 26, 2013
  6. morix_001

    morix_001

    Joined:
    Dec 4, 2012
    Posts:
    4
    $ImportSetting.jpg
    i attached image
     
  7. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    The sprite mode is Single, so the UVs are 0,0,1,1.

    Depending on the order of the quad vertices, you would set the UVs of the uvs to one of the following:
    0,0
    0,1
    1,0
    1,1

    If you have a Sprite Atlas, change the sprite mode to Multi and open the slicer. After slicing, my above code should work.
     
    Last edited: Nov 26, 2013