• 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
0
Question by spaceshooter · Dec 17, 2010 at 08:43 PM · guihealthbar

Efficient Health Bar

I implemented the Health Bar solution found here: http://answers.unity3d.com/questions/4456 for health bars. The problem is, once I get 4 or 5 instances of the health bars on the screen the frames per second is down a good amount.

For a single bar it might be fine, but for a larger number it doesn't appear to be viable. Any recommendations?

Edit: Found a code problem which helped... but I still wonder if there is a quicker way to draw the bars.

Comment
Add comment · Show 1
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 Statement · Dec 17, 2010 at 09:10 PM 0
Share

"the frames per second is down a good amount", could you give us a figure? Is there anything else drawing on the screen?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Loius · Dec 17, 2010 at 09:08 PM

Calling OnGUI from multiple objects might be the culprit. Try assembling a group of healthbars into an array and only having one handler object run OnGUI over those objects.

Something like this pair - Create exactly one Healthbars object and any number of LivingThing objects.

LivingThing.js

var health : float; var maxhealth : float;

function Start() { Healthbars.AddBar( this ); }

Healthbars.js

static var healthbars : Array(); static var instance : Healthbars;

function Start() {
if ( instance ) throw ("Duplicate Healthbars instance: " + gameObject.name ); instance = this; }

static function AddBar( lt : LivingThing ) { healthbars.Add( lt ); }

function OnGUI() { for ( var lt : LivingThing in healthbars ) { // display code goes here } }

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 Ejlersen · Dec 18, 2010 at 09:33 PM 0
Share

Good example, but one thing about the code makes me a bit confused. Its the "for(var lt : LivingThing in healthbars)". I only code in C# in Unity, but that looks very much like a foreach. They are quite expensive, since they have to check the type of each element in the array. But okay, that may not be a problem in javascript, however isen't there something with for-in in javascript that iterates through the array abitrarily? That might give a weird effect on the screen, if the healthbars are on top of each other.

avatar image Loius · Dec 20, 2010 at 03:28 PM 0
Share

It goes through the array in order. If speed is a problem, you can use a builtin array (square brackets), but you'll have to manually resize that yourself. If your healthbars are added in an arbitrary order (which they're pretty much guaranteed to be), and that creates problems, you'll have to arrange the healthbars somehow to not overwrite each other, either by noting what screen space is taken up by already-drawn ones and rearranging the others, or by using some other form of control. Honestly it might be better to use actual healthbar objects to display health if you worry about overlap.

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

No one has followed this question yet.

Related Questions

Scrollbar Thumb Scaling 0 Answers

How do I make a custom health bar? 0 Answers

Healthbar is over play dead Animation 1 Answer

Help!!! NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.DrawTexture 2 Answers

Help with GUI, Textures and Materials (Health Circle) 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