Custom Inspector - Only Serialize if true (LISTS AND MULTIPLE PER LINE) Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys and in this video i'm going to show you how to only serialize some variables if blank is equal to true for instance i have this enumeration and if it's type is equal to background only then i will show this backgrounds list and you can put in some elements in there it's pretty cool and i also have this three liner details variable where you have a string name an integer cost and a float for happiness all these cool things you can have you can just give it a name carl and whatnot so that's what we're going to do so why not let's just do it right now after this message what's up dude um you enjoying this content is this what you were looking for if so subscribe if not then still subscribe anyways let's get into it all right so i have this shop item game object it has a shop item class onto it and i have this enumeration called item type and in this item type you can just basically pick your item type so there's decoration food but for this video i'm going to be using background let's go ahead and open the script i forgot to change visual studio's color theme for this video i hope it's not too weird and you can see that we have one serialized field which is just serializing this enumeration right here so that you can pick what kind of type you just saw it and i have this list called backgrounds which is basically if the item type is equal to background only then i will serialize this backgrounds list which is a pretty cool thing to do and i also have these three variables that i want to serialize as well so to get started we'll just go ahead and create a class inside of this class that will customize this class's inspector so public class and i'll call it shop item editor because this is named shop item and this is the class that will edit the shop item and it will derive from editor but there's one problem we need to use the unity editor namespace so we'll just go ahead and do that now using unity editor editor and go ahead and drop our classes brackets down here and there's one more problem with this namespace it's not just cut and dry the thing is whenever you build your project and you're playing the game say on your phone or on a pc you're not really using the editor so it will throw errors and it will not build and to fix this all you have to do is say if unity editor only then we will declare this namespace here and then we can just go ahead and end if right here and just to be safe we can just copy this if unity editor and wrap our entire class in it and i'll just take this end if and copy it and wrap this class and then there's one more thing that i want to do is wrap this class in a region so region and i'll call it editor and then i'll end region right here and i do this so that whenever you want to only work on this class you can just fold this away and everything will be nice and clean for you all right so the next thing i want to do is create a custom editor so i'll just come above this editor class and say custom editor and we need to pass in a type so this is going to be type of the class that we are editing and it is called shop item so we'll just pass in shop item and now we will come down here and create an override on the inspector so public override void on inspector gui pretty self-explanatory and if you hit tab you'll get this line right here basically what this is is it just draws all of your default variables so this item type it basically draws all that for us and what we want to do is serialize after this base is drawn because if you come in here and put the base below everything that you're serializing then what will happen all of your variables will be drawn even above your class so you want to make sure that this line is executed first it doesn't really make much of a difference but it's cleaner so now i want to create a label field so that we can put all of our details under the label field so to do that i'll just go editor gui layout dot label field pretty self-explanatory and all we have to do is pass in a string so for me that's the details close that off and if we save and jump back into unity you can see just like that that easy we are already customizing our inspector pretty awesome so now i just want to make a little space in between this variable pretty self-explanatory editor gui layout dot space there you go wow that was so hard jump back into unity there you go there's a space now the next thing that we need to do is for us to serialize all of this on the same line we will just have to begin a horizontal layout group and then end it and all we have to do is edit our gui layout dot begin horizontal and then here we'll just editor gui layout dot end horizontal that was tough guys basically all this is is anything you serialize in between these two lines of code will be drawn on the same line but now we need a reference to this shop item class so below this base i'll just create a ship i'll just create a shop item named shop item and we'll cast it into a shop item and mark it as target so that that is the target class that we are editing and now all we have to do is come in here and say shop item dot item name is equal to editor gui layout and since this is a string we will need a text field to edit the string so we just have to simply go text field let's yup just like that so you guys once you get the hang of creating your own custom inspector everything will get a lot more simpler it may look really confusing right now because before i ever did any of this i never seen this attribute i never seen any class deriving from editor i never seen inspector gui i never really seen editor gui layout all of this was pretty new to me i'm pretty sure it's pretty new for you too if you haven't seen it before but as long as you use it a little bit kind of get comfortable with it it's going to get a lot easier so for this text field we have to pass in a value and of course this value is the item name so we'll just go ahead and go shop item dot item name and just like that and we also need to serialize the cost and happiness so i'll just go ahead and take this line and copy and paste paste and go cost and since the cost is an integer we'll go ahead and cast it into an int field instead and pass in the cost instead of the item name and do the same thing for the happiness but this time it is a float field because it is a float and passing the happiness here now if we go into unity just like that we got all these details like guys i cannot tell you i wish i knew this sooner many many times i wanted a cleaner inspector i wanted to serialize three things on one line since this is a background i'll just call it coral it costs maybe 100 coins and gives like 2.5 happiness all that good stuff and now what you can do is put a label before each variable we'll just go in here and same thing label field just inside of this horizontal layout group just drop some labels and i'll separate them a little bit just to make it a little easier to read and instead of details this will be cost this will be um no no does not cost that his name this is cost and this is happiness everything i feel all the time but there's a problem with this label fields are really wide you could see that the label field is so wide that it just pushed the variables so far away so we can fix that by giving it a max width so we'll just go gui layout again it's like kind of confusing what to call we have editor gui layout gui layout editor styles you'll get used to it so guy layout.max with is what i'm looking for and since the name is pretty short we'll just say it's 50 go ahead and copy this for the costs and the happiness and since the happiness is a lot longer we'll just give it 75 pixels jump back into unity and just like that we have a pretty clean way to serialize all of our details so with this done now we can go ahead and serialize our list now what i want to do is take all of this excluding so do not take this take all of this go ahead and extract the method and we'll just call it draw details and i do this so that our uninspector gui function is really clean i'll just get rid of this private i don't think that's necessary and now to draw a background there's three things that we will need to do first of all we need to create a size integer field so the developer can input a size for the list and then next we need to correct that size in our code and then once we corrected that size we can go ahead and serialize all the elements on that list this could be um confusing at first once you do it a couple times it'll be really easy but for the size create a list i do this so that my code is cleaner so i can just create a list here of game object and i'll just call this list so that it's easy to call and it's going to be equal to shop item dot backgrounds and i do this so that whenever i need access to this backgrounds list i don't have to go shop item dot every time now all i have to do is say list and now with that out of the way we'll just create an integer called size of course and it's going to be equal to max and then the minimum value that you could put in for the size of this list will be zero of course but the maximum size will be editor gui layout dot int field and here we will pass in the name of this end field which is of course size and then we need to pass in a value and the value is the list dot count so all the elements on the list and just like that you can see right here we have this size and you can input any size here so now let's go ahead and correct the size we'll say while size is more than list dot count meaning the developer just put in a size that is bigger than amount of elements on the list that just means we need to now start adding so we'll just go list dot add no and let's just go ahead and copy this and paste it and say while size is less than list account that just means we need to start removing from the list so we'll go remove and we don't want to just remove we want to remove add so we can specifically remove the last element on the list we need to pass in an integer so we'll just remove at list dot count minus one so we just basically remove the last element on the list now we can finally come in here and serialize all the elements on that list so to do this we'll just create a for loop and double press tab so that we don't have to write all this yabba jabba and while i is less than list dot count the list at the index of i is equal to editor gui layout dot object field obviously because we are serializing an object and here we need to pass in the name of the object field element space make sure you put a space in there plus i and then next what we are going to serialize and that is obviously list at the index of i do we need to pass in what type we want this object field to be of so it's going to be the type of list that we are serializing so the list type is game object yours may be different so just put in your type mine is game object and then next is if you want to allow scene objects basically what that means whatever object is on the scene do you want to be able to assign those objects to that list since this is deriving from a mono behavior meaning it is on a game object we want to go ahead and allow this but if you have some kind of say scriptable object something that is just sitting here in your assets folder then in that case you would probably say false and we're still getting an error here that's because we need to cast this object field as a game object and this is actually all we needed to do we go into unity and we can input any size and it serializes all of these fields but one more thing we need to do is create this kind of fold out effect go back into our code and before we do all of this let's just create a fold out and this fold out requires a boolean so we'll go inside of our shop item class and create a bool and i'm going to call it show backgrounds because this dictates whether we will show the backgrounds list or not and its default value is going to be false so the list is going to be closed by default and we go back down here and say shop item dot show backgrounds is equal to editor gui again layout and this time it is a fold out and here we want to pass in the boolean do not come in here and say false or something like that thinking whenever you're gonna press it's gonna be true and false and it's gonna keep opening and closing no you need to pass in that show backgrounds variable again so show backgrounds and then next you need to pass in the label so it's going to be called backgrounds and we can go ahead and close that off but there's one more thing we can pass in as a parameter it's called toggle on label click now it's actually called toggle on label click but if you say false when you press on label in the inspector so if we press on the label here it's still going to be opening even though we said toggle on label click false and we're pressing the label and it's still toggling yet if we press on the arrow it does not toggle pretty weird so just imagine it says toggle on arrow click instead so do you want to toggle on arrow click yes that would be nice now we will only want to show all of these elements if this fold out is opened pretty easy to do we just have to go ahead and create an if statement if shop item dot show backgrounds only then we will do all of this so take all of this and put it inside of that if statement and i also want to put a space above so editor guy layout again space so that we have a space between our details that we drew and now we can open and close this list but there's also another thing that's different than their list they have an indent level we do not very easy to fix all we have to do is before we start serializing everything we just go editor gui dot indent level and notice i did not say editor gui layout this one's different so we're going to increase the indent level so we just say plus plus i'll take this line and once we're done serializing everything we'll just reset the indent level so we'll just come down here and say minus minus and just like that we're able to perfectly replicate a list now there's one more thing we need to do we only want to show this backgrounds list if the item type is equal to background so to fix that we just create another if statement and then come down here and above all of this even this folder right here will create an if statement saying if shop item dot item type is equal to item type dot background which is how you access the values in the numeration then only then we will do all of this so just put all of that inside of this if statement now the type is decoration and it's gone set it to background and it's back now of course you can do the same thing with a boolean or you had a bull to show the list and of course it doesn't show decorations list when we switch to decoration but the thing is you already know how to do that so all you have to do is say else if shop item dot item type is equal to item type dot decoration then you will serialize another list and to keep this on inspector gy clean we'll just take all of this and extract the method yet again and we'll call it draw lists so that's basically it for this video i thank you guys for watching i hope it helped you out if it did be sure to subscribe if you haven't and like the video the thing is whenever i was trying to do this on my own there was a lot of scattered information out there i had to go um just a bunch of different tutorials it took a few hours finally got it down and the thing is i was kind of inspired to make this tutorial because there isn't really a proper tutorial on youtube showing exactly how to do this so again thank you guys for watching and i'll see you next time
Info
Channel: Kap Koder
Views: 7,551
Rating: undefined out of 5
Keywords: Custom inspector, only serialize, if true, multiple variables, per line, editor, unity editor, gui, list, enum, tutorial, unity, c sharp, c#
Id: RImM7XYdeAc
Channel Id: undefined
Length: 18min 23sec (1103 seconds)
Published: Tue Aug 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.