Top tips for scripting in Unity 2022 LTS | Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we are going to look at tips to increasing productivity for scripting in unity using Nam spaces in Unity comes with several benefits it makes it easier to organize code avoid naming conflicts it helps facilitate encapsulation and modularity and with that also improves code reusability testing and readability to easily add a root namespace go to edit project settings editor and add a root namespace now when creating a new script the namespace is automatically added you can also edit the script templates themselves including adding a namespace and much more first delete out the root namespace in the project settings to ensure you don't end up with two sets of namespaces script templates are stored in a specific folder on either Windows or Mac make a copy of the script templates folder now you can drag this folder into the unity project window close and restart unity and now it will be using the templates in this folder instead of The Originals open the first file which is the default script template here you can add a name space to protect your scripts now remove any unnecessary comments and modify it in any way you like for example you could remove the start and update methods or add awaken disable methods or any other combination new scripts will now use this updated template and you can change the template layout numerous times throughout the the project attributes are metadata markers that provide additional information or behavior to classes Fields methods and other elements within Unity scripts now you probably already know tool tip and serialized field but there are several more available this script requires a character controller to be attached to an object so we can use the required component attribute to achieve this now when I drag the script onto an object it automa Ally adds the character controller component it also prevents anyone from removing that required component and reduces the chances of Errors when a script needs to communicate with a component type there is also a help button on all script components by default this will take it to the unity documentation however we can customize this to open any URL using the help URL attribute I can paste a URL here and the out button now directly links to that web page this can be useful for sharing a workflow directions or instructions from a web page or a YouTube video you can also customize the inspector using attributes such as header to create a heading and range attribute to restrict a float variable within a certain range now in the inspector this looks much clearer and I have a slider for the floats this ensures that we cannot go beyond the maximum or minimum values a Unity project should ideally feel like it's being developed by by a single author no matter how many developers actually work on it a style guide can help unify your approach for creating a more cohesive code base it's generally recommended to follow industry standards wherever possible however some projects have different needs the key is that while there's more than one way to format Unity C code agreeing on a consistent code style for your project enables your team to develop a clean readable and scalable code base let's look at some general recommendations avoid variable names that are unclear such as single letters or abbreviations ensure the names are descriptive and easy for all team members to understand you could use camel case Pascal case or snake case when creating variable names but be consistent throughout your project for variable names avoid using too many prefixes or special encoding you could use an underscore for member variables or M underscore kcore for constants and S underscore for static variables so the names reveal more about the variables methods perform actions so their names could begin with a verb and add context as needed for example get direction find Target you are aiming for clarity Above All Else every team member should be able to understand the code without ambiguity or confusion for more tips download our code style guide ebook from the video description below object pooling is a technique which can improve memory management to minimize performance Spike significantly in your game using unity's pooling class improves gameplay performance for example when creating bullet prefabs instead of instantiating and destroying every bullet it instantiates them once and adds them to a pool now they switch off when not needed and switch back on when needed we can see this in the script an object pool variable is declared in the awake the object pool is created it then instantiates the object adding it to the pool now instead of destroying the object it is released back to the pool and instead of instantiating a new object it gets the object from the pool find out more about this in the object pool tutorial Link in the video description below another quick tip to dramatically speed up the time of previewing your gaming Unity is going to edit project settings and in the editor section enable play mode options both reload domain and reload scene are both unchecked so now it doesn't have to rec compile every time you press play notice the scene now begins playing immediately Unity Muse supercharges your project development solve challenging problems get ideas for your project and generate usable code through conversational AI chat that guides you on your creation process I can ask Muse to generate a script by asking a question I will ask how to dynamically create a box cider during runtime and double its size every time I press D I tell Muse which version of unity I'm using to get code specific to that version after a few seconds of musing it provides a script example and also a detailed explanation of how that script works I can now use this example modifying it slightly so it finds the correct object during play mode it works exactly as I wanted it to Muse also Flags potential performance issues in this case increasing collider size during runtime so it also guide you as well as providing a solution Muse can also be used alongside the unity documentation so here I want more information on how the render State block Works copy the code and ask Muse how this code Works paste the code here and muse now provides a detailed explanation of how it works step by step you can also ask follow-up questions to add further clarification Muse can also be used to improve your existing code here I have a script that destroys objects when they get with in a certain range of my object Muse has analyzed the code and provided a more efficient version that helps to improve performance Roslin analyzers are tools that Leverage The Net compiler platform Roslin to provide static code analysis directly within the IDE and is powering your intellisense these analyzers examine your code as you write it identifying potential issues suggested improvements and enforcing en coding standards and best practices you are probably already using it here it will add a missing name space or include a cast to convert a float to an integer the red is the current code and the green is the recommended updated code allowing you to preview the change before applying it or add a missing variable it highlights errors in the code making it quick and easy to spot and fix them you can also highlight a set of commands and right click to quickly extract a method or a local function to improve formatting and readability you can also Implement a new interface use the shortcut control shift and M for Windows or command shift and M for Mac to open a list of methods and have the code autocomplete it for you an efficient way to enforce code style rules across a large team is to set up an editor config file that ships with your project to do this in visual Studios go to tools options and under text editor C and code style General click generate editor config file settings this will now create the editor config file now under view open solution Explorer and go into folder view double click on the editor config file to be able to customize all the options which will be enforced when coding for any option you can change the severity levels under Tools new get package manager and manage new get packages for solution you can SE search for more Roslin analyzer packages or customize the ones you already have to learn more check out the debugging with Roslin analyzers Link in the video description below debugging is an important step during any project development the debug class using debug log Etc is useful however to create an effective debugging process use the debuging workflows available in Visual Studio code Visual Studio Rider or whichever IDE you are using in this example I'm using visual studio break points can be used in Visual Studio to check that the lines of code are performing correctly and in the right order clicking in the left section allows you to enter a breakpoint shown here in red I want to check that this line of code runs when I move my mouse over a button from the debug menu choose start debugging in unity it attaches the debugger and asks if you want to enable debugging for this session during play mode now stops playback when I move my mouse over the button and it shows the line in Visual Studio click on the break point again to remove it I can do the same to check that the for Loop is working correctly pressing continue will then skip to the next line and continue to run the code the locals tab at the bottom will also show any variables so we can monitor how they are changing at that specific line in code you can also attach conditional break points by right clicking in the right hand section I want to set a condition on this line to break if damage is equal to 100 now it will only break when the damage is 100 pressing the stop button will stop debugging and return Unity back to normal mode for more debugging tips check out the links in the description below to strengthen your debugging workflow you can also use assertions to validate assumptions about your code during development this can be a great way to catch logical errors or unexpected conditions that could lead to bugs or crashes you should focus on one assertion at a time first add a use an assertions at the top I want to check if damage is being set to 50 on collision check this using the assertor equal 50 is what I expect damage is what I will check if they are not equal I will leave a message to be displayed I have also added a break so it pauses the game immediately when the first projectile hits the wall now we see it in action there are no me mes because the assertion was correct the damage is 50 when the game resumes the second projectile hits and it displays a message because the damage is now 100 assertions are a very useful way of testing that your code is producing the correct results testing in game development can quickly become repetitive and prone to error the unity test framework however offers you a way to automate your testing Unity test framework UTF is available as a package in the package manager and allows you to test your project code in both edit and play modes you can also Target test code for various platforms such as Standalone iOS or Android to learn more check out the link in the video description below Unity provides you with a suite of tools that helps you prevent identify and fix performance problems the profiler memory profiler and the profile analyzer complement each each other let's take a look at how and when to use each one the profiler can be open from window analysis and profiler during playback it records data at any point I can click in CPU usage and analyze frame performance in the timeline which shows the frame completion time in milliseconds and all the thread processes that are taking place this helps you to find ways to identify any potential issues and optimize gameplay profile analyzer which can be downloaded from the package man manager is used to compare the difference between before and after optimization across a range of frames to ensure the changes you are making are having a positive impact on performance memory usage can also be profiled by downloading the memory profiler from the package manager and gives a screenshot of memory usage at a specific point in your game this is important to avoiding game crashes and ensuring the gaml runs smoothly to find out more about profiling watch the tutorials on profiling the links can be found in the video description below download the ebook found in the video description below for more great Unity tips thanks for watching
Info
Channel: Unity
Views: 29,899
Rating: undefined out of 5
Keywords: Unity3d, Unity, Unity Technologies, Games, Game Development, Game Dev, Game Engine
Id: InLmgSOJg3M
Channel Id: undefined
Length: 13min 42sec (822 seconds)
Published: Fri Apr 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.