• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by talyon · May 27, 2014 at 08:52 PM · c#guitexturelifebar

How can i show number of lives as image during gameplay

I would like to show the lives as in the following image

alt text

I added the first life, and t$$anonymous$$s 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 $$anonymous$$m automatically wrapped the x position of the texture "middle"?

It's possible do t$$anonymous$$s w$$anonymous$$te 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 t$$anonymous$$s t$$anonymous$$ng, I'm a beginner and t$$anonymous$$s is the only way that came to my mind.

Right now I'm using t$$anonymous$$s 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

prova ui - copia.png (23.5 kB)
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by christoph_r · May 27, 2014 at 10:10 PM

Have you tried looking at w$$anonymous$$le 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);
 w$$anonymous$$le(i < life)
 {
         GUI.DrawTexture (new Rect(800.0f - 60 * (i - 1),-75.0f,100.0f,200.0f), Middle,ScaleMode.ScaleToFit,true,0);
         i++;
 }

T$$anonymous$$s type of loop comes in very handy in many situations, so getting familiar with it should help you further down the road, too.

Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image talyon · May 28, 2014 at 09:21 AM 0
Share

@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.

avatar image christoph_r · May 28, 2014 at 11:52 AM 0
Share

That is a bit difficult in Unity, what you might want to look at is GUIUtility.ScaleAroundPivot.

avatar image talyon · May 28, 2014 at 12:24 PM 0
Share

@christoph_r There are simpler ways?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to gradient-wise fill a bitmap programmatically 1 Answer

Equivelant of GUI.DrawSprite() ? 0 Answers

Texture2D not showing in build, but shows up in editor 1 Answer

GUITexture is lagging my object 0 Answers

How to dynamically change the text in Unity(Augmented Reality + NYARtoolkit(C#)) ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges