Animation CPU Spike from Loading.ReadObject - c#

The first time I run my game and trigger an Animation, there is a pretty big CPU spike (last one was 153ms) which once investigated, led me to see that it was Unity loading the animations as needed and that was causing it, at least that's what I believe.
I have read around and everyone seems to say that having the assets in the Resources folder and using Resources.Load("") would fix it, however I am still running into the issue despite placing that in both Awake() and Start() methods on various objects. Am I doing something wrong? Or is there a specific way for me to load the sprites I need at load and would that stop the CPU spikes?

Ever since unity 4.5.x There is a way to use Resources.Load("") in a asynchronous way. This would allow you to prevent/reduce the spike in CPU usage quite a bit. The syntax for this being Resources.LoadAsync() In combination with a coroutine you should be able to resolve your issue.
If you need more detailed help with this, please include your actual code in your question as well.

Related

Why does activation phase of a scene being loaded in Unity take too much time?

I'm new to Unity3D. I am trying to make my own FPS game in Unity 2020.3. I have to make a lot of maps (scenes). So far I discovered a problem with LoadSceneAsync.
What I know from Unity docs: When AsyncOperation.progress variable is between 0 and 0.9, the engine is loading resources and stuff for the new scene. When it reaches 0.9, it starts activating the new scene (deleting old objects and adding new ones + running Awake and Start methods?).
The problem is that the activation phase takes A LOT of time. For example: when the loading phase takes only a few frames, the activation phase takes about 10 or a 100 times more frames to do its thing.
I tried running the game on another devices and I saw no comparable change in loading speed. Is this a Unity problem or am I using it wrong? Or are those device that I tested the game on just not good enough?
I have no idea why this is happening. I pulled up profiler but I can't understand what exactly is going on. Can somebody please explain what makes this so slow and how to improve its performance?
What version of Unity are you using? When I look at your profiler log I see Prefabs.MergePrefabs (834,62ms):
When I google that I find an answer here that seems to imply that this has something to do with nested prefabs, or prefabs used in a GameObject hierarchy.
I honestly am not sure what the answer is saying to do to resolve the issue, but search results also turned up this Unity issue that says a slow Prefabs.MergePrefabs is:
Reproducible with: 2019.3.0a1, 2019.3.12f1, 2020.1.0b8, 2020.2.0a9
The issue tracker states that the issue is:
Fixed in 2020.2.0b1
Fixed in 2020.1.8f1
Fixed in 2019.4.24f1

Unity big scale world simulation performance problem.

We are working on simulation game. We have about 25000 objects at world. All has 1 unity c# script. If we activate empty update function we get 15 fps if we activate empty fixed update with 0.02 time scale we get 1-2 fps at average spec computer. In Will we need to do something on update function. So need some help for this performance problem.
In that case, what can we do for performance?
Thanks for advices and help.
Well even though your question is broad i will try to give some tips. My guess is you are doing some heavy calculations in your Update and this causes fps problems. Also rendering 25000 object at the same time would cause fps issues. First i suggest using Unity Profiler and Unity statistics to find out what is causing the issue. Then my suggestions would be based on the problem i assume:
Use Coroutines instead of doing all the calculations in Update.
Use Occlusion culling for rendering as minimum objects as possible. You do not need to render objects which are not in Camera frustum.
If you have meshes which are detailed use level of detail if you have the chance.
If you narrow down your problem i am happy to help further. Good luck!
The very need to make a call to Update() is one of the major slowdowns in Unity. That's why they are now pusing towards Entity Component System where there is no need for such jumps.
If all the objects are having the same script, it should be quite easy to modify the code so that only 1 object has the script, but operates (i.e. via a for (;;) loop) on all other objects. This should bring massive improvements already.
Aslo if theres any chance your objects share meshes - do use Instanced Mesh Rendering it is faster by two orders of magnitude

Unity 2D Physics: angle limits and motors are preventing rigidbodies to sleep

Aim: I'm developing a turn-based game that heavily relies on Unity's 2D physics. It's important for me that all bodies go to sleep as soon as they stop moving, because I'm waiting for it to start next turn.
Issue: Unfortunately, it happens quite often that bodies never go to sleep. I tried tweaking Physics2D settings for days, thus improving stability and/or performances, but this issue still occurred. I figured out that this problem is related to HingeJoint2Ds: when they try to enforce a constraint (both angle limits or motor force), they won't allow their connected bodies to sleep.
I also tried to manually set really slow moving bodies to sleep, but (and this sounds quite weird to me) it seems that calling .Sleep() on a body causes other bodies to wake up, thus preventing the whole world to ever be able to fall asleep.
Question: Is anybody facing a similar situation? Any clue or workaround on how to solve it?
Thanks!
Have you tried grouping them under a sleeping parent game object then removing the ones you want to be awake from this object?

Optimization of a GC language, any ideas?

I'm a pretty big newbie when it comes to optimization. In the current game I'm working on I've managed to optimize a function and shave about 0.5% of its CPU load and that's about as 'awesome' as I've been.
My situation is as follows: I've developed a physics heavy game in MonoTouch using an XNA wrapper library called ExEn and try as I might I've found it very hard to get the game to reach a playable framerate on an iPhone4 (don't even want to think about iPhone3GS at this point).
The performance degradation is almost certainly in the physics calculations, if I turn physics off the framerate jumps up sharply, if I disable everything, rendering, input, audio and just leave physics on performance hovers around 15fps during physics intensive situations.
I used Instruments to profile the performance and this is what I got: http://i.imgur.com/FX25h.png The functions which drain the most performance are either from the physics engine (Farseer) or the ExEn XNA wrapper functions they call (notably Vector2.Max, Vector2.Min).
I looked into those functions and I know wherever it can Farseer is passing values by reference into those functions rather than by value so that's that covered (and it's literally the only way I can think of. The functions are very simple themselves basically amounting to such operations as
return new Vector2(Max(v1.x, v2.x), Max(v1.y, v2.y))
Basically I feel like I'm stuck and in my limited capacity and understanding of code optimizations I'm not sure what my options are or if I even have any options (maybe I should just curl into a fetal position and cry?). With LLVM turned on and built in release I'm getting maybe 15fps at best. I did manage to bring the game up to 30fps by lowering the physics precision but this makes many levels simply unplayable as bodies intersect one another and collapse in on themselves.
So my question is, is this a lost cause or is there anything I can do to beef up performance?
First of all, love your game on Windows Phone 7!
Secondly, I don't see anything out of the ordinary in your profiler output. I did a quick and dirty performance analysis of the Farseer engine once (running in .net) and came up with similar results. It almost looks like you have a slowdown that is proportional across the board and may be due to mono itself.
I suppose you follow the performance hints in http://farseerphysics.codeplex.com/documentation already :-)
The most important thing seems to be
to reduce complexity for the
collision detection calculations,
i.e. not the visual but the colliding
shapes. In Unijty3D they are called
colliders and you can attach a simple
cube as a collider to a complex human
body. I don't know anything about
Fareer but they probably have similar
concept (is it called body?).
If possible, try to replace your main
character or other complex objects by
easy cubes and check if fps raises.
Compiler switches sometimes leverage performance. Be really sure that there are no debug settings activated (I got up to 30 times slower code in a C++ library project). Ensure that armv7 optimisation is turned on and -O3 or -Os
Watch out for logging statements as they are extremely expensive on iPhone
[Update:]
Try to decrease the number of actively calculated AABBs just to find out which part of the physics engine causes the trouble. If it's the pure number follow FFox' advice.
What is about other platforms? Where did you perform the testing during the development phase, on simulator? Which one? Any chance to get it running on Android or Android simulator or Windows Phone? This would give you a hint if it is an iPhone specific problem.
Ah, I just saw that ExEn still is in pre-release state and the final will be launched on July 21th as OS. IMO this changes the situation: If your App is running fine on some other comparable platform, then just wait for the release and give it a new try. Chances are pretty well that there is still debugging code in the pre-release you are working on.

Debugging a performance issue on ListBoxDragDropTarget (Silverlight Toolkit)?

I have a complex project using SilverLight Toolkit's ListBoxDragDropTarget for drag-drop operations and it is maxing CPU. I tried to reproduce the issue in a small sample project, but then it works fine. The problem persists when I remove our custom styles and all other controls from the page, but the page is hosted in another page's ScrollView.
"EnableRedrawRegions" shows that the screen gets redrawn on every frame. My question is this: How can I track down the cause of this constant redrawing?
I have used XPerf to help track down performance issues related to redrawing in Silverlight. It is not completely straightforward or an easy process, but it can help point you in the right direction to where your problems are.
I started with a great tutorial by Seema about using the XPerf command-line tool to profile CPU usage for a Silverlight app. You can basically load up your app, start sampling with XPerf, perform your CPU intensive operations, and then stop sampling and analyze the profile XPerf generates. When you look at the XPerf charts you can select can filter by some process (such as iexplorer or your browser) to see the total % CPU. You can then select a specific length of time in the profile and drill down to see what functions from which DLLs are taking the most CPU cycles. If you point XPerf to Microsoft's symbol server you should get the specific names of the functions where the app is spending most of its time.
For a Silverlight app it's most important to look at what's going on in agcore.dll, npctrl.dll, and coreclr.dll. If your performance problems are related to redrawing, most of the CPU time is likely spent in agcore.dll since that does most of the graphics related work for Silverlight. You can then drill into that and see the specific functions in agcore.dll that are getting called most often during your sample time.
I understand it is kind of an annoying way to debug since you can only really see what is going on in the core Silverlight functions, but it may be able to help you figure out what is going on. In my case I was able to see that most of the time was spent calculating drop-shadows in agcore.dll. I was then able to figure out I stupidly had some content within a drop-shadow effect that was changing many times a second and causing constant recalculation/redraws of the entire drop-shadow effect.
Once you identify your redrawing issues you might want to look into GPU Acceleration with BitmapCaching if you haven't already. That will help offload some of the redrawing to the GPU and save you some CPU cycles.

Categories