- Home /
How to call sprite at run time
In my game i need to call sprite at run time . For example i have a sprite sheet of progress bar and progress bar sprite is sliced into 11 slice. How to call each slice at run time
Answer by wijesijp · Mar 20, 2014 at 07:06 AM
Create an anim tree with the sprites. Each animation is a level in the progress bar. You should have animations for each of the levels in the progress bar. To display any level in progress bar all you have to do is play the correct animation.
But why not put a white sprite above this blue progress bar and scale it? You will have very smooth control that way ...
Answer by wildex999 · Mar 19, 2014 at 12:40 PM
I would recommend you set it up as a spritesheet in Unity(Set sprite to multiple and slice it) and then use an animation where you manually progress it to the next frame when you need it to.
Check out: http://michaelcummings.net/mathoms/creating-2d-animated-sprites-using-unity-4.3
Thank you for answer .Nice tutorial , that gives idea about how to make sprite ,and sprite animations.
Anyone looking for sprite animation , please refer above link
Answer by trololo · Mar 19, 2014 at 03:25 PM
Hi, you can just display a certain bar depending of time:
private IEnumerator ProgressBar(float time = 2f)
{
float startingTime = Time.time;
float elapsedTime = Time.time;
float stopTime = 1f / time;
int barID = 0;
while(barID < 10)
{
if((elapsedTime - startingTime) >= 1)
{
barID++;
startingTime = elapsedTime;
}
elapsedTime += Time.fixedDeltaTime * stopTime;
yield return null;
}
}
Where "barID" is the id in your array or in the spritesheet of the bar you want to display :)