How do you draw a sprite from an atlas on to a mesh?
I want to take a sprite from a unity sprite atlas and assign it to the texture of the material my mesh is using. I found Sprite.texture but gives me the whole atlas. I just want a single sprite from that atlas
               Comment
              
 
               
              Answer by kylkrie · Mar 17, 2016 at 05:25 PM
I figured it out. I get the original UVs for the mesh, set the material texture to the atlas, and change the UVs to draw that specific sprite
     public void Init() {
         _originalUVs = _meshFilter.mesh.uv;
         _originalUVsLength = _originalUVs.Length;
     }
 
     public void SetSprite(Sprite sprite) {
         // get the rect the sprite uses on the atlas
         var spriteRect = sprite.rect;
 
          // convert to UV space (between 0 and 1)
         var x = spriteRect.x / sprite.texture.width;
         var y = spriteRect.y / sprite.texture.height;
         var w = spriteRect.width / sprite.texture.width;
         var h = spriteRect.height / sprite.texture.height;
 
          // set uvs for this sprite
         var newUVs = new Vector2[_originalUVsLength];
         for(var i = 0; i < _originalUVsLength; i++) {
             newUVs[i] = new Vector2(x + (w * _originalUVs[i].x), y + (h * _originalUVs[i].y));
         }
 
         _meshFilter.mesh.uv = newUVs;
         _material.mainTexture = sprite.texture;
     }
 
 
              Your answer
 
             Follow this Question
Related Questions
How do I use a sliced 2D sprite as the repeating texture on a 3D object? 0 Answers
Importing multiple textures to a multi-part mesh 0 Answers
material sprite dont fills mesh 0 Answers
Imported meshes colors changed to blue. How do I fix them? 2 Answers
Having trouble with a texture not showing on a generated mesh. 1 Answer