- Home /
Insane amount of CPU power used to activate UI inventory window
Hey everyone,
I'm reposting this, because I noticed that help room basically completely inactive.
I've been having random issues lately, that never occured before, with my inventory UI. I have not touched it for legit like a month and yet sometimes opening inventory results in massive fps drop on a frame when the key to open inventory is pressed.
The whole process sometimes takes over 40 ms! Which is just insane. I don't know how this thing triggers, but when I restart unity, the spikes usually vanish for a while, but after some time of testing and working with my code the issues happen again. However even when the issue does not appear in it's 'full power', opening invenotory takes around 7-8 ms (4-5 ms to update canvas and 2-3 ms to activate Inventory and Equipment elements on the canvas)
Here on the image you can see the actual results from the profiler. The huge spikes happen when I open the Inventory (which activates game object Inventory and Equipment on my canvas)
Image with the better scenario: https://prnt.sc/25s7d35
And here is my main canvas in the hierarchy. Basically this one only has hp bar, mana bar, inventory and equipment UI. Inventory has 40 slots.
Since I can't add anymore attachments to this post, I'm sending the rest of the information via links to the images.
This image contains the structure of my inventory slot. Inventory slot parent has inventory slot script, that handles adding items into the inventory slot, updating icon, showing remove button and updating item stack size if needed.
ItemButton contains image and button component and on click script that has IPointerClickHandler to detect clicks in given interval.
RemoveButton has image and button with a function that is called in the inventory slot script on the InventorySlot object.
ItemStackSize has text and an outline.
None of the inventory uses update method or anything similar. Inventory only updates when items are added into it, when they are consumed (potions) or when they are equipped or unequipped.
These fps drops only happen when you open the inventory.
This is how my UI looks: https://prnt.sc/25s6mrn
Anyone know how to fix these issues? They just don't make any sense at all to me. I know that changing one thing at canvas forces the game to recalculate whole canvas again, but this problem only appears when I activate the inventory object. Equipment object in fact has no effect at all on the game. As you can see on the profiler image, in the worst case scenario for some reason it takes whole 12 ms to just activate game object and 28 more to rebuild the canvas. Even in the best case scenario (which is majority of the time, especially after I restart unity) the activation of UI takes 8 ms which is still insanely crazy and I just have no idea why is this happening.
Do you have a lot of items under the update() function? I know that can be CPU intensive but this UI doesn’t look like it’d be so intensive
Hello, inventory does not use anything related to update, and in fact, once it's open, even if you update item slots 100 times per frame, the game doesn't even feel it. This issue only happens when you SetActive the Inventory object. Equipment object also has no impact at all.
That’s odd. I googled some things that might cause some high cpu usage. Try adding this to your start method and see if this changes anything Application.targetFrameRate = 60;
Also I’d try to attach your code to this post
Answer by wfreeman480 · Jan 03 at 07:32 PM
After some research I saw that a lot of people switch their UI components with TextMeshPro and that helped. Also have you tried running the game in it’s executable.
hello I had text mesh pro in there, switched to normal text to see if there is any difference and there is actually none
and yes i tried running built game and even if i spamm the inventory you can't feel it because the fps is still over 120, however the game is in a pretty early stage, so it might be worse later
this must surely be a bug, I don't see how this can make sense in any way
I agree. When I was googling people were talking about this actually being a possible bug related to some text elements. One forum I read said that the problem magically disappeared overtime. I wish I could help you more.
ill pray for it to disappear over time lol, honestly have no clue how 40 slot inventory can take 3 ms to activate and 5 ms to rebuild
however it's insanely frustrating for me not to be able to figure this out and fix it, i shouldn't even care probably because u are not going to open inventory every frame...
Answer by OnYtoln147 · Jan 04 at 01:12 AM
A little update, I moved from Unity 2020.3.17f1 to 2020.3.18f1 and the performance issue is basically halved, now it takes 4-5ms for inventory to open (2ms for gameobject.SetActive and 2 ms to build canvas).
At this point it feels like a joke. Must be some terrible bug because I have no clue how can this be happening.
Issue still unresolved!