- Home /
Unity UI slow?(WaitingForJob and Canvas.RenderOverlays related problem)
I have a game where the enemies have a health bar above their head made with Unity(5.2) UI, but if I enable the UI my game fps goes from 1000 fps to 100 fps(see image of profiler). How can I fix this?? This is (a part of) my code:
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
}
}
![Profiler][1]
Please answer, this is really important! Thanks in advance. [1]: /storage/temp/59559-ui.png
Answer by FlyingHighUp · Dec 08, 2015 at 04:53 PM
Hmmm... Your code looks fine to me. I think your issue is probably the effect of something else, probably the number of things in your UI or some other GameObject.
The only thing in your code that would cause any kind of slowdown would really be this "healthBar.position = Camera.main.WorldToScreenPoint(transform.position+ offset);" But that should still be very fast. If Camera.main does a search for the tagged main camera every frame it may be a cause. But 5ms just for that sounds a bit large.
But even when I have 5 enemies, it goes down to 100 fps. Is there another and faster way to convert a world point to a screen point?
Not that I know of. Personally I would check the profiler and see if this Update() method here is really the source of your woes, I suspect something else is the cause. $$anonymous$$aybe the internal UI drawing method itself.
Also worth noting that the vast majority of screens can only display 60fps, so it's typically not worth it to get your fps higher than that. Unless you're developing for the latest Occulus rift, which I hear can display 90fps.
I see the problem is not in the Update(), but in WaitingForJob in Canvas.RenderOverlays. I can't find an solution on the internet. Can you help me?
@FlyingHighUp 100fps on a pc is not the problem, but the problem is that my fps goes from 1000 to 100fps and I want to make the game for mobile phones and then my game will be really slow
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity UI is very slow?!? 1 Answer
Render Texture vs 3D model in screen space - camera 1 Answer