- Home /
Android performance hit on changing Button.Interactable
Using Unity 5.3.1f1, I'm noticing a weird UI performance hit on an Android device.
The game has a HUD button that's made available/unavailable based on the current quantity of a particular power-up (which is found throughout the game). If the player has at least one such power-up in their possession, the button is available, otherwise it's not. The button availability is controlled by setting its Interactable property.
When playing the game on an Android device, just as the first power-up is touched by the player, the game completely freezes for 0.5 - 1.0 seconds. Digging through the code, the culprit is this single line:
JetPackButton.interactable = _jetPackCounterText.enabled = count > 0;
Commenting out that line completely prevents the problem. And, the delay only happens the first time this line of code is executed. Beyond that, I never see the delay again.
Any thoughts on what's so performance heavy about the above?
Also, the value of the Interactable property doesn't cause (or not cause) a bunch of other code to execute. So, the rest of the execution path is the same either way.
When profiling on the Android device, the obvious spike on the offending frame is attributed almost entirely to "BehaviorUpdate | EventSystem.Update()".
Any advice appreciated.
Jeff
Update #1
A few other pieces of information that might be helpful:
The offending button is enabled by default and is initially disabled by the same line of code mentioned above when the app sets the initial power-up count to 0. So, this isn't even technically the "first" time the button state is toggled. It is, however, the first time the button state is enabled.
The UI system has been already been interacted with at this point, as the User has had to navigate the Main Menu screen in order to start the game - which is where the first "enable" of this button causes a very painful lag.
Update #2
As I haven't yet found a solution for this issue, I thought I'd try to simply "toggle" the offending button once while the User is sitting in the Main menu. I really kind of expected that to fix the issue but, unfortunately, it did not. I'm running out of ideas and would appreciate some input.
Answer by Fredex8 · Mar 08, 2016 at 10:32 AM
To be honest I am not even especially sure what that line of code is actually doing. It seems a bit messy to have = _jetPackCounterText.enabled
and = count > 0
in one line.
Perhaps just using a simple if (count > 0)
to enable it would be better?
if (count >0)
{
JetPackButton.interactable = true;
}
else
{
JetPackButton.interactable = false;
}
If not you could always fake the interactable element of the button by setting it active/inactive and having an identical image to the button with no interaction active/inactive inversely. If that makes sense.
Thanks for the input, but that code is fine. It's simply setting the value of 2 bools (JetPackButton.interactable and _jetPackCounterText.enabled to either true or false based on the result of the expression "count > 0". So, if count is greater than 0, both bools are set to true. If count is less than or equal to 0, both bools are set to false.
Again, everything works exactly as intended, there's just a nasty delay the very first time the button is enabled in a running instance of the game. From that point on, there are no more delays...
Thanks for the explanation there. I'm fairly new to C# so shortening things like this comes off as pretty illegible to me. As far as I am aware shortening the code like that doesn't actually result in any performance improvement so keeping things easier to read makes more sense to me. I've not tested my build on Android so it's entirely possible that enabling buttons my way causes the same problem but have you tried writing it out the long way to see if the delay still occurs? For some reason it just seems logical to me that the compiler could struggle with that condition.
"For some reason it just seems logical to me that the compiler could struggle with that condition."
Thanks, but this is pretty standard code and definitely not something the compiler "could struggle with"...
Your answer
Follow this Question
Related Questions
How do I make my player jump on click on the button? 3 Answers
Bad performance on certain android devices 1 Answer
Scene not loading properly on android device? 0 Answers
How to set mobile control and scale in size in android setting? 1 Answer
How do I control the behavior of Android soft buttons (Immersive mode)? 1 Answer