As I wrote last I've gotten the sprite animation somewhat working and I've even found a good way of making the sprite sheet. But I still haven't found the trick to getting the quality of the images right :(
So to start with the quality seems fine almost anyway as you can see here:
But after a while of running the application the graphics get blurred as you can see in this second screenshot. I have no idea why.
If anyone out there has an idea as to what I'm doing wrong then please comment.
May we see some code?
ReplyDeleteMy initial guess would be that you either play the animation again and again on top of itself (instantiates a new animation in each loop) or that you are filling some sort of graphical buffer, which should be emptied.
But it's wild guessing, as those images are not much to go by :)
This is the code for the sprite sheet:
ReplyDelete----
using UnityEngine;
using System.Collections;
public class Animation_sprite_sheet : MonoBehaviour {
//vars for the whole sheet
public int colCount = 6;
public int rowCount = 6;
//vars for animation
public int rowNumber = 0; //Zero Indexed
public int colNumber = 0; //Zero Indexed
public int totalCells = 36;
public int fps = 10;
Vector2 offset; //Maybe this should be a private var
// Update is called once per frame
void Update () {
SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);
}
//SetSpriteAnimation
public void SetSpriteAnimation( int colCount, int rowCount, int rowNumber, int colNumber, int totalCells, int fps){
// Calculate index
float index = Time.time * fps;
// Repeat when exhausting all cells
index = index % totalCells;
// Size of every cell
Vector2 size = new Vector2 ((1.0f / colCount), (1.0f / rowCount));
// split into horizontal and vertical index
float uIndex = index % colCount;
float vIndex = index / colCount;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
Vector2 offset = new Vector2 ((uIndex+colNumber) * size.x, ((1.0f - size.y) - (vIndex+rowNumber) * size.y));
renderer.material.SetTextureOffset ("_MainTex", offset);
renderer.material.SetTextureScale ("_MainTex", size);
}
}
---
I was thinking that I might be instantiating stuff but as far as I know I'm not doing that. What do you think?
Hmm...
ReplyDeleteHave you tried with only one kid animated? See if less animations equals longer time before the blurring. If this is the case, your code might simply be too memory-intensive (or something similar).
Your code is almost the same as the one on the wiki: http://www.unifycommunity.com/wiki/index.php?title=Animating_Tiled_texture
Although it looks like you are defining "offset" twice. What is the first one for? (the one you have commented on as a candidate for being private)
Also, you might want to change your float-variables to be type int-variables instead (int index = (int)(Time.time % fps);) (assuming that the Vector2 constructor accepts integers and assuming I'm not missing something vital)
You might get some ideas from this guy: http://www.rebelplanetcreations.com/downloads/Other/Tutorials/HowToMakeAGameInUnity3D.pdf
Especially page 13-14 looks interesting.
Thanks Steven, I'll start reading.
ReplyDeleteNo luck?
ReplyDeleteMust confess I haven't done much yet.
ReplyDeleteBut I came across somewhere on the net where it said it's bad to change material during runtime when it comes to the iphone. So now I wonder whether animating sprite sheets can be considered changing material during runtime? And if so should I then rather make a bunch of "pre-rendered" animation I play at run time instead?