Search Unity

Filled (pattern) sprite

Discussion in '2D' started by Nikita-M, Nov 21, 2013.

  1. Nikita-M

    Nikita-M

    Joined:
    Nov 21, 2013
    Posts:
    10
    Hello!

    A few days trying to find the answer how to create filled(pattern) sprite.

    Maybe someone knows how to do it?

    Many Thanks :)
     
    Last edited: Nov 21, 2013
  2. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
  3. sotirosn

    sotirosn

    Joined:
    Jan 5, 2013
    Posts:
    24
    I just wrote this to convert a sprite to a normalized quad.

    Code (csharp):
    1.  
    2. public class SpriteTexture : MonoBehaviour {
    3.     public Sprite sprite;
    4.  
    5.     public void Awake() {
    6.         Apply();
    7.     }
    8.  
    9.     public void Apply() {
    10.         renderer.material.mainTexture = sprite.texture;
    11.         Debug.Log (sprite.texture.width + " " + sprite.texture.height);
    12.         Debug.Log (sprite.textureRect);
    13.  
    14.         renderer.material.mainTextureScale = new Vector2(
    15.             sprite.textureRect.width/sprite.texture.width,
    16.             sprite.textureRect.height/sprite.texture.height
    17.         );
    18.  
    19.         renderer.material.mainTextureOffset = new Vector2(
    20.             sprite.textureRect.x/sprite.texture.width,
    21.             sprite.textureRect.y/sprite.texture.height
    22.         );
    23.     }
    24. }
    25.  
    I wrote this to place on a 9-sliced quad MeshFilter mesh but you could use it on a regular quad. You could then tweak the mainTextureScale to do tiling. By filled pattern you mean tiled right? If you just want to stretch a sprite to fill a space you can adjust its gameobject transform directly.
     
  4. Nikita-M

    Nikita-M

    Joined:
    Nov 21, 2013
    Posts:
    10
    Thanks soooo much for help!