Search Unity

I'm trying to create a Tiled Sprite, what am I doing wrong?

Discussion in '2D' started by Lohoris2, Dec 1, 2013.

  1. Lohoris2

    Lohoris2

    Joined:
    Aug 20, 2013
    Posts:
    85
    Since apparently Unity 2D still lacks tiled sprites, I'm tyring to create a class which takes a normal Sprite and tiles it using custom dimentions.

    It doesn't crash, but doesn't yield results either: I just get an empty sprite apparently.

    The transform position is correct (unaltered), I do use Apply, and the new width is multiple of the original one. Now I'm clueless about what could be the problem.

    Here's the script, can anybody help?

    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.    
    5.     public class TiledSprite : MonoBehaviour
    6.     {
    7.         public int width,height;
    8.    
    9.         private SpriteRenderer renderer;
    10.         private Sprite originalSprite;
    11.    
    12.         void Awake ()
    13.         {
    14.             renderer=GetComponent<SpriteRenderer>();
    15.             originalSprite=renderer.sprite;
    16.    
    17.             // crea il nuovo Sprite
    18.    
    19.             Texture2D target = new Texture2D(width,height);
    20.             int ow = originalSprite.texture.width;
    21.             int oh = originalSprite.texture.height;
    22.    
    23.             Color32[] pixOrig = originalSprite.texture.GetPixels32();
    24.             Color32[] pixDest = new Color32[width*height];
    25.    
    26.             int osize=ow*oh,dsize=width*height;
    27.             int cur=0;
    28.             while (cur<dsize)
    29.             {
    30.                 for (int cur2=0; cur2<osize; cur2++)
    31.                 {
    32.                     pixDest[cur++]=pixOrig[cur2];
    33.                 }
    34.             }
    35.    
    36.             target.SetPixels32(pixDest);
    37.             target.Apply();
    38.    
    39.             Sprite newSprite = Sprite.Create(target,new Rect(0,0,width,height),new Vector2(width/2,height/2));
    40.             renderer.sprite=newSprite;
    41.         }
    42.     }
    43.  
     
  2. giyomu

    giyomu

    Joined:
    Oct 6, 2008
    Posts:
    1,094
    hmm do you need to do this at runtime ? because you can use the slice "grid" option in the sprite editor , once you set the sprite mode to multiple.
     
  3. Lohoris2

    Lohoris2

    Joined:
    Aug 20, 2013
    Posts:
    85
    This is not what I'm trying to do.

    I want to display a single sprite, tiled multiple times.