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 Get source texture of packed sprite

Discussion in 'Scripting' started by shlakva, Oct 26, 2022.

  1. shlakva

    shlakva

    Joined:
    Oct 15, 2017
    Posts:
    15
    Hello,

    That is how we can get source texture of packed sprite[Documentation]:

    Code (CSharp):
    1. public static Texture2D SpriteUtility.GetSpriteTexture(Sprite sprite, bool getAtlasData);
    However, in this way you cannot build your game due to SpriteUtility belongs to UnityEditor namespace.
    Is there a way to achieve the same result but not in UnityEditor namespace?
     
    AldeRoberge likes this.
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Sprite.texture
     
  3. shlakva

    shlakva

    Joined:
    Oct 15, 2017
    Posts:
    15
    @orionsyndrome No. First line in documentation:
    Get the reference to the used texture. If packed this will point to the atlas, if not packed will point to the source sprite.
     
    AldeRoberge likes this.
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    I'm sorry, but the in-game runtime texture can contain only atlases. What would be the purpose of an atlas, if game had access to both the atlas AND the source textures? Can you appreciate the point of an atlas in the first place?
     
  5. shlakva

    shlakva

    Joined:
    Oct 15, 2017
    Posts:
    15
    Hmm... If Sprite have access only to atlas texture then how it get it's sprite? There should be a mechanism behind it that I can copy, no?
     
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    It got its sprite from the importer, then the atlas is made, then the runtime has access to this atlas.

    Atlases are packed to a) reduce the amount of textures / optimize memory consumption, and thus b) reduce draw calls / improve performance / improve compression rate etc. If you keep the original sprites lingering around, there are no benefits, especially with many sprites.

    But maybe there is some runtime utility I don't know about. There is DataUtility for example.
    I just thought I should offer the most obvious answer, in case you missed it somehow, because there is no way you get the reference to the original sprite texture, that would defeat the point of an atlas entirely.
     
  7. shlakva

    shlakva

    Joined:
    Oct 15, 2017
    Posts:
    15
    Thank you for answers, looks like you was right. For now it doesn't make sense to get texture if atlas exists.
     
    orionsyndrome likes this.
  8. AldeRoberge

    AldeRoberge

    Joined:
    Jun 23, 2017
    Posts:
    48
    I have the same problem. I am actively looking for a solution.

    Edit : https://answers.unity.com/questions...atlas.html?childToView=1937434#answer-1937434
    Same question, no answer.

    Edit : Ractangle

    Edit : Okay, long story short :

    Make sure you DISABLE 'TightPacking' in the Sprite Atlas and ENABLE 'Read/Write'

    Code (CSharp):
    1.             // Get the sprite in the atlas
    2.             Sprite sprite = _item.Sprite;
    3.  
    4.             // Get the texture of the atlas
    5.             Texture2D texture = sprite.texture;
    6.  
    7.             // Get the texture of the sprite in the atlas
    8.             Texture2D spriteTexture = new Texture2D((int)sprite.textureRect.width, (int)sprite.textureRect.height);
    9.             spriteTexture.SetPixels(texture.GetPixels((int)sprite.textureRect.x, (int)sprite.textureRect.y, (int)sprite.textureRect.width, (int)sprite.textureRect.height));
    10.             spriteTexture.Apply();

     
    Last edited: Nov 4, 2023 at 6:41 AM