Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Nov 13, 2012 at 04:39 PM by KiraSensei for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by KiraSensei · Oct 13, 2012 at 11:50 PM · texture2d

Texture2D problem : life bar

Hello everyone !

I'm trying to make a nice life bar, but I'm having some issues to do it ...

Here are my textures : alt text

As you can imagine, the colored bar is entirely shown when my player has full life (100). My challenge is to display a fraction of the black bar on top of the first one, considering the life of the player.

Here is my non working code :

 var life:int = 100;
 Graphics.DrawTexture(new Rect((0.75*Screen.width),(0.06*Screen.height),300,50), lifeBar);
 Graphics.DrawTexture(new Rect((0.75*Screen.width),(0.06*Screen.height),300,50), deathBar, new Rect(deathBar.width*(1-life/100),deathBar.height,300,50),0,0,0,0);

The thing is I'm taking a rectangle from my second texture, not a parallelepiped like I would like to ... My hole problem is that the bars are leant. Is there an easy way to do what I want ?

The easy solution would be to create 100 textures, but it takes some time and it's heavy ...

EDIT : display of new textures. 1st one is health at 100, 2nd at 0, 3rd is the result with the previous code at life = 100, and 4th is result with life=70 and the following code :

 Graphics.DrawTexture(new Rect((0.75*Screen.width),(0.06*Screen.height),300,50), lifeBar);
 Graphics.DrawTexture(new Rect((0.75*Screen.width),(0.06*Screen.height),300*(1-life/100),50), deathBar);

This last result is wrong because of a scale problem (not the same parallels) and starts at the right (should be on the left side)

sans titre.jpg (58.8 kB)
Comment
Add comment · Show 8
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 ByteSheep · Oct 14, 2012 at 12:37 AM 0
Share

Could you clarify what exactly the problem is?
• Does your code generate any errors?
• Do both the textures get displayed?
• Does the black texture resize itself properly?
• Is the positioning correct?
A few more details would be awesome, I doubt you will have to resort to creating 100 different images ;)
P.S. Could you perhaps give us an image of what the bar currently looks like when you run the game?

avatar image Eric5h5 · Oct 14, 2012 at 01:19 AM 2
Share

http://answers.unity3d.com/questions/14770/creating-a-circular-progressbar-timer.html

avatar image KiraSensei · Oct 14, 2012 at 04:56 PM 0
Share

I understand the mechanism, but how do you create your image like that ? I already have my life bar, how can I simply add the alpha parameter to it ?

BTW thanks for the link, very useful :)

EDIT : I don't have any clue to how to do my stuff with this alpha mask thing. I understand it is what I want but I really don't have any idea how ... I created my first life bar with red and full alpha on the left and green and 0 alpha on the right. If I change my texture with the green circle of the example, it works, but with my texture, I see very little changes when I move the cursor, and everything is gone when I get to 0. It is not progressive at all ...

EDIT bis : I just checked my texture and there is a problem with my alpha parameter :(. I'm trying to correct it ...

avatar image KiraSensei · Nov 08, 2012 at 07:04 PM 0
Share

This solution force me to have a gameobject. But I would prefer to use Graphics.DrawTexture or GUI.DrawTexture. Does someone else has another idea ?

avatar image sparkzbarca · Nov 08, 2012 at 08:01 PM 0
Share

do you have a good reason for not wanting to use a game object?

the lifebar is an object of the game. aren't you going to want to maybe access that later? Shouldnt you store that?

If you have a solution that only requires you make it a game object and you have no good reason for not making it a game object.... $$anonymous$$ake it a game object.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Create Texture2D and assign image to it through a script 1 Answer

Two flat textures interfere with each other when both on z=0 2 Answers

How do you create animated textures with Texture2D.Apply with any regularity? 1 Answer

Reloading texture2d from code 1 Answer

Unlit/Transparent texture leaves dark lines on the transparent edges 1 Answer


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