 Howdy guys, IndiePixel here, and what I wanted to do was continue the Unity tools series and In this video or at least in this little section what we're going to do is we're going to build an object Replacer tool. Okay. Now, these are one of your staple tools that you use all the time in level designer any sort of like Scene development. You always want to replace one object with another. So we're going to do a basic example of that Just so you can get more comfortable with using Unity's editor API So what I'm going to do is come in here and create a new folder, and I'm going to call this the object replace Video because I've already got my other version going here And in here what I want to do is create another folder called editor And the reason why we have to do that is because we're working on editor tools So any sort of editor tool has to go inside of this editor folder so Unity can pick it up Okay So what I want to do now is going to create a C sharp folder, and I'm going to give this a name of IP replace objects Editor. All right. So this is gonna house the core meat And I didn't want to do that. Let's go back to Unity there. This is gonna house the core meat of the editor code so The other thing that I like to do is I like to create menus for all of these types of tools. All right, you can see up here I have this menu option here and what it does is it launches the tool for me, so it opens up an editor window and To do that we need to take advantage of the menu item attribute inside of Unity But I don't necessarily want to put that inside of this particular script So what I usually like to do is I create another script right next to it And I just call it the same thing but with the menu Suffix there Okay, so that way I know where all my menu code is and where I'm only editor code is just a way to keep it nice And clean and separate it out. That way you don't end up with these Editor scripts that have all the menus in a minute becomes really hard to organize all that You will see that as we move along here So what I'm going to do is pop open the replace objects menu here. All right And I'm going to get rid of my previous ones there Okay, and what we want to do is we want to come in here and I always like to put My code inside of namespaces. All right, so if you're not familiar with namespaces These are ways to categorize your code basically and to keep it protected from other Classes that might have the same name Okay, so I put it underneath the indie pixel that tools think of it as a folder So it's indie pixel and I don't have a sub folder called tools and then all this code is going to fit inside That particular folder. It's a good way to kind of visualize it in your head All right, so what we're going to do is we are going to make a menu item first All right, because this is going to be that menu item that drop down that I just showed that allows us to launch the tool and in order to do that we need to be using the Unity editor namespace like so, okay And then from there we have all the Particular code that we need we don't need this to be a mono behavior either. So what we can do is we can Start typing out the attributes. So we use the square brackets for that And we say menu item and then we give it a path. All right, so this path will be indie pixel And we'll do In this case I already have a scene tools So I want to make a little bit different. So I'm just going to call it level tools Again, you can make it whatever you like. Okay And then I'm going to call this replace objects Or replace selected objects. How about that? It's a little better there All right, and so with that we actually have a menu item The last thing that we need to do is we need to associate a method with this right because This particular attribute is going to be looking for The method right below it to launch. All right, so what we're going to do is we're going to declare a public static void And we're going to call this method replace selected objects All right, so there we go And what I want to do now is just put down a debug log just so I can see that this is working So if this all worked, what should happen is whatever I type into here Whatever I put in as a string here Is going to be displayed in the console inside of Unity. So I'm just going to say replacing selected objects Always got to put the exclamation marks in there Makes it seem more dramatic All right, so what I'm going to do is wait for it to compile And if we did our job right, we do have a level tools and we do now have a replace selected Objects option in here. So if I were to go to a console here All right And come up to indy pixel and say replace selected objects We get A nice little log to the console. So we know that that method is working So what we need to do now is get the window to launch. So I'm going to open up the editor script here Okay, and I'm going to put it underneath that same namespace Indy pixel tools because I'll put all my tools under here basically And I am going to get rid of these functions because they are not valid in this particular Place and what we need to do is we also need to utilize the Using Unity editor namespace that way we have access to all the editor api scripts and methods and classes and Everything like that. So All right. So with that in place All we need to do now is turn this into an editor window because I want it to pop up a open a window And to do that we need to inherit from the editor window Class and this will give us a lot of built-in functionality to handle displaying a window inside of unity Okay, cool. So from here what I usually do is I just lay out my region So I'm just going to put in a variable regions Or region Like so and then I do the built-in methods All right, because we're going to be using some of those built-in methods that are inside of the editor class Okay, so I'm going to say end region And then we're going to Do a custom methods region Yep, and then we want to close it. All right, so First thing we need to do we need to make sure that we launch the window All right, so when we go and press This particular menu option right here We're going to fire off this function right here and then whatever code is inside of that particular method is going to get Executed so what we want to do is we want to launch the editor window like so so to do that What we need to do is we need to provide a public static void And we're going to call this method launch Editor like so All right cool And that is looking pretty good. I'm getting some odd We have n region. Oh, there it is. That's what it is Forgot to end the region. There we go. Okay, so now we are Calling this particular method. We're actually haven't called it yet. We should come back to our menu now and just say ip replace objects editor dot launch Editor because we made it static we can access it now keep this in mind this class isn't static just the method inside of it So we can call that cool So from here what we want to do is we want to Create the window and this is actually pretty easy. It's gotten a lot easier over the years Inside of unity. So all we need to do is just declare a generic variable and I'm going to call this editor Win all right, and we're going to say get window now this particular method Comes with this editor window Class and I want to give it a type So we're going to say type I should do it lower lowercase Actually, I just need to give it the Type that we're looking for so it's going to get window and of type replace objects editor All right, and now you'll notice that this particular method has a bunch of overrides. All right, so we have nine overrides so each one of these Will change the functionality of your particular Window and the thing that we are really interested right now because I'm still trying to keep all this stuff basic All right, just so we get comfortable with this is the title. So I just want to give it a a different title So I'm going to say replace objects And let's capitalize that for professionalism cool All right, so with that we have now a window, but it's just being stored in reference It's just being stored inside of this particular variable We actually need to do one more step to actually display it to the screen. Okay, so we need to say editor win dot show Cool So that gets everything up and running. So let's go back to unity and test this out again All right, so I'm going to come up to any pixel Level tools and replace selected objects and voila. We now have a window that has our title It's getting cut off too, which I never really liked. I wish it would just resize, but it doesn't so What we're going to do now is we're going to just get the basics of the ui going So I just want to get something stubbed in just so I know that the ui is working as well Okay, so let's close this down for now And jump back into visual studio here and what we're going to do now whenever this particular window is shown All right What we can do is we can utilize the on gooey Method that's built in all right And this on gooey method is going to be the method that controls all the ui for our particular editor window So what we can do right off the bat is we can uh, draw a label to it. So I'm going to say editor gooey layout dot label field All right, and you can see that we have eight overrides for this one Okay, a bunch of ways to stylize and customize the look All I really want to do is give it a label So I'm just going to give it a string. I'm going to call it my label All right, and then I want to give it a different look and in this case What we can do is we can utilize the built-in styles. So I'm going to say editor styles dot bold label and you can see all these different Styling features that come with it. So I'm going to say bold label And that just speaks at a bold label All right, so there we go now we have our first ui element in place for our tool So let's go back to unity and check it out one more time All right, so come up here level tools replace selected objects And there we go. We now have a my label Now one thing that we should do before I close out this particular video is we should Include the repaint Call all right so that method right there that comes with editor window and this will just help update the Editor ui as things change in the scene. All right, so this will just help us update it If you didn't put it in there wouldn't update until you clicked on the screen Okay, so this is just causing it to to update All right, so with that what I'm going to do is I'm going to Close out the video there and in the next video we're going to start working on the functionality So that when we have let's say a bunch of cubes in our scene What I can do is I'm going to sign a prefab to This particular window here and hit a button and all the cubes will get replaced with a sphere if they're selected Okay, thanks so much