Imagine you are doing a great masterpiece A masterpiece so big not even Picasso himself would be able to compete against you. After all the sweat and tears that fell from you, you manage to finish it. You've got, art. But, there is a slight problem. You have no way or place to show that art. Because of that, no one is going to see
it now. All that hard work for nothing. Thankfully, a kind human name Unity saw your art and wanted to make it part of its Museum. Overjoyed, you kindly accepted the offer and your great art is now displaying at its Museum. Together with other people's art. Everyone can see your art now, and what's best, is that your art has its own display area. And to that display area, we call, an Editor Window. In Unity, we have two ways of creating our Editor Window. One is automatically and the other one is manually. Unity provides us with an option to create an Editor Window. If we right click in our project window, and go to Create > UI Toolkit (at the bottom), we see that there's an option called Editor Window. Clicking on it makes a window pop up
and allows us to create three different file types We are only going to need a C# script for now so uncheck the other two. Name it whatever you like but keep in mind that if you name it EditorWindow, you will need to specify the EditorWindow namespace on the
script inheritance, as Unity doesn't automatically update that for you. I'll name mine DSEditorWindow and when that's done I'll press Confirm. It should add it to the Editor Folder right
away, so open it up and create two extra folders. The first one I'll name DialogueSystem. And inside of that folder, I'll create another one named Windows. And move the DSEditorWindow script there. Opening it up, we see that there's some default code that creates an Editor Window already. I'll make sure to update my namespace here as Unity doesn't insert the default namespace in this script template. I'll also add it to the Windows subnamespace. Here we are not going to need the CreateGUI method so feel free to remove that one. In our show window example method we simply update the DSEditorWindow text to be "Dialogue Graph" instead. This is the title of the Editor Window. In my version, the GetWindow method already uses this "titleContent = new GUIContent()" when passing in the title as a parameter, so I'll send the title as a parameter
instead and remove the variable declaration. In our MenuItem attribute we can set the path as we'd like for the menu that opens this window. I'll leave the first menu to be "Window" so that it goes inside the already existing Window menu. And give my custom path of "DS" and "Dialogue Graph" for the last item. Feel free to remove whatever namespace we are not using right now as we are going to import whatever we need by the time we need it. Our Editor Window can now be opened through our custom menu path and if you want to see the title updated you'll need to reopen the window. That's all you need for the automatic creation so feel free to skip to the next video if you don't care about doing it manually. You will probably only want to do it manually if the automatic option is no longer available for you. And really, what you'll need to do is to simply achieve the same code that the automatic script has. But for those that prefer to go step by step this is the part of the video for you. In the automatic way, we've already made these two folders so if you haven't watched it feel free to create them inside the Editor Folder. In the Windows folder simply right click in the project window and create a new C# script. Which I'll name DSEditorWindow. Opening it up we have our very basic and common MonoBehavior script. Feel free to remove those methods as we are not going to
use them. We want this script to be an Editor Window so what we gotta do is swap the MonoBehavior inheritance to EditorWindow instead. If Unity doesn't recognize the EditorWindow class, that simply means its namespace is not imported. If you are using Visual Studio like me we can import it by having our cursor near the class or in the line, and press alt + enter. And then you should see "using UnityEditor" in the options If your code editor doesn't support this you'll have to import it by typing it out at the top. Of course, I'll go ahead and make this part of my Windows subnamespace here. Inside of our script there is only one thing we will need for now, and that's a way for us to open our Editor Window. We can start by doing that by creating a method that will help us out doing that. So I'll go ahead and type in "public void" and I'll name it "Open", but you can name it whatever you'd like as long as it's not a reserved name. And inside this method I'll type in a single Debug.Log saying "Opening!". We have now made the method, but we still need a way to call this method. And thankfully, Unity makes that easy for us by having an attribute named "MenuItem". A Menu Item is simply one of those menu items you see in your Unity top bar. And we can make our own item show up there. So let's go ahead and on top of our
method type in [MenuItem]. And here we need to pass a parameter, which is the path of the menu item. If we want our own menu, we type in our own menu name in the first name of the path. If we want it to be inside an already existing menu, we simply type in an existing menu name as the first name of the path. I'll go ahead and make it part of the Window menu and make mine a sub-menu. So I'll type in "Window/DS (Short for Dialogue System)/Dialogue Graph". If we save and go back to Unity, we see that we have an error. That's because the MenuItem attribute requires our method to be static for it work. So let's go ahead and make our Open method static for that. Saving again we should no longer have an error, unless you didn't clean the previous one. And opening our Editor Window through the menu should show the Debug.Log we've typed in. Of course, we want the Window to open and not for a Debug.Log to happen, so let's go back to our Editor Window script and remove that Debug.Log we typed in earlier. In here, we want a way to open up our Window. And thankfully for us again, Unity EditorWindow class has a method named GetWindow. This method will try to get the window of a type we have specified and open it up, or, if it doesn't find it it, it creates a new one. We now have our method that opens up our Window, and that works fine. But we might want to update our Window title and we currently have three ways of doing that. The first one might depend on your version because it is currently deprecated. That's by assigning the GetWindow return value to a variable and then update its title using its "title" variable. Here, you would send a string with the name of the Window, however, I won't do that because I won't be using this one, as it says it's deprecated. The current way of doing it is by using the "titleContent" variable instead. This accepts a GUIContent parameter instead of a string, which seems to be simply something that accepts both a text and an icon. This is the way we are going to do it. However, we will do that by using in the third way possible. Our GetWindow method accepts a parameter as well, and that's our title. By going to Unity GitHub, in my current version they seem to do it with the "titleContent" already. Basically, exactly the same as we have it here. So by simply passing in the text we want to the method, we do
the same as our previous "titleContent" assignment. If we save it and go back to Unity, we should have our Editor Window title update when we re-open the Window.