- Home /
I'm trying to create a Tiled Sprite, what am I doing wrong?
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.
Hint: I'm >>>NOT<<< talking about atlases, which now are natively supported, I'm talking about having a single sprite and displaying tiled multiple times (which might be supported in future, but currently apparently isn't)
Here's the script, can anybody help?
using UnityEngine;
using System.Collections;
public class TiledSprite : MonoBehaviour
{
public int width,height;
private SpriteRenderer renderer;
private Sprite originalSprite;
void Awake ()
{
renderer=GetComponent<SpriteRenderer>();
originalSprite=renderer.sprite;
// crea il nuovo Sprite
Texture2D target = new Texture2D(width,height);
int ow = originalSprite.texture.width;
int oh = originalSprite.texture.height;
Color32[] pixOrig = originalSprite.texture.GetPixels32();
Color32[] pixDest = new Color32[width*height];
int osize=ow*oh,dsize=width*height;
int cur=0;
while (cur<dsize)
{
for (int cur2=0; cur2<osize; cur2++)
{
pixDest[cur++]=pixOrig[cur2];
}
}
target.SetPixels32(pixDest);
target.Apply();
Sprite newSprite = Sprite.Create(target,new Rect(0,0,width,height),new Vector2(width/2,height/2));
renderer.sprite=newSprite;
}
}
Hi there. I'm stuck with the exact same problem. I'm trying to do the same as you, create a tile importer, and everything works but the link to the sprite.. Did you ever figure out the problem? If you have made any headway, I would really like to know.
Link to my resent posted question http://forum.unity3d.com/threads/219939-Prefab-created-at-runtime-is-missing-sprite-reference-(original-object-is-not)
Thanks. - Henning
Answer by Lohoris2 · Jan 05, 2014 at 02:51 PM
While I still have no clue why this didn't work, I've been helped into a much more direct solution: Unity already supports this kind of thing, it's just matter of knowing how to do that.
set your image Texture Type: Texture, Wrap Mode: Repeat
create a new Material using that image, set the Shader: Particles/Alpha Blended, set the Tiling to how many times you want to repeat it
in the scene create a Quad using that Material
Not a perfect solution, especially since you have to manually set the tiling in the Material itself, but was good enough for what I was doing.
Thanks, but that was kind of the old way of doing it. I was expecting a better way in 4.3+.. So as you say, not perfect.
I found another solution that worked for me, but it relays on the spritesheet already existing.
snippet from my blog post on the subject. (link text)
had newer written an editor script for unity before, and did have a lot of problems with references to the sprites I generated in the script… They did not exsist after the script exitede! :/ After a day of googling, asking in the unity forum, installing IRC and asking in the channel #unity3d without anyone being able to help, I did figure it out myself. I had to import the sprites in unity as I normaly would, so they exsisted and would be referencable from the script, this I could do by loading them via Resources.LoadAll(name).*
Thanks for getting back with your findings :)
While this works to repeat a simple image, it doesn't work very well if you have a more complicated repeating item that needs to tile on the fly from my findings. For example a ladder doesn't tile very well with this technique (unless I'm missing something).
I dont have Texture mode to be selected from $$anonymous$$ultiBox, what can I do, I am using Unity 5
Answer by Ash-Blue · Jul 23, 2014 at 04:23 PM
This is quite easy to do with a simple script and nothing else except a sprite renderer. See my answer at the link below for more details (note it might not be live yet since I'm awaiting moderator approval).