- Home /
Sprites warped/not sliced on android builds, fine in editor
Note: possibly related to auto slicing sprites from script - see this question and answer here > http://answers.unity3d.com/questions/1113025/batch-operation-to-slice-sprites-in-editor.html#answer-1113065
I am working on little project on android, where sprites are assigned through code when prefabs are spawned at runtime. It grabs a load of textures from resources, then it takes certain sprites and assigns them at random to a prefab thats instantiated, and animates through a spritesheet for that particular sprite. The code works like this:
private Sprite[] spriteArray;
private List<Sprite[]> spriteArrayList;
private List<Sprite> backgroundList;
spriteArrayList = new List<Sprite[]>();
backgroundList = new List<Sprite>();
for (int i = 1; i <= 125; i++) // theres 125 sprite characters!
{
spriteArray = Resources.LoadAll<Sprite>("Sprites/Sprite (" + i + ")");
spriteArrayList.Add(spriteArray);
}
for (int i = 1; i <= 178; i++) // theres 178 tiles!
{
backgroundList.Add(Resources.Load<Sprite>("Tiles/Tile (" + i + ")"));
}
And then a random selected sprite is assigned when a prefab is spawned like this:
void SpawnSprite()
{
int random = Random.Range(0, spriteArrayList.Count - 1);
float randomX = Random.Range(camTopLeft.x, camBottomRight.x); // somewhere within limits spawn the sprite
GameObject newSprite = Instantiate(SpritePrefab,
new Vector3(randomX, camTopLeft.y, -1),
Quaternion.identity) as GameObject;
SpriteAnimate newAnimate = newSprite.GetComponent<SpriteAnimate>();
SpriteMovement sM = newSprite.GetComponent<SpriteMovement>();
sM.Speed = sM.Speed * level;
newAnimate.SpriteSheet = new Sprite[spriteArrayList[random].Length];
newAnimate.SpriteSheet = spriteArrayList[random];
}
And in the editor this works no problem, however on device, specifically a Note 4, this causes graphics issues. Some sprites will show entire sheets instead of just a single sprite like intended, and some of the "splatter" sprites - which are left behind when you destroy a sprite - end up being stretched improperly. This looks like this:
I am kind of at a loss of where to start trying to fix things, but thought this must be a bug related to reassigning sprites at runtime, from resources. This is hard to diagnose considering it only happens on the device. I'll be testing in AVD here in a moment, but I'd bet the same result happens! Anybody know whats going on?
As another note, I created an editor script which takes those sprites, creates a prefab, and assigns them all to the correct sprites. This again works fine in the editor, and behaves the same on devices. Possible bug with auto slicing sprites from script? Updated question with a link to the script that does that process!