- Home /
How can i show number of lives as image during gameplay
I would like to show the lives as in the following image
I added the first life, and this is the script that I did:
public class GuiLife : MonoBehaviour {
public Texture middle;
public Texture Base;
public GameObject player;
private playerLife lifeScript;
private int life;
private int n;
void Start ()
{
lifeScript = player.GetComponent ("playerLife") as playerLife;
life = lifeScript.vita;
n = life - 1;
}
void OnGUI()
{
if (life == 1)
{
GUI.DrawTexture (new Rect(800.0f,-75.0f,100.0f,200.0f), Base,ScaleMode.ScaleToFit,true,0);
}
if (life > 1)
{
GUI.DrawTexture (new Rect(800.0f,-75.0f,100.0f,200.0f), Base,ScaleMode.ScaleToFit,true,0);
for (int i = 0, i < n , i++)
{
}
}
}
}
I wanted to check inside for how many lives I n times and add the texture to life, but the question is how do I tell him automatically wrapped the x position of the texture "middle"?
It's possible do this white the FOR or i need to place the textures for each case?
It is very likely that there is a much more efficient way to do this thing, I'm a beginner and this is the only way that came to my mind.
Right now I'm using this bad script.
void Start () { lifeScript = player.GetComponent ("playerLife") as playerLife; n = life - 1;
}
void Update ()
{
life = lifeScript.vita;
}
void OnGUI()
{
if (life == 1)
{
GUI.DrawTexture (new Rect(800.0f,-75.0f,100.0f,200.0f), Base,ScaleMode.ScaleToFit,true,0);
}
if (life == 2)
{
GUI.DrawTexture (new Rect(800.0f,-75.0f,100.0f,200.0f), Base,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(740.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
}
if (life == 3)
{
GUI.DrawTexture (new Rect(800.0f,-75.0f,100.0f,200.0f), Base,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(740.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(680.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
}
if (life == 4)
{
GUI.DrawTexture (new Rect(800.0f,-75.0f,100.0f,200.0f), Base,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(740.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(680.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(620.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
}
if (life == 5)
{
GUI.DrawTexture (new Rect(800.0f,-75.0f,100.0f,200.0f), Base,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(740.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(680.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(620.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(560.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
}
if (life == 6)
{
GUI.DrawTexture (new Rect(800.0f,-75.0f,100.0f,200.0f), Base,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(740.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(680.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(620.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(560.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(500.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
}
if (life == 7)
{
GUI.DrawTexture (new Rect(800.0f,-75.0f,100.0f,200.0f), Base,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(740.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(680.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(620.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(560.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(500.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
GUI.DrawTexture (new Rect(440.0f,-75.0f,100.0f,200.0f), middle,ScaleMode.ScaleToFit,true,0);
}
}
Thanks in advice!
[2]: /storage/temp/26981-vita_base.png
Answer by christoph_r · May 27, 2014 at 10:10 PM
Have you tried looking at while loops used with an iteration integer i?
int i = 1; //we're starting at 1 here because you draw a different texture for only one life, usually i starts at 0
GUI.DrawTexture (new Rect(800.0f,-75.0f,100.0f,200.0f), Base,ScaleMode.ScaleToFit,true,0);
while(i < life)
{
GUI.DrawTexture (new Rect(800.0f - 60 * (i - 1),-75.0f,100.0f,200.0f), Middle,ScaleMode.ScaleToFit,true,0);
i++;
}
This type of loop comes in very handy in many situations, so getting familiar with it should help you further down the road, too.
@christoph_r thanks for the help, but i have another problem, do you know how i can resize dynamicly with screen resolution? because doing so changes everything from phone to phone.
That is a bit difficult in Unity, what you might want to look at is GUIUtility.ScaleAroundPivot.
Your answer
Follow this Question
Related Questions
Texture2D not showing in build, but shows up in editor 1 Answer
How to gradient-wise fill a bitmap programmatically 1 Answer
Equivelant of GUI.DrawSprite() ? 0 Answers
How to change font size in ongui function 0 Answers
What is the best way to concatenate string to create an interaction logging console? 4 Answers