Using Attributes | Unity Editor Scripting

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
attributes are the simplest and fastest way to modify how our code is interacted within the unity editor frequent use of attributes can significantly decrease the risk of developers making mistakes for working on the game and they also make our components look and feel so much better in this video we'll look at using pre-made unity specific attributes first we'll very quickly go through what they are and then we'll walk through various examples to learn how to use them for ourselves as is the case with all videos in this series if you already feel confident on this topic do feel free to skip ahead otherwise let's get started attributes are part of the c-sharp programming language we can think of them as modifiers for how parts of our code is interacted with for our purposes it means that we can change how specific fields and classes behave in the unity editor here i have a new unity project i'll begin by making an empty game object and a new script to put onto it i'll simply call it example and get it opened in visual studio let's say that i in here want to have a public string and i'll just call it description just like that and i'll save and head back into unity of course here i'll get a little field where i can write a short sentence this works perfectly fine unless you want to write a longer sentence or a full text because as you'll see once i reach the edge it just starts scrolling to this side and that's not very convenient because i can't see what i wrote back here and i can't really see like if i move the cursor back here i don't really know where in the sentence i am and i also cannot add new lines in these cases it would be quite a lot better if it behaved more like a text editor luckily for us there is an attribute called text area which achieves exactly that so let's get into visual studio and see how to use it when we want to use an attribute we go before the piece of code that we want to modify and we open square brackets in here we just write the name and you can see that it suggests this here and describes it as an attribute to make a string be edited with a height flexible and scrollable text area that sounds pretty cool if you ask me so i'll write that save and head back into unity okay as you'll see the text area has now jumped down to be under the name here this means that it's a lot wider also it is taller and you'll see that once i reach the end here the text will simply wrap down around so i actually can see all of it at once i'm also able to click enter to create new lines and i get this nice little scrollable area over here i can scroll up and down but perhaps this is still not good enough for you maybe you want to write a really beefy text and you want to be able to see much more at once well as fortune would have it attributes are a bit similar to methods in the sense that they can take arguments and have overloads so if i open parentheses here we'll be able to see that there are two overloads and the second one allows us to specify a minimum and maximum amount of lines i'll just try writing 10 and then 20 in here and i'll save and go back into unity and we'll now see that the text area has gotten 10 lines tall already and if i fill them out and write more it will simply expand until we hit the 20 and at this point we'll get this scrolling area again this is quite the improvement from what we had before and it was all achieved with just this little line this shows you something about how simple and powerful it can be to use attributes something else i want to point out is that attributes are made to be used in a specific context text area for example only works with strings if i were to change this to an integer and go back in here we would see it complaining in both the console and in the inspector here for now you just need to know that this doesn't work so don't do it okay now i want to show you the two attributes that i by far use the most they are called serialized field and hiding inspector when programming it is good practice to hide as much information from the outside world as possible if you're for example making a character controller script and you want to have a variable for the movement speed you might be tempted to make it public just so that you can mess with it in the inspector but this brings in a lot of problems because making it public makes it well public to all other scripts and it means that they can modify it later in development this could lead to some really weird behavior as your movement speed might start changing around without you really knowing why that happens that then in turn leads some wasted time debugging and all in all it would have been better to just leave it as b on private the solution to this problem is to use serialized field let me show you this i'll make a private integer and call it private bar and public and public bar okay so when i go into unity we'll of course be able to see in the public variable and not the private variable however if i now go in here and add serialize field and save we will be able to see both of them so this neatly solves that problem now hiding inspector practically works the other way around so where serialized field makes private variables show up in the inspector titan inspector hides public variables from the inspector so as you can see here now we can only see the private variable this is quite useful if we have public variables that only are relevant at runtime that is it isn't something that we should mess around with out of game another thing i want to point out is that clearlies field only works on types that already are supported by the inspector that is something that would work when it's public can now be made to work when it's private but for example a property even though it's public will not work in the inspector as you'll see here it doesn't show up even if i go in and write serialize field it will not change anything so the same is the case when using interfaces and stuff like that serialize field cannot make something show up that wouldn't otherwise show up now i use serialized fields so often that i like putting it on the same line as the code itself to save some space this is just completely down to preference and you can do it however you want now i want to show you the range attribute so let's imagine that we have a public integer called degrees and we want this to show some information about the rotation of an object there's no point in this number going below zero or above 359 because then the same number could just be represented with the number within that range so to enforce this we can use the range attribute we can write a minimum and a maximum number for the variable below so if i for example here write 0 and 359 and save and go back in here we will see the variable appear on this little slider so i can drag it from 0 and all the way up to 359 but not beyond if i try to edit this number to something higher and click enter it will simply just jump back down this is also a good opportunity to show you that these attributes only actually affect how the inspector deals with them if i were to go in here and in the start method write that degrees should be 400 and then head in here and click play you'll see that it actually jumps up to 400 it goes completely off the slider that is because the attribute only limits what you can do in the inspector it does not limit what the variable actually is now if i try to change this to 401 for example it will jump back down but it does not limit what i can do with script if you want to limit the variable itself you should use properties okay let's move on to a different kind of attributes some that are related to the layout so let's imagine that i'm making a bit more complicated script like a character controller here i want to have a lot of different variables for example a reference to the rigid body or b maybe a reference to the collider layer lighter let's say i want to have some floats here for maximum speed acceleration let's also do a jump height and a gravity okay so there's nothing inherently wrong in this if we go into unity we'll of course be able to see all of them and it works completely fine however if we have a lot of variables like this it can start look a bit messy and clumped up and it can be difficult to find exactly what it is you need this is where it can be great to start using these layout attributes for example we might say that these two are fundamentally different from these two and you're only really going to need these when setting up the game objects whereas these four are something that you need to tweak all the time so we could split them up and we do that with the header attribute so i'll write header here and this allows us to write a title i'll call this setup and down here i'll make another one called movements and just for clarity i'll also put a little space in here so in going into unity we will now see these titles in bold and a little spacing in between so they're clearly separated and it becomes a lot easier to find the things that you need we might also determine that while these four are all part of the movement they're a bit different because these two deal with movement speed and these two here deal with jumping so we could represent that by adding a space in here so we'll see that that just adds a little spacing in here as we might expect the third attribute i want to show in this context isn't that much about layout but it's kind of in the same spirit let's say that we have a designer working on the game here and they think well the movement is a bit weird and i want to tweak the values a little bit and he goes in here and starts playing around and then he sees gravity what the hell does gravity mean at this point he could open the code and hope that you've written some comment in here but that takes time and is bothersome what you can do instead to make his job a lot easier and faster is to write a tooltip so as you see we can open here and make a string gravity is the acceleration applied to the character so if i save this and go into unity i'll be able to see this message when i just hover over gravity here this can save you from so many misunderstandings and especially if you're making something like an api you definitely need to have things like this okay enough about variable attributes let me show you a couple that are used on classes let's say that we hear in this character movement script don't want to have to assign the rigid body manually we're a bit lazy so we'll just make this private and then make an awake method here and say that rigidbody equals getcomponent rigidbody just like that so at the start of the game it will be assigned this will work completely fine if we remember to actually add a rigidbody to the game object if we haven't we'll get an error and we'll see okay it's complaining about that we'll go in at the rigid body and now it's working but that is some time wasted and we don't like wasting time so what we can do is go up here and write require components and you can see that it describes this as the required component attribute automatically adds required components as dependencies i'll show you what that means if we open parenthesis here and write type off and then the kind of component that we want so rigidbody and then save and head into unity it will now require that there is a rigid body on this game object it won't update immediately here because this component was already on but if we remove it and now add it again it will add the rigidbody too and if i try to remove the rigidbody it will give me an error here can't remove rigidbody because example depends on it so that is what was meant with dependencies now this ensures that we won't get errors from this and as such we save ourselves some time which is always great okay now for another example for the last attribute of the day let us imagine that we want to have a recipe so a recipe needs some ingredients so i'll make a class here which i'll call ingredients and an ingredient probably has a name and maybe an amount let's say we here want to have a single ingredient so i write public ingredients ingredients now we might expect that this will show up in unity and that we'll be able to modify it but when we go in here and say look we'll see that it actually doesn't why is that it is because ingredient is not actually supported by the inspector it cannot be serialized to fix this we need to apply the serializable attribute it lies in the system's namespace because it's not actually only for unity it has much wider capabilities than just making it show up in the inspector but we won't deal with that for now so using system and then we can write serializable and that is actually all we need to do it will now show up here perfectly like expected and we can write things that was all i wanted to show you this time there are many more attributes to use but these are the ones that i myself use the most and the purpose of this video was to introduce you to the concept not to make an exhaustive list i encourage you however to go take a look at the other ones i'll leave a link in the description to their page in the unity manual in the next video we'll learn how to make our own custom attributes to achieve things that we otherwise couldn't if you find yourself running into a problem multiple times over your game development career it can be a great idea to make a custom attribute for it as you'll be able to just copy it into future projects and use it there too if there's another particular subject you want to see covered in this series you can write it in the comments you can also head over to our discord server and suggest it there it's also a nice place to hang out and check out our games if you're into that kind of thing i look forward to seeing you there until then have a magnificent day [Music] you
Info
Channel: Otter Knight
Views: 740
Rating: undefined out of 5
Keywords: c#, coding, cs, csharp, custom editor, custom editor unity, editor, editor scripting, editor scripting unity, editor tool, game dev, game development, game engine, gui, how to, programming, script, scripting, tutorial, ui, unity, unity 3d, unity c#, unity cs, unity editor, unity script, unity scripting, unity tutorial, attributes, premade attributes, unity attributes, c# attributes, textarea, serializefield, hideininspector, range, serializable, requirecomponent, header, space, tooltip
Id: YwHtIvIXwq8
Channel Id: undefined
Length: 13min 46sec (826 seconds)
Published: Fri Apr 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.