Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
1
Question by Larry-Dietz · Apr 04, 2010 at 09:49 PM · guihealthbardrawtexture

Another GUI question. HealthBar

I created two Texture2D objects. One which contains an empty "HealthBar" image, and one that contains a full "HealthBar" image.

What I am trying to do is draw the empty bar using GUI.DrawTexture, then draw the leftmost portion of the Full texture up to the current health of the player.

The problem I am having is getting the leftmost portion of the texture. GUI.DrawTexture doesnt appear to have an overload that lets me select a source rectangle from within the texture. Graphics.DrawTexture does, but it also requires a color, Material, and borders. The results I get using it do not look very good :(

Am I going about this the wrong way? Is there a better way to accomplish this??

Thanks, -Larry

EDIT Ok, found an example elsewhere, and have figured this out for the most part. But still having a problem. See this function I created...

function DrawBar(Left:int, Top:int, BarTexture:Texture, MaxValue:float, Value:float) { var LocationRect:Rect = Rect(Left,Top,EmptybarTexture.width,EmptybarTexture.height); var DrawWidth:int;

 DrawWidth = EmptybarTexture.width*(Value/MaxValue);
 GUI.BeginGroup (LocationRect);
     GUI.DrawTexture (Rect (LocationRect), EmptybarTexture);
     GUI.BeginGroup (new Rect (Left, Top, DrawWidth, EmptybarTexture.height));
         GUI.DrawTexture(LocationRect, BarTexture);
     GUI.EndGroup ();
     GUI.Label(Rect((LocationRect.width/2)-10,Top-5,40,50),Value.ToString());
 GUI.EndGroup ();

}

This expects that there is a variable in the script named EmptybarTexture and HealthbarTexture.

You would call this with...

DrawBar(0,0,HealthbarTexture,100,50);

The problem I am having now is if I make a 2nd call with the top parameter set to 10, this 2nd bar doesn't display at all.

If anyone has any ideas, please feel free to chime in :)

-Larry

Comment
Add comment
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
0
Best Answer

Answer by Larry-Dietz · Apr 05, 2010 at 01:15 AM

Ok, figured it out.

Here is the revised function in case anyone else want's to use it :)

function DrawBar(BarLeft:int, BarTop:int, BarTexture:Texture, MaxValue:float, Value:float) { var LocationRect:Rect = Rect(BarLeft,BarTop,EmptybarTexture.width,EmptybarTexture.height); var DrawRect:Rect = Rect(0,0,EmptybarTexture.width,EmptybarTexture.height); var DrawWidth:int;

 DrawWidth = EmptybarTexture.width*(Value/MaxValue);
 GUI.BeginGroup (LocationRect);
     GUI.DrawTexture (DrawRect, EmptybarTexture);
     GUI.BeginGroup (new Rect (0, 0, DrawWidth, EmptybarTexture.height));
         GUI.DrawTexture(DrawRect, BarTexture);
     GUI.EndGroup ();
     GUI.Label(Rect((DrawRect.width/2)-10,-5,40,50),Value.ToString());
 GUI.EndGroup ();

}

The problem was that I didn't realize that the Left and Top were relative to the group created, so essentially, I was setting the limits, then attempting to draw outside of them. This function works as expected. Not sure why I had to set the top to -5 to get the label to display correctly, but it works :)

-Larry

Comment
Add comment · Show 2 · 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 DarkHype · Apr 14, 2010 at 08:04 PM 0
Share

I tried your script but it doesnt work for me it says unknown identifier EmptyBarTexture so if you could help me cuz im working on an RPG with a couple of friends!

avatar image Larry-Dietz · Apr 15, 2010 at 02:25 AM 0
Share

You need to add the following to the top of your script, then drag the textures you want to use into these objects in the inspector.

var EmptybarTexture:Texture2D; var HealthbarTexture:Texture2D;

After you have these at the top, and the textures you want to use assigned, it should work just fine.

Use the following to draw the bar... DrawBar(0,0,HealthbarTexture,100,50);

Hope this helps, -Larry

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Drawing a GUI.DrawTexture call above GUI.Button in seperate scripts 0 Answers

DrawTexture without stretching it 0 Answers

GUI.DrawTexture isn't centred. Help? 1 Answer

GUI.DrawTexture disable anti alias 1 Answer

DrawTexture on RenderTexture without Camera 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