11 Things You (Probably) Didn't Know You Could Do In Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] if you've been watching this channel for a while you'll know that i frequently try to sneak in a couple of handy tips or tricks in the middle of videos showcasing some of the things you might not have known you can do in unity i often try to make them fit within the theme of the video so not all of the tips or tricks that i've picked up over the years are able to make it into an episode here on the channel similarly unless i wanted to spam your subscription box with a bunch of short videos that nobody watching on anything other than a phone likely appreciates a lot of the other tips aren't really long enough to merit their own video as a single talking point and so they may never make it into a video if the theme doesn't fit collect enough of these tips however and they just might be long enough to turn into a compilation video something that i think has a lot more merit to be posted here on the channel hi there i'm matt and welcome to game gamedev guide and in today's video we're going to take a look at some of the not so commonly known things that you can do in unity in c sharp that might make your working life a little easier now we all know that there are plenty of resources out there regarding unity tips so i'm going to try to avoid a lot of the commonly discussed tips and instead try to focus on some of the more bespoke things that are hidden inside this little black box we call unity before we head down the rabbit hole though i'd like to take a moment to thank this video sponsor millernote different from some of the other types of planning software you might be familiar with millernote is more akin to moving notes and pages around a wall within your office or studio allowing you to collect inspiration plan out tasks and develop your ideas all in the same place their interface is designed in a way that's intuitive and flexible allowing you to drag drop and arrange things in whatever way makes sense for you having a singular visible space to work from helps make planning your game easier but you're not just tied to one type of board boards can be standalone or nested and different boards can serve different purposes depending on your project additionally access to each board can be customized so you can work on it alone or share it with collaborators and work on a board together millernote is free to use with no time limit so whether you're planning your first experimental game or a multi-year project it's no different the link is in the description below to sign up thanks again to miller note for sponsoring now let's go ahead and get started looking at our list so we're going to start off with some of the more generic tips and gradually get more and more bespoke so while you may know some of these stick around for some really interesting ones towards the end [Music] most of you should be familiar with the player loop and the methods that unity uses within our components one of the most common that we use is the start method but did you know that we can actually define start as a co-routine that's right if we change our return type from void to i enumerator here in our script unity will treat our start method like a co-routine meaning that when our object is enabled we can actually define time-based execution of our code pretty handy if you want to start a clock or trigger a series of events upon activation and it saves you having to write your own co-routine method just do it inside of the start method instead when i ask the game dev guide discord server for their best unity tips i got a whole bunch of suggestions for random shortcut commands that make life in the editor much easier so instead of listing those out one by one this one is kind of a bunch of shortcut tips rolled into one you can access and rebind unity keyboard shortcuts of which there are tons of by going to edit shortcuts this brings up a handy window for your shortcuts here you can study and search for all sorts of commands that are available in the unity editor you can remap shortcuts if they're not your tastes but honestly the most useful part of it is being able to find all of the existing shortcuts for very specific commands in the editor it's worth noting that you can also create various different shortcut profiles using this drop down over here study this and become one with the editor [Music] one of the most annoying parts of unity is its lack of auto saves or any kind of backup system seriously unity it's 2022 and you still don't have a solution for this thankfully there is one minor saving grace when you're four hours into an editor session and suddenly faced with the horror of a crash window unity actually saves your scene anytime you hit the play button so if you happen to have caused unity to crash in play mode you might be in luck before you open unity again head into your temporary files within the project directory in here you'll find either one or a list of dot backup files these were scenes that were loaded during play mode when it crashed so if you rename these to a dot unity file and copy them out and back into your assets folder you have a somewhat auto saved version of your file it's not a perfect solution but it's better than losing all of your unsaved work when the editor crashes i guess again unity you're halfway there come on buddy this one is a little more commonly known but another pretty handy trick inside of the editor comes with the ability to lock the inspector as you probably know you can lock it via this little padlock up here to stop values changing when you click away making it handy to assign lots of assets into a list for example but did you also know that you can duplicate the inspector and have two open at once this can be ideal if you need to compare values between different objects and assets simply lock one inspector open up another and you can have them side by side to compare with similarly if you want to keep access to a specific component while you navigate around your project you can right-click on any component and choose properties this will extract a floating window of your selected component for you to make adjustments at your own discretion [Music] while we're on the topic of the inspector here's something handy that you might not have known you can do let's suppose we are working on our own little custom editor script and we're looking to build out a nice little drawer for some internal unity components in a handy place however in order to fetch the properties for our editor we're going to need to know the property names of the variables this is usually not a problem when we're doing things on our own scripts as we usually know all of the property names and types however unity being the little black box that it is means that we can't easily open up the source code for the component and look up the variable names what we can do however is switch our inspector into debug mode and hold the alt key as we hover over one of these labels as you can see this shows us all of the serializable names of each property on our components we can now look here and find the official value to use when trying to find the names we need to reference our serialized properties correctly and now the final and probably most useful thing you might have known you can do with the inspector in debug mode and that is replacing inherited components easily so let's suppose i have a component in my class that is just a default unity button well then let's suppose that for whatever reason i want to replace this button here with my own custom button that inherits from the button class like this most people will probably not think twice and simply delete the button component and then replace it with the new derived version the only issue with this is that you lose all of your settings and you have to set it up again if you're doing this on a few different objects or components this quickly becomes quite tedious what you can do to speed things up is throw the inspector into debug mode find the class you want to replace it with and select it now your component will be replaced with its variant and all of your properties will carry over what's more is that you can do it in reverse too this is a trick that has saved me on countless occasions that i've needed to replace various built-in components with my own custom ones or restore scripts back to their default type lists are extremely useful but have you ever found yourself wanting to keep a reference to a collection of classes that inherit from the same base type for instance inside of a scriptable object previously in unity any attempt at doing so would limit you to only being able to serialize the property from the base type your derived classes additional fields wouldn't be preserved and your object would instead be serialized as if it was the declared type instead so for instance here i have a base class of type farm animal and these three classes that derive from this type in my scriptable object i have a list to hold my farm animals if i try to add either a cow sheep or pig here in unity notice how it's only storing the information from the base class in this list to stop this from happening and to preserve the integrity of polymorphic classes inside of a list we can use the serialized reference attribute above our list to tell unity to store it as a reference rather than a value or in layman's terms preserve the polymorphism inside of the list now when i add these into my list their structure is preserved and our list can hold a variety of different derived types okay so this one is for those of you who are frequently working on editor tools this is kind of three tips rolled into one but they're all focused on icons so i'm just gonna roll with it so we should by now know that we can assign icons to our custom classes yes if not you just go here click this and hey presto your class or scriptable object is now easy to identify but how about getting access to an object's icon so that we can display it inside of the editor for instance let's suppose i'm building a custom editor for my scriptable object here i'd love to be able to show the icon as part of the window i'm building well we can use the editor gui utility dot object content method to do just that and retrieve the information from any instance unity object so for our editor window here we'll grab the texture from our object like so i personally find it much easier to locate things via their icons so this method is extremely handy for improving the user experience of my custom editors and a final note while we're here have you ever wanted to use some of unity's built-in icons all you need to do is know the name of the icon and use the editor gui utility.icon content method i'll link below to this github post listing out a variety of the available icons that are built in but for instance let's say that i want to show an icon on this button to act as my list of scenes well instead of designing my own one this scene asset icon here is the perfect icon for that so i can retrieve it from the editor like this and it then fits [Music] perfectly while we're on the topic of custom editors one of the things i've often shown off on this channel is how to build and embed editors from other objects in a single editor something i recently learned is that there's actually a built-in function in unity to draw an editor from another object inside of another editor that's a lot of the same word so let me give you an example i have a script here for my camera and my camera is linked to a camera settings scriptable object what i'd love to do is show this scriptable object inside of my editor for my camera controller here and access its settings well we can actually write a little attribute that will automatically expose any scriptable object settings within our editors by using the editor.create cached editor method to draw it we'll create a custom attribute called expose scriptable object attributes and a property drawer for that attribute in here we can add a little fold out and when expanded used our cached editor method to draw the editor for our scriptable object [Music] if we then add this attribute to our scriptable object in our camera settings here the inspector now shows us a little drop down and draws our scriptural object's properties exposing all of the settings for editing more easily [Music] hanging around on editor windows for a moment one of the awesome things that i recently discovered is the ability to present an editor window as a modal window this can be extremely useful for interim windows you might want inside of custom editors as we can actually use them to pass results back into our original editor windows here's an editor window and when i press this button here i want another window to open and i want the result of the window to be returned and displayed in this base window here if we tell unity to open our second window as a modal window we can actually pass an object back as a return parameter so in this instance we can just return the string value when one of the options is picked and the window is closed notice how when we open the window we can no longer interact with unity and need to choose one of our items here when we pick it our window closes and the string is returned and the next line of our code executes with that property assigned [Music] [Music] okay so we probably all know about the formerly serialized as attribute right well sometimes you need to rename or move an entire class and that's usually fine and unity can often remap those references pretty easily but let's take a look at what happens if the class is moved or renamed and is referenced in a list that is using the serialized reference attribute that we discussed earlier we lose it completely in fact the list just breaks entirely and we get an unknown managed type error if you're doing a big refactor of your project and you need to rename or move certain classes around and you have lists like this this absolutely sucks thankfully the moved from attribute exists inside of the api updating namespace and comes to the rescue using this attribute we can tell unity how to find the reference again by explaining what the class used to be called and which assembly or namespace it used to belong to in this instance i just renamed the file so we can leave everything else blank and tell unity what the former class name was [Music] as a final bonus tip here if you see something in a video of mine but you're getting a little squiggly red line here often it just means that the namespace is missing and you need to import it if you hit control and period inside of visual studio that will show you a list of quick fixes to problems like this and often will suggest the name space that you need to import most of you probably do know that one already but i get so many comments around this that i really just needed to show you and hopefully solve the issue for you so those are a bunch of things you might not have known you could do in unity before watching this video how many of these are new to you how many did you already know let me know your thoughts and any of your own awesome less commonly known unity tips in the comments section down below this was a fun one to put together so definitely let me know if you'd love to see more videos like this in the future huge thanks to everyone over on the gamedev guide discord server for their suggestions if you've enjoyed the video be sure to give it a thumbs up and if you're new to the channel please do consider subscribing as you'll be able to know when new videos like this go live however if you'd like to see more videos from me first why not check out the one recommended on screen now as always thank you very much for watching and i'll see you again next time you
Info
Channel: Game Dev Guide
Views: 131,379
Rating: undefined out of 5
Keywords: unity, games, gamedev, how to, making games in unity, tutorial, unity tutorial, learning unity, unity3d tutorial, programming, making games, unity tips, things you can do in unity, unity engine, game development, c#, learn unity, unity 3d, unity3d
Id: mCKeSNdO_S0
Channel Id: undefined
Length: 13min 49sec (829 seconds)
Published: Wed May 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.