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 /
This question was closed Dec 08, 2015 at 02:03 PM by whaleinthesea for the following reason:

Nobody answers my question :/

avatar image
0
Question by whaleinthesea · Dec 06, 2015 at 06:10 PM · c#uislowprofiling

Unity UI is very slow?!?

For my game I have a health bar above the enemies made with UI panels. But when I enable this, my fps goes from 1000fps to 100-200fps(see the image). What is the reason, and how can I fix this? profiler

This is my code to create a new health bar from prefab and keep it above the enemy(note that some variables are set from other scripts):

 public RectTransform healthBar;
 public RectTransform healthBarChild;
 private int curHealth;
 private int maxHealth;
 private float maxScale;
 private Vector3 offset;
 private Transform canvas;
 
 void Start()
 {
     canvas = Global.canvas;
     healthBar = Instantiate(healthBarPrefab).GetComponent<RectTransform>();
     healthBarChild = healthBar.GetChild(0).GetComponent<RectTransform>();
     maxScale = healthBarChild.sizeDelta.x;
     healthBar.SetParent(canvas);
 }
 
 void Update()
 {
     healthBar.position = Camera.main.WorldToScreenPoint(transform.position+ offset);
 }
 
 void onDamage(int damage) // is called when enemy gets damage
 {
     curHealth -= damage;
     Vector2 curSize = healthBarChild.sizeDelta;
     curSize.x = curHealth / (float)maxHealth * maxScale;
     healthBarChild.sizeDelta = curSize;
     if (curHealth <= 0)
     {
         die();//I didn't include this(and some other functions) as it is not necessary
     }
 }
ui.png (83.9 kB)
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

  • Sort: 
avatar image
1

Answer by OncaLupe · Dec 06, 2015 at 08:16 PM

Are you updating the UI every frame? If you are, you should only update it when values change. Also, if you're using GetComponent every time, store the references and use those in updating.

Pasting your code would help to see if there is anything that could be done.

Comment
Add comment · Show 1 · 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 whaleinthesea · Dec 07, 2015 at 07:09 PM 0
Share

I paste some code, but I am not using GetComponent every frame. And I need to change the position every frame, because the enemies are constant moving and the health bar should follow them

Follow this Question

Answers Answers and Comments

43 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 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 avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity UI slow?(WaitingForJob and Canvas.RenderOverlays related problem) 1 Answer

script runs fine on start up, but slows on second attempt 1 Answer

Request feedback for C# script (instantiating UI elements depending on Player Input) 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