How to make an EDITOR WINDOW in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Just a small note, Selection isn't a namespace, instead it is a class.

👍︎︎ 1 👤︎︎ u/messipro 📅︎︎ Jul 03 2017 🗫︎ replies
Captions
I think one of the most important features in unity is being able to extend the editor it's something that people often forget about especially beginners but being able to create your own tools to make game development easier it can just save you so much time this video will be an introduction to creating editor scripts and unity ok now an editor window and talk about how you can use it also huge thanks to Commander Firestone 38 for support over on patreon if you want to support the videos yourself you can do so at patreon.com slash brackets so let's get started to create an editor window we need to start in the same way when adding scripts to our game let's go to the project panel and hit create and that selects c-sharp script let's call this script example window let's hit enter and let's double click it to open it up in Visual Studio now first we want to delete the two using tags at the top here we won't be needing those and we can also go ahead and delete our two methods now normally when we code in unity everything that we need to use is inside of the unity engine namespace but when programming for the editor we have a separate namespace let's write using unity editor and of course we don't want to put the script on a game object instead we want unity to recognize it as an editor script to do that instead of writing monobehaviour we write editor window so now we should have all the functionality needed available to us what we then do is create a function will write void and this function is called on GUI we don't need any arguments here and let's just open and close our curly brackets so those of you who've been using unity before the new GUI system in 4.3 you might recognize this function that's because the on GUI method was used to draw UI in the game as well but now we pretty much only use it in the editor this is where we'll put all of our window code so if we want to add buttons to our window editable fields labels and even some functionality we'll put it in here so this is our window code but first of course we need to display the window to do that we create a separate function now this needs to be a public static void and let's call it show window again it won't take in any arguments and in here we want one of two things to happen if the window isn't already shown on the screen we want to create one and if it is we just want to focus on it but we don't actually need to check for this sort of stuff ourselves if we write editor window dot get window this method we'll actually open up a window for us and make sure not to create another one if one is already open all we need to do is specify the type of the window and that's gonna be our class name so in our case the type of window is going to be example window we can even go in here and give it a title if you don't specify a title it's just gonna use the class name which is fine but I just want to go ahead and shorten this to example note how it kind of grayed out the first part here that's because since we are deriving from editor window this function is available to us without needing to call the specific class and so we can actually just get rid of that so as you can see opening up a window is really easy but we're not currently calling this show window method anywhere so right now it's never gonna get opened we could call this from another script or we could make a menu item that would call this method and therefore show the window to do that we use a simple attribute attribute to use by using square brackets we then write menu item let's open and close the parentheses and here we simply write the item name we want ours to be under the window tab and we want it to be called example so now when we save this head into unity wait for it to load then go under window we can now see our example when we press this a new window called example opens up and we can go ahead and dock this anywhere in our editor but currently the window is totally blank let's go ahead and add some stuff to it remember this is done inside of the anchoring method first of all we can add a label by going GUI layout GUI layout is a class that has functioned for all sorts of stuff like drawing buttons labels spaces etc the cool thing is that it will automatically layout everything for us so if we now create a label we want the label to say something like this is a label and we can even give it a style so we can go in here and specify a editor style dot let's just choose bold label now when we save this go into unity and wait for it to reload we can see that it says this is a label in a bold fund of course this is very static we can also add a text field here to do that we first want to create a variable that is going to store our text let's create a string I'm just gonna call it my string and set it equal to hello world then in our on GUI method we write Eddie GUI layout dot text field we then give the text field a label so that the user knows what to input here in our case we could put in something like name and we then feed the variable that we want the text field to display in our case that's going to be my string and then finally of course we'll set my string equal to the result of this text field that we're both displaying the string and updating it now you might notice that in one place here I'm using GUI layout and in another one I'm using editor GUI layout well the distinction between the two is actually really vague some functions are only in one class and only in the other and sometimes the same function will be in both but they will look different as a rule of thumb though we use editor GUI layout whenever we want to edit fields and properties and we use GUI layout whenever we want to create labels spaces between properties and buttons but you really just have to get a feel of the two if you see the same function in both start by trying out the editor GUI layout after all it's made for this particular purpose now when we save this heading to unity we can see that we have a field here called name that is currently set to hello world I'm gonna set this to Dwayne Johnson just because you know he's cool now we can add labels and fields let's add a button to do that we write if GUI layout dot button and then we write the text for that button I'm just gonna write press me we then open and close some curly brackets and everything in here will be called if our button is pressed let's just throw a debug dialog statement here saying button was pressed let's say that go into unity we now see a button does appear called press me and when we press it button was pressed awesome but this is just showing off the technology let's try and apply this to a simple topic here I have three primitives I have a cube a sphere and a capsule and each one of these objects also have a material which is currently just a simple standard shader but they're all white say I had a ton of these objects and I wanted to be able to simply select them and then color all of the selected objects to another color well we could go and edit a million materials and try and find all the matching ones or we could make an editor extension so no example we know let's go in and rename our example to color riser let's do the same thing for a menu item call a riser let's delete our string and the text field for our string as well we can then change the label to color these selected objects and then exclamation mark we annotate the button to colorize instead of throwing out a debug deadlock well we want to go through and colorize stuff let's just delete that for now of course we want to be able to specify a color let's go to the top here and create a color and let's just call it color notice how this is the exact same process as when we were creating a editable string you know on GUI method after our label we can now go and call editor GUI layout dot color field again we want to give this a name we'll call it color and want to give it a color to show which is going to be our color variable and of course we also want to set our color variable equal to the result of this color field let's just save to see if these changes are applied you can see how the title of our window didn't change immediate deep let's just collapse that go window and now we have the colorizer and you can see that it's changed here as well and we have this color field here that we can edit in any way that we want but of course if we select a few objects here and hit colorize nothing currently happens so when we press the button we want to loop through all of the currently selected objects to do that we use selection under the selection namespace we have stuff like the active game object whether or not the selection changed and most importantly we have a variable called game objects this is simply an array of game objects with all of the currently selected objects as it says here this also includes prefabs non-modifiable objects and so this includes pretty much everything for now we won't worry too much about that so now that we have a way to get all of the currently selected game objects we can loop through it let's create a for each loop and for each game object and we'll call the game object we're currently looking at obj in our selection that gameobjects array well then we want to go ahead and set obj dot and to access the color here we first need to get the renderer component so we go get opponent of type renderer and if we just go ahead and access the material here directly we might get a case where we have object selected that don't have a renderer and so that would display an error let's instead store this in a temporary variable the variable is going to be of type renderer and let's just call it render off with annoying capital R then we can check if renderer is not equal to null so if we actually found a renderer on the object well then we can set renderer dot shared material dot color equal to our color variable and that should actually be it it's now for each object in our selected game objects we get the renderer and if it's not null well then we go ahead and change the color and of course all of this happens when we hit the colorize button to make this just a tiny bit cleaner let's go ahead and cut our code and wrap this in a separate function called colorize and let's create that right here void colorize no arguments and then simply paste all that code in there editor scripts can get really long and weird so it's a good idea to separate it out in multiple functions let's now save this go into unity try selecting say the cube and the sphere here and hitting colorize and there we go we can change to any color here and do any kind of selection and really easily we can colorize our game objects that's pretty much it for this video I have another video coming on the topic of extending the editor this one is going to be about custom inspectors which are just super useful so make sure to subscribe so you get notified when that comes out on that thanks for watching and I will see you in the next video thanks to all of the awesome patreon supporters who donated in June and especially hence Huff toon commander feinstone 38 wood goat yes pamekasan Thomas Varley stone gamer Sybok mommy Jason Lotito derek Eames Kirk face of Mara Phi 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: 257,712
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, material, materials, beginner, easy, how, to, howto, learn, course, tutorial, tutorials, fix, tip, game, development, develop, games, programming, coding, basic, basics, C#, editor, extension, window, tool, color, change, inspector, GUI, layout, GUILayout, EditorGUILayout
Id: 491TSNwXTIg
Channel Id: undefined
Length: 10min 49sec (649 seconds)
Published: Sun Jul 02 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.