Author Topic: UIPanel.LateUpdate() make so high CPU  (Read 6731 times)

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
UIPanel.LateUpdate() make so high CPU
« on: July 23, 2014, 03:20:47 AM »
I have too many objects needed update (about over 100 objects) My objects is a health bar and they must be update position follow character move.

My code update position of health bar

  1. void LateUpdate () {
  2.                 if (gameObject.activeSelf)
  3.                 {
  4.                         pos = worldCamera.WorldToViewportPoint (Target.transform.position);
  5.                         pos = uiCamera.ViewportToWorldPoint (pos);
  6.                         pos.y += 0.35f;
  7.                         pos.z = 0f;
  8.                         transform.position = pos;      
  9.                 }
  10.         }

This make high CPU, do you have any idea to help me to deal with this issue ?



kurozael

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: UIPanel.LateUpdate() make so high CPU
« Reply #1 on: July 23, 2014, 03:41:53 AM »
One thing you could try is only updating the position when the worldCamera's position or rotation have changed.

This is just a very basic example, and doesn't include when variables like camera FOV changes.

Say your camera class is called "CameraClass", and you give it this method and variables.

  1.         public delegate void OnCamUpdate(Camera cam);
  2.         public event OnCamUpdate onCamUpdate;
  3.         private int lastPos;
  4.         private int lastRot;
  5.  
  6.         public void LateUpdate()
  7.         {
  8.             var posCode = transform.position.GetHashCode();
  9.             var rotCode = transform.rotation.GetHashCode();
  10.  
  11.             if (posCode != lastPos || rotCode != lastRot)
  12.             {
  13.                 lastPos = posCode;
  14.                 lastRot = rotCode;
  15.  
  16.                 if (onCamUpdate != null)
  17.                     onCamUpdate(camera);
  18.             }
  19.         }
  20.  

You'd then want to change the LateUpdate code in your object with the health bar, to:

  1. void OnCamUpdate (Camera cam)
  2. {
  3.         if (gameObject.activeSelf)
  4.         {
  5.             pos = cam.WorldToViewportPoint (Target.transform.position);
  6.             pos = uiCamera.ViewportToWorldPoint (pos);
  7.             pos.y += 0.35f;
  8.             pos.z = 0f;
  9.             transform.position = pos;  
  10.         }
  11. }
  12.  
  13. void Awake()
  14. {
  15.     CameraClass.onCamUpdate += OnCamUpdate;
  16. }
  17.  

This code was written in the browser, but you get the idea.

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
Re: UIPanel.LateUpdate() make so high CPU
« Reply #2 on: July 23, 2014, 12:49:51 PM »
Caching your transforms and game objects may make a noticeable difference, too.

  1. private Transform cachedTransform;
  2. private Transform cachedTargetTransform;
  3. void Start()
  4. {
  5.     cachedTransform = transform;
  6.     cachedTargetTransform = Target.transform;
  7. }
  8. void LateUpdate()
  9. {
  10.     pos = worldCamera.WorldToViewportPoint (cachedTargetTransform.position);
  11.     …
  12.     cachedTransform.position = pos;
  13. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIPanel.LateUpdate() make so high CPU
« Reply #3 on: July 23, 2014, 08:08:07 PM »
Biggest performance improvement can be achieved by moving your HUD objects onto their own panel, separating them from the rest of the UI.

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
Re: UIPanel.LateUpdate() make so high CPU
« Reply #4 on: July 30, 2014, 04:47:14 AM »
Thank for support but it doesn't solve my problem, FPS lost 10-20 when UIPanel.LateUpdate() calculate the position of 3d gameobject and the follow object 2d

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIPanel.LateUpdate() make so high CPU
« Reply #5 on: July 30, 2014, 10:31:08 AM »
UIPanel.LateUpdate() doesn't calculate 3D game object position, and doesn't follow anything. UIPanel.LateUpdate() is where geometry gets rebuilt. Geometry needs to be rebuilt whenever something changes in the UI, which is why I suggested moving your HUD elements onto their own panel, so that their changes don't affect stationary widgets.

You really should look into the HUDText extension. UIFollowTarget script there does the following logic inside, and can disable game object's children if the game object is not in view, which saves a lot of performance.

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
Re: UIPanel.LateUpdate() make so high CPU
« Reply #6 on: August 18, 2014, 11:14:38 PM »
I'm follow your suggest but seem to be not reduce FPS :( I tried 3 case (used HUDText extension run on Nexus 7 2012)

case 1: one panel and many hud object inside (around 36 hud object) just moving hud object and FPS is ~30-35

case 2: each hud object have one panel (draw call increase to 50~60) just moving panel and FPS is 33 - 38

case 3: look like case 1 but i have 4 panel for all hud object (each panel active 9 hud object, so 4x9=36 hud object inside 4 panel) FPS 30-35

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIPanel.LateUpdate() make so high CPU
« Reply #7 on: August 19, 2014, 11:04:20 AM »
If you have that many HUD panels in there, then yes I can see how it wouldn't increase performance. Mobile devices are limited by draw calls. I suggest you reduce the complexity of your UI if possible. The single panel approach will get faster the less widgets you have, and the less complicated widgets you use. For example, a label is much more expensive than a sprite. A label that uses a shadow or outline is more expensive still. Label shadow in particular is very expensive as it draws the text 5 times.

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
Re: UIPanel.LateUpdate() make so high CPU
« Reply #8 on: August 20, 2014, 10:39:43 PM »
Thank for reply, i have to do this follow design of game. I cant make it easier :( A health bar is made by 2 slider and 1 label and need ~ 20-30 health bar show in scene on run time. If use it, FPS reduce ~10-15 FPS so expensive for RGP game   

tnbao91original

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 41
    • View Profile
Re: UIPanel.LateUpdate() make so high CPU
« Reply #9 on: August 21, 2014, 04:35:27 AM »
I wanna ask you a question. My panel has 100 hud objects but at run time just has few active hud objects (~10-20 hud objects, the others are unactive) so does panel will update all of hud objects both active and unactive or just active hud objects when a active hud object changes transform ?

I want to test a last case, I will store all 100 hud objects unactive in Panel 1 and when i need to use some of them i will move hud object to Panel 2 and active it, when finish unactive them and move back to Panel 1.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIPanel.LateUpdate() make so high CPU
« Reply #10 on: August 22, 2014, 03:24:35 AM »
Disabled game objects don't exist as far as the panel is concerned. They are simply ignored.