Speed Up Compile Times in Unity with Assembly Definitions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] if you've spent any significant amount of time working on a project in unity you've probably become quite familiar with the somewhat frustrating phenomenon of unity's compile times growing longer and longer and longer in fact if you're anything like me you're finding that so much time passes between making a changing code and unity responding again that your attention drift has probably brought you away from work and right to this video while you wait well luckily for you you've stumbled across the right video as it turns out that unity actually has a little secret that it's been hiding from you that can drastically improve compile times and reduce those gaps between you writing code and you working in the editor again the answer to this common problem is to start taking advantage of assembly definition files these are asset files that live in your project that unity uses to determine what to recompile and when and if used correctly can drastically improve your experience working in unity hi there i'm matt and welcome to game dev guide in this video we're going to take a look at how we can use assembly definitions in our project we're going to learn what assembly definitions do and how they work we're going to look at how to get started adding them to our project and we're also going to discuss some of the project architecture best practices that we need to consider to get the most out of using them so let's go ahead and get started i've got a project here that i've been working on for a while now and i've noticed that it's beginning to choke whenever we switch back from our ide as you can see things are laid out all over the place there's a whole bunch of folders of assets from the asset store hundreds of scripts for the game and various other folders full of tools and utilities i'm sure this is a familiar sight to many of you as your project gets bigger unity takes longer and longer to respond after making changes and so it gets increasingly more frustrating to work on a large project like this it's rather demoralizing to understand what's going on and why this happens we need to switch back into our ide and explore how unity and c-sharp compile our code if we take a look at the solution file for our project notice how a lot of our classes are bundled up into this header of assembly c-sharp or assembly c-sharp editor one being the runtime files and the other being all of our custom unity editor classes in a nutshell an assembly is just a group of classes like this when we create a new script in the project unity automatically adds it to one of these two assemblies when we edit a script that's in an assembly and return to unity it recompiles everything in that assembly and any assembly that has a reference to it so as your project grows and your classes get bigger and more dependent unity steps through every single entry in this list and recompils it even if it's just one file that you've changed with this in mind you can probably understand how breaking up your project into lots of smaller groups of classes would speed things up by putting things into these groups we can stop unity from wasting time recompiling large chunks of code that hasn't changed this is exactly what assembly definitions let you do assembly definitions essentially break up your code into small groups and enables unity to recompile select groups of your code base instead of the entire code base whenever there's a change notice how there's additional assemblies in here already that's because a lot of unity's built-in packages are defined in their own assemblies so they don't recompile when you're working on your project it's worth mentioning that adding assembly definitions to your project comes with a few architectural constraints that you'll need to consider and may influence how useful they'll be to you in your current project when building out your assemblies you'll often need to think about how well your project splits up into standalone groups of code in order to get the most out of using them we'll discuss what i mean by that in a bit for now though one of the things you can probably do no matter how your project is set up is group all of your asset store code into its own assembly it's pretty safe to assume that many of you never touch the code from the asset store but unless the developer has defined an assembly for their asset most of the scripts in these assets from the store will be included in this c-sharp or c-sharp editor assembly so if you've ever noticed unity slowing down after downloading quite a large asset from the asset store this is most likely why scripts included in these assets are all being included in the recompile process anytime you make a change to your game code seeing as a lot of this stuff doesn't get touched most of the time we can pop it into its own assembly and likely see an increase in compile times straight out of the gate to do this let's create a new folder called external and then let's move all of our asset store assets and folders into here then inside this directory let's create a new assembly definition file in the root of this folder and call it project.external now we have compiled all of our asset store files into its own assembly if you get any errors in the console it's likely that one of these asset store files is referencing a unity assembly or package you can solve this error by adding the assembly it's looking for in the assembly definition references list if we hit apply and take a look back in our ide we can now see our new assembly in the solution now whenever we change our game code unity will no longer recompile the scripts in this folder these scripts will only get recompiled if one of them changes if you've got a lot of asset store scripts in your project you should already notice an improvement in compilation times whenever you make a change to your code if you're using 2020.2 or later unity has actually made a whole bunch of improvements to the editor making assembly definitions even faster and more efficient information on that is in the description for most people in the early stages of their projects moving asset store files into its own assembly will be more than enough to speed the project back up but if you're pretty deep into development like i am you've probably still got quite a lot of scripts lurking around in those two assemblies unity creates for you the goal then should be to break up your code into chunks that make sense for your project if you have a project full of cross references and spaghetti code this is going to be a bit trickier to optimize because assembly definitions work best when they're not referencing other assemblies this is because assemblies are recompiled any time code within them changes or any anytime their references change and this makes sense right if code in assembly a is referencing code in assembly b and code and assembly b changes assembly a needs to recompile to make sure that that code it's referencing is still valid so with this in mind you want to consider just how granular you can get with your assemblies the next step you can probably take regardless of your architecture at the moment is to split your code base up into an editor assembly and runtime assembly there's no point recompiling runtime code when you're just working on editor scripts so let's create folders for those and split everything up accordingly in our editor assembly we'll need to create a reference to our core assembly keeping in mind what we said earlier that any changes to our project will cause our editor assembly to also recompile but not vice versa you may notice here that we can tell unity to include different assemblies based on the target platforms we know our editor assembly only needs to work in the editor so let's set this as an editor assembly if you're building for console or 64-bit machines you can break your code base up into assemblies for these platforms and unity will only include them when it's set to build for that platform so we've now got three assemblies our asset store code our editor code and our runtime code from this point it's going to vastly depend on your project structure to determine if you can break down your code further into assemblies and sub-assemblies i can't really help much more than this other than show you how i've broken up things in my own projects this is the game i'm working on and before doing my thing with assembly definitions it would take upwards of two to three minutes to respond after making a change to a single script so i have a few different editor tools in my project and i don't really want all of my project to recompile when i'm just working on the tool so i've broken the project up into my game code and tool code assemblies i've also added a shared assembly for different utility type classes that rarely change much but all of these other assemblies need to have access to you'll likely want to play around with these to figure out something that works for your current project like i said before you'll need to consider removing as many dependencies on other assemblies as possible to get the most out of using them so you may have to get creative with how you group code or create cross-assembly references and this is kind of an interesting side effect of using assembly definitions as they force you into a workflow that influences clean architecture and helps you avoid lots of spaghetti references after all of this work you should notice a significant improvement in how long unity takes to respond after your code changes especially when you're working within an assembly that isn't heavily referenced and that's about it for this video hopefully you found this useful be sure to leave your thoughts down below and let me know if you've learned a thing or two about assembly definitions and speaking of learning a thing or two excellent segue nailed it you may also be interested in this video's sponsor skillshare as you probably know skillshare is an online learning community with thousands of inspiring classes for creators explore new skills deepen existing passions and get lost in your creativity a class that i've really enjoyed watching is this one called 3d illustration creating isometric designs in adobe illustrator which basically gives you the tools to create isometric art in illustrator that you can use as sprites in your games i'm a huge fan of simcity and isometric art in general so this one is great for me and definitely worth a watch skillshare is created specifically for learning meaning that there are no ads and they're always adding new premium classes so you can stay focused and follow wherever your creativity takes you if you're interested in supporting the channel click the link in the description below as the first 1000 of my subscribers to click the link will get a free trial of skillshare's premium membership so you can explore your creativity and check out these classes yourself thanks again to skillshare for sponsoring and also a big thanks to my patrons for your support i really appreciate it be sure to like the video if you've enjoyed it and if you're new here feel free to hit that subscribe button it'll let you follow the channel and see new videos as they go live as always thank you very much for watching and i will see you again next time you
Info
Channel: Game Dev Guide
Views: 22,540
Rating: undefined out of 5
Keywords: unity, learn, games, gamedev, how to, making games in unity, tutorial, unity tutorial, learning unity, unity3d tutorial, programming, making games
Id: eovjb5xn8y0
Channel Id: undefined
Length: 9min 54sec (594 seconds)
Published: Fri May 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.