How to make a CUSTOM INSPECTOR in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This video helped me. Thank you!! My only critique is naming your scripts "Cube" and "Sphere" was kind of confusing.

👍︎︎ 1 👤︎︎ u/Gode14 📅︎︎ Jul 11 2017 🗫︎ replies
Captions
custom inspectors and unity is one of those tools that can just make developments so much easier we'll have a look at a few practical use cases for custom inspectors but really the possibilities here are endless so let's get into it so here's the basic thing with only a camera a light and a cube and on this cube I have a script called cube as well if you open the script up you will see that it generates a random color it does this when the game starts so inside the start method we are calling the generate color method this is getting the renderer on our object accessing the material then setting the color equal to a random color we also have a function here for resetting the color this is getting the materials color and simply setting it back to white so if we go ahead and hit play each time we play our cube is going to change to a random color but in case it's like this hitting play every time you want to test out your script can be really annoying especially if you have a large game where it takes some time to play so let's write a custom inspector that will give us a button that we can press in order to generate a color to do that let's go and create another folder this folder can sit anywhere in your project but it must be named editor that's because any scripts inside of an editor folder won't be included in builds so let's right-click go create and let's create a c-sharp script we could call this something like cube editor let's then double click it to open it up in Visual Studio and first let's go ahead and delete the two using tags at the top let's also delete our start and update methods remember whenever we want to program for the unity editor we want to be using the unity editor namespace sub here let's write using unity editor and of course we don't want the script to sit on an object we want this to go and modify an already existing script therefore we don't want to derive from monobehaviour but instead from editor we also need to tell unity what script that we want this to be an editor for to do that we go up here and using an attribute we write custom editor then inside two parentheses we write type of then again we write parenthesis and in here we give it the script so in our case we want the custom inspector for our cube script and now we're actually ready to start writing our inspector now 99% of the code that you're going to be writing when doing custom inspectors going to be inside of the on inspector GUI function to create this function rewrite public override void and then on inspector GUI and you will notice how I just used autocomplete there this of course went ahead and rode on Spector GUI of Renta sees are open and closed brackets but it also inserted a line into our function that's because of this keyword right here the override what this means is that we're actually taking a function that unity has already created called on inspector GUI and unity uses this function to display all of the variables that are currently inside of our inspector so for example you have a public float you're going to have a field that allows you to edit that in the inspector and that's what uninspected GUI allows you to do so upon overriding this we're actually going to be ignoring all of the stuff that you needed does for us except however if we call the original on inspector GUI that's what we're doing here base that on inspector GUI goes back and says hey do all of the original GUI code and then allow us to add stuff before that or after that so whenever you want all the default stuff as well as adding something on top leave this in here and if you want to do it completely by yourself simply get rid of it but we'll just leave it in here in case we decide to add variables later on then after this we want to add of course a button well if you have used unity before the GUI system in 4.3 you might recognize this function if we go if GUI layout GUI layout is a class that allows us to do all sort of stuff with UI we can do labels spaces and most importantly for us we can do buttons then we open a close of parentheses and then inside of these two parentheses we can write a title for our button in our case let's call it generate colors then of course we want to open and close some curly brackets for our if statement and the cool thing about this function is that it will return true when the button is pressed and so everything inside of these two curly brackets will only happen if we press the button let's try and write debug that lock here saying we pressed generate color let's try and save this head into unity let's wait for the editor to load and when we now click on the cube we see that we have a custom button when we press on it we get a debug that large statement saying we pressed generate color and our game isn't even running so now you might start to see kind of how easy it is to add functionality into our editor but currently we aren't really communicating with our original script we want from the cube editor to call a function called generate color inside of our cube script to do that we need a reference to the cue locally whenever you derive from editor unity gives you a very simple way to do this we simply use a variable called target as it says here the target is the object currently being inspected and since this is a custom editor for the cube and ever only will show when we're inspecting the cube this is always going to refer to the cube so now we can actually go and store this in a variable so let's create a cube variable let's call it cube the non-capital C and set it equal to that target this will however give you a red line and that's because target is by default of type object so we simply have to cast this meaning change its type into a cube and we do that like this so now we can access all of the stuff inside of our cube so instead of throwing out the debug the log message let's go cube dot generate color remember to mark any function or property that you want to edit through your inspector script as public otherwise you aren't going to be able to see it this works in the exact same way that it would between scripts in game so if we now go into unity and hit the generate color button you can see just how many different colors we can get without even playing the game of course you might also want a reset button in here to do that we use the exact same syntax in fact I'm just going to copy the entire if statement let's now call our button reset and instead of calling the generated color method that's called the reset method and now we have a reset button as well I think it would be a lot prettier if our script had these two buttons side by side to do that that's going to our editor script before the first button we go and write GUI layout dot picking horizontal and after both our button we write GUI layout dot and horizontal so what we're saying here is that everything within the begin horizontal and end horizontal methods well are not going to be laid out vertically but instead horizontally and so these two buttons will show side-by-side let's increment our code to show that clearly let's now say that heading to the inspector and wallah and of course they work as intended but that's that's more that you can do with the custom inspector than just adding buttons here I have another simple scene with the sphere and the sphere has a sphere script inside the script we animate its size based on a sine wave and you can see us changing the local scale here you don't have to understand this code but basically we have a base size from which we apply animation on top so if I hit play you can see that we get this heartbeat like animation I'm going to increase our base size or decrease it we can control the overall size of our sphere but inside of our editor when we increase and decrease our base size it has no impact on our scale let's go ahead and change that to do that let's go and create another editor folder you could also use the original folder and put both scripts in there but I'm just going to put it in here let's right-click go create three sharp script let's call this the sphere editor let's open it up again let's delete our two system tags let's delete our two functions let's use unity editor and let's derive from editor let's make this a custom inspector type of let's input our sphere here now let's again create a public oval write void and the method we want to override here is the on inspector GUI and again we're calling based on inspector GUI now if we save this everything should be the exact same way it was before however we can now make sure that every time we update our inspector we're also going to be changing the size this way we can see the physical change in the editor to do that let's get a reference to our sphere again by using the target variable let's create a variable of type sphere let's call it sphere as well and let's set it equal to our target and remember we always have to cast this into the type of our component in our case that's fear after this we can set the scale of our sphere by going sphere that transform dot local scale and set it equal to vector 3.1 multiplied with and this is where we want to input our base size and so because that variable is public we simply go sphere dot based size if we now save this head into unity and drag on our base size we can see it updating in our game and seeing you and this is actually fairly efficient of course our calculation is super simple but on inspector GUI is also fairly smart because it will only go ahead and update a local scale when something on the GUI has changed it's not like update that runs as often as possible so that works really well for us here but changing the variable like this feels pretty non-intuitive let's instead add a slider to do that we add some space in here and to create a slider we go editor GUI layout as we also talked about in my video on creating a custom editor window GUI layout and editor GUI layout is similar in a whole lot of ways they even have some of the same functions but in general we use editor GUI layout whenever we want to create some kind of editable field in our case we want to go ahead and create a slider and the value that we want to edit here is sphere dot based size and we want it to go between say point 1 and 2 of course we also need to make sure that we actually set Speier dot base size equal to this slider some people find it weird that we need to feed in base size twice that's because this is what this slider should show and this is where we give it the result of our slider that's kind of how that works if we now save this we'll see a few problems well we do in fact have our slide in here and we can definitely tweak it but we also have our original base size variable and that's really nothing indicating what this slider is doing to make our original variable disappear we simply need to delete or base that on inspector GUI call let's simply delete that part of the code and to give it a label we feed it one more argument in the very beginning here let's just call it size and now we should see that there are no distracting variables and that our slider is correctly labeled we can even go super fancy and create a small explanation for our script let's go GUI layout dot label and the label that we want to add here is something like other dates around a base size now in the editor we can see that label awesome that's pretty much it for this video if you enjoyed it make sure to subscribe so don't miss the future one also if you haven't seen it already I have another video on editing extensions this one's about creating windows in the editor on that thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in June and a special thanks to hence off-tune commander firestone 38 will go yes pamita's new thomas volley stone gamer sidewalk mommy Jason the Tito Derrick Eames Kirk Fraser mattify and Peter Locke if your name is not on the list I will make sure to include it in videos later this month and the next month as well thanks a lot guys
Info
Channel: Brackeys
Views: 197,331
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, material, materials, beginner, easy, how, to, howto, learn, course, tutorial, tutorials, fix, game, development, develop, games, programming, coding, basic, basics, C#, custom, inspector, editor, tool, extension, tools, creation
Id: RInUu1_8aGw
Channel Id: undefined
Length: 11min 47sec (707 seconds)
Published: Sun Jul 09 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.