Top 5 Optimization Tips from Building a Mobile Game in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to give you my top five optimization techniques that i used in my game llama survival to boost the fps from nearly unplayable on my powerful pc to a solid 60 fps on a mobile device hey chris here from lom academy here to help you yes you make your game damn dreams become a reality optimization is a pretty weird topic to try to do a tutorial on because everybody's game is different everybody needs to look at different places to do their optimization some of the tips i'm going to give you today are widely applicable and will provide a benefit to your fps but some of them depending on your game type maybe not applicable that's why it's a little bit weird because there's so many different optimization techniques you really need to inspect that profiler to see where's your fps going and where can you optimize so the number one tip i'm going to give you is to use the profiler the profiler is your main tool when you're trying to find out where is your fps going how much memory is my application using and this kind of information that you need to know where to focus your efforts unity gives us some okay documentation on how to use the profiler i'm just going to go over some quick basics though because really the profiler is pretty complicated and there's a lot to know about it so depending on when you watch this either there'll be a card on the screen link in the description to that video or if you're watching it right when this comes out i don't have that video yet for me usually the cpu is the bottleneck so that's usually the first place that i look before i look at anything else in the cpu section you can see exactly how much time is being spent on each type of task and from there you can optimize okay i'm spending a lot of time on scripts dive in a little bit and see which scripts are taking up the most time that kind of gives you the direction of okay so this script is taking a long time what's happening in that script and then you can optimize that script maybe animation is taking a long time in that case there's some tips that i'm going to give you in this video how you can work on lowering that animation overhead again i don't want to go too much in the profiler so just number one tip use that profiler for tip number two batch batch badge in unity you can view the number of batches draw calls set pass calls all these things in the rendering section of the profiler we're going to talk specifically about the draw calls here because the more objects you can batch together into a single draw call lowers the amount of overhead that's happening between the cpu and the gpu that's the processor and the video card each draw call is essentially the processor telling the video card hey please draw these objects into my scene and there's some latency between when the processor tells a video card to do that and when the video card can actually do that so the lower number of those that we have the higher fps can go if we have thousands of draw calls that's every single frame thousands of times the processor has to tell the video card draw this draw this draw this draw this and that really lowers your fps especially if you're like me and you're targeting mobile platform it doesn't handle that very well on a desktop application this is a little bit less of an issue but there's still a lot of performance be gained if you still do the batching i had something like 2500 draw calls and still could get hundreds of fps in a previous game that i did but that was really unoptimized i've told you to batch objects but how do you batch objects in unity if you mark in the inspector that an object is batching static and it shares a material with other objects in the relative area that are also marked batching static unity will do an okay job at trying to combine these meshes so they can be drawn in a single draw call doing it this way relies on unity to do the optimization for you and leaves a lot to be desired remember that for an object to be batched it needs to have the same material and thus also the same shader and either needs to be marked batching static so that way unity will try to do that for you or you need to combine those meshes together because let's say that we have like 100 buildings maybe we made our own cool modular asset pack to build our own buildings we have now thousands of objects in the scene that all share the same material and same shader but that wall or that building is made with hundreds of different objects because i've got windows i've got wall sections i've got corner pieces unity will try to combine a bunch of those for you but what you can do is manually combine those meshes together yourself and that will make it so we know exactly all of these can be drawn in a single draw call the process to doing this some people maybe say it's straightforward i couldn't understand it when i was first working on this so i ended up buying an asset from the asset store it was five dollars it's simple mesh combined and that worked pretty well for me i'm not getting paid from that asset creator but i did find it to be really useful there's an affiliate link down in the description if you want to check that one out what this does is allows us to in the inspector select the objects we want to combine together click a button to combine those meshes and if we realize that oh we we can combine more or maybe the way that we combine these didn't work out so well we can undo that operation still in the editor and once we've played around and found out the way that we want it to be we can export that then as an obj file or a dot asset file so that way it's not baked into the scene i found that to be a really useful feature because whenever i was combining my buildings for example in llama survival some of those buildings i needed to be able to fade out because they block line of sight to the player and if i had too many buildings together that were batched all in the same draw call whenever i faded out that one building that was blocking the player you'd actually fade out all of the buildings because they were all the same mesh whenever you're combining these meshes really consider which objects should be batched together and weigh that against that performance benefit of getting one less draw call the last thing i want to say about combining meshes is make sure that you export that combined mesh into an asset file because when it's baked into the scene it makes your loading a lot slower at some point my scene bios got inflated to like 30 40 megabytes because i a lot of duplicated mesh data in the scene and it made it take a really long time to load each scene after i exported all these and reused the same meshes where they could be reused i lowered the scene size to under megabyte and the loading became almost instantaneous tip number three limit the number of skinned mesh renderers you'll remember earlier i was talking about how maybe your animation time is taking up a lot of time that's exactly the problem that i was running into i had potentially hundreds of animated models on the screen at the same time on a mobile device and it just ran absolutely terribly the reason for this is that skin mesh rendering is done on the cpu so it's a cpu that's modifying the vertex positions of all of the meshes this is something that can be highly parallelized which means that it's really a better job for the gpu the video card this technique is called gpu skinning and it removes the need for the skin mesh render and allows us to use a normal mesh render instead with a special shader on it that will do the manipulation of the vertex positions meaning that all that happens on the gpu instead of on the cpu unity understands that this is a problem and actually gave us some tool in 2018 that would allow us to batch skin to match renders by using gpu vertex manipulation instead of doing it on the cpu so you just have a mesh with some special shader and some properties that would get adjusted at runtime and that would animate your mesh for you i had a really hard time using this i really couldn't figure out how to make it work even and the documentation wasn't very good and i didn't understand the concept extremely well whenever i was first working on it honestly i still don't so there's a link in the description if you do want to check that one out it is free and it's on github it's available for everybody what i did was purchase this asset from the asset store called mesh animator again i don't know the guy who made this they did a really good job on it though i've got an affiliate link down in the description below but i wasn't like paid to promote this for him it just really gave me a great benefit my game when i couldn't figure out how to use the unity tool it was really easy to integrate and once i did this i have now a mesh renderer instead of a skin to mesh render on my zombies and they got moved and manipulated on the gpu instead and that reduced my animation time to almost zero because all this animation is done on the gpu it's not technically called an animation anymore this increased the load on the gpu and significantly reduced the load on the cpu there are some trade-offs to doing it this way number one is it uses a lot more memory and the higher fps animations you want use even more memory so if you have only a few animated models on the screen at a time probably this isn't going to be the optimization technique that you need to use if you have hundreds or thousands of animated models on the screen at the same time then this is something that you can take a look at that would really improve your fps tip number four use the job system where it makes sense you may have heard of the concept of dots or data oriented text stack the job system is only one piece of that and it's really the least experimental one i think it's actually fully released meaning it's a stable addition to the unity engine the concept of this job system is we can use multiple threads to do something that we need to do whenever writing c sharp code by default it's only going to run on one thread meaning if you have a 12 or 16 core processor you're only using one of the 16 or 12 available cores there are other ways besides the job system to do multi-threading in c sharp but the drop system is a way unity gives us to safely do some stuff that normally can only be executed on the main thread meaning we can do things like physics.raycast or physics.spherecast on multiple cpu threads which really can improve your fps let me give you an example in my game i have ranged enemies that need to pick a location around the player that has line of sight to the player that is some distance away so to do that i pick a point around the player i then do a spear cast from wherever that point is to the player and determine did that hit something or did it hit the player if i hit the player great we have line of sight if not i need to choose a different location and try again it's a relatively simplistic algorithm but that's what i ended up with i could have dozens of these ranged enemies in the game alive at the same time this made the fps come to a crawl on some of these mobile devices because they couldn't do 50 sphere casts every frame or every x number of frames if i throttled it along with all the other stuff that's going with the pathfinding and the attacking animating gpu stuff all that was just too much so i did was jobified this code to make it where it would run across all the available threads on that device this made it so there was virtually no overhead instead of this huge chunk of time happening on the main thread that got split across however many threads are available and it made it go extremely quickly not all code should be done this way because it is a little bit more complicated to write it this way there's also some overhead whenever we're dealing with multiple threads because we need to synchronize those threads and then come back to the main thread at some point that's why i'm saying use a job system where it makes sense only when you're doing something that needs to be parallelized because you're going to do some expensive operations all at the same time they need to be done let's say in a frame only in those cases should you consider okay let's jobify this thing those use cases are the ones that make the most sense to turn into a jobs type of job tip number five reduce the complexity of your canvases maybe you're like me and you thought oh the ui is virtually free to render because there's not that much going on there and it's not really that complicated but think that's not correct the ui ends up being an extremely complex part of your game the way i initially did my ui that i had to completely scrap and start over from scratch was i had a bunch of really nested transforms so i'd have like 13 14 deep transforms and whenever you would modify one of those makes the entire canvas recalculate what's going on there basically and whenever i was animating these first off i was using the animator don't use the animator on your ui use a tweening library there's lean tween that's for free you can make your own if you really want but lean tween's free might as well use it anyway whenever you're using the animator it also makes any time you're moving anything it has to re-calculate the entire canvas every frame which is why you're not supposed to do it it's very slow couple that with having an extremely complicated deep one where i'm gonna animate some stuff back and forth simple kind of slide this in slide this out animations that i was doing would go at like 10 fps when there was nothing else happen it was the main menu there's nothing besides a canvas there and it would perform so poorly so as they say keep it simple silly in general unity recommends that you don't have extremely nested transforms so on the ui there's no difference there don't have extremely nested transforms if you can help it second piece of that don't use the animator on the ui terrible for your performance use something like lean tween instead as long as we're here i've got one more bonus tip you can batch your canvases so if you have some like sprite pack or something that you got and you're using 15 of those sprites let's say on your ui what you can do is combine all of those into a single texture using a sprite editor you can then slice up that 15 packed textures into one texture and use that on your canvas if you then split your canvases by each one's only using one texture then each canvas will only take one draw call ideally so when i was really trying to optimize my draw calls my canvas was taking like 30 40 draw calls if you batch your entire canvas with one texture you can reduce the canvas draw calls to down to one alright to recap the five tips number one inspect the profiler if you're not inspecting the profiler you're gonna optimize the wrong thing that why waste time doing something that's only gonna optimize like one percent of your frame time when something's taking 15 of your frame time number two batch batch batch as much as possible use the same material use the same shader combine meshes as much as possible number three remove skinned mesh renderers as much as possible if you have a lot of objects that all share the same material those are great candidates to use some kind of animated mesh like mesh animator or the tool that unity gives us number four use a job system where it makes sense if you have some piece of code that's highly parallelizable then you can use the job system to get a bunch of stuff done in the same frame across multiple threads remember that what i did was parallelize the sphere casting for my ranged enemies to get line of sight of the player and that greatly reduced the amount of time each frame spent on trying to find that line of sight number five simplify your canvases do not have hugely nested transforms that are moving around that makes it where the ui system ends up running really slowly and can really drain your fps at a unreasonable amount and the bonus tip batch your canvases too combine as many sprites as possible into a single texture and use that one texture on your canvas and remember there are a lot of other optimization techniques these are just the top five that i used in my game that provided the biggest benefit when i was doing my first optimization pass my game is far from perfect there's a lot of areas i can still improve on the optimization and the polish front so i'm just sharing those tips that i found useful so far i want to give a huge shout out to all of my patreon supporters every one of you is helping this channel grow reach more people and add value to more people and that means more people are making their game development dreams become a reality if you want to support this channel you can go to patreon.com academy choose which tier is right for you get a boy shout out starting at the awesome tier and some other cool perks at the tremendous and phenomenal to your level speaking of those awesome tier supporters i have andrew bowen gerald anderson autumn k matt parkin and paul berry thank you all for your support i am so grateful the key thing to remember when you are doing performance profiling is understand what is your performance target for me it was 60 fps for you maybe it's only 30. if you're meeting that performance target don't waste your time optimizing something that you're writing me a performance target it's once you start having trouble meet that performance target on the hardware that you want it to run on that's important to check too if i have this powerful gaming pc and i'm getting 60 fps and i throw it on the phone and i get 10 fps i definitely need to optimize it some more if you want me to do some deep dives on some of these topics go ahead and leave me a comment down below about which topics you found really interesting and that you'd like to hear more about if you got value out of this video please consider liking and subscribing to help the channel grow reach more people and add value to more people remember those new videos posted every tutorial tuesday and i'll see you next week
Info
Channel: LlamAcademy
Views: 25,319
Rating: undefined out of 5
Keywords: unity, optimization, tutorial, unity optimization, tips, unity optimization tips, tip, top 5 optimization tips, top unity optimization tips, top unity optimization, optimization guide, how to optimize unity, how to, how to optimize unity game, how to optimize game unity, game, optimize, mobile, console, desktop, high fps, fps, boost fps
Id: 7ioIHn1tmIM
Channel Id: undefined
Length: 15min 53sec (953 seconds)
Published: Tue May 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.