• 08/20/13 05:43 PM
    Sign in to follow this  

    Scripting Custom Windows in Unity3D

    General and Gameplay Programming

    CaseyHardman
    Unity allows you to extend its interface by scripting your own custom windows, which will be draggable and re-sizable like the other windows in Unity (i.e. Project, Scene, Hierarchy, Inspector). I'm going to teach you the basics of making your own windows. You will need:
    • Unity3D
    • Something to edit scripts with. If you have Unity installed, you should at least have the built-in editor, UniSciTE, installed.
    • You should at least know the very basics of Unity and scripting with Unity.

    Creating a Window

    Let's start by getting our new window to show up. Unity makes this rather simple. Create a new project or open an existing project (whichever you prefer). In your Project window (by default, it's at the bottom of the screen), click the Create button and then click 'Folder'. You can also right-click anywhere in the Project window, hover your mouse over 'Create', and then click 'Folder'.
    [attachment=15639:CreateFolder.png]
    In order to script a window, you have to put your script files in a folder that's named "Editor". If it's not in a folder named Editor, it cannot access the classes and functions it needs to behave like a window. So name the new folder you created 'Editor'. If it's not already being renamed, click twice on the folder name to begin renaming it. Once the folder is properly named, right-click on it, select Create, and then the type of script you want to make (JavaScript, C#, or Boo). Name this new script "MyWindow". Open the script with your text editor. [note] JavaScript users may not know this: every script file in Unity is actually a class that extends the [tt]MonoBehaviour[/tt] class. Unity 'hides' the class declaration from you if you use JavaScript (but it doesn't do this for Boo or C#), but it lets you declare it yourself if you want to. [/note] We want our new script to inherit from the [tt]EditorWindow[/tt] class, not [tt]MonoBehaviour[/tt], because we don't need to place this script on GameObjects; we just need it for this window. Declare your class like this: [code] //C Sharp: using UnityEngine; //This should be in your script file already. using UnityEditor; //This won't be in your script file by default, so put it there. It lets us access all of Unity's methods and classes for making windows. public class MyWindow:EditorWindow //Name your class the same thing you named your script file, and make sure it inherits from EditorWindow instead of MonoBehaviour (it will inherit from MonoBehaviour by default!) { } //JavaScript: import UnityEditor; //This won't be in your script file by default, so put it there. It lets us access all of Unity's methods and classes for making windows. public class MyWindow extends EditorWindow //Name your class the same thing you named your script file, and make sure it extends EditorWindow instead of MonoBehaviour (it will extend MonoBehaviour by default!) { } [/code] Now we'll add an option to create our window. Put this function in your window class (the one named [tt]MyWindow[/tt] that we just made): [code] //C Sharp: [MenuItem ("Window/My Window")] //This is the drop-down menu in the Unity editor where the option to create the window will be added. public static void ShowWindow() //Don't change the name of the function { EditorWindow.GetWindow(); //If you disobeyed this article and named your class something else, replace the 'MyWindow' in this line with that name. } //JavaScript: @MenuItem("Window/My Window") //This is the place that the option to create the window will be added. static function ShowWindow() //Don't change the name of the function { EditorWindow.GetWindow(MyWindow); //If you disobeyed this article and named your class something else, replace the 'MyWindow' in this line with the actual name. } [/code] Now you can open your window by clicking on the 'Window' menu at the top of the screen, then finding 'My Window' and clicking on that. Your window won't have anything in it yet.

    Displaying Things in the Window

    Now that your window is showing up, let's get it to show things. This kind of code should go in the [tt]OnGUI()[/tt] function in your class. Usually, you'll be using editor functions that you can find in the [tt]EditorGUI[/tt] and [tt]EditorGUILayout[/tt] classes, which we have access to since we're inheriting from [tt]EditorWindow[/tt]. Let's make some fields that the user can input into. Here's the code, including all of the previous code: [code] //C Sharp: using UnityEngine; using UnityEditor; public class MyWindow:EditorWindow { //Let's declare some variables that we'll use later: public string textField = "Text"; public int intField = 2; void OnGUI() //This is where we'll put all of our code that the window should run { textField = EditorGUILayout.LabelField("Input some text:",textField); //This will display the first string, followed by a box that the user can type characters into intField = EditorGUILayout.IntField("Input some numbers:",intField); //This will display the first string, followed by a box that the user can type numbers into } //Displaying the Window: [MenuItem ("Window/My Window")] public static void ShowWindow() { EditorWindow.GetWindow(typeof(MyWindow)); } } //JavaScript: import UnityEditor; public class MyWindow extends EditorWindow { //Let's declare some variables that we'll use later: public var textField:String = "Text"; public var intField:int = 2; function OnGUI() //This is where we'll put all of our code that the window should run { textField = EditorGUILayout.TextField("Input some text:",textField) as String; //This will display the first string, followed by a box that the user can type characters into intField = EditorGUILayout.IntField("Input some numbers:",intField); //This will display the first string, followed by a box that the user can type numbers into } @MenuItem("Window/My Window") static function ShowWindow() { EditorWindow.GetWindow(MyWindow); } } [/code]

    Conclusion

    Now you're prepared to make your own windows in Unity. Once again, make sure you always place your window scripts in an 'Editor' folder, and be sure to inherit from [tt]EditorWindow[/tt] instead of [tt]MonoBehaviour[/tt]. Here are some miscellaneous notes about scripting Unity windows:
    • The [tt]Input.mousePosition[/tt] variable is not updated when the game is not playing, and is useless for editor windows. If you want to get the mouse position, you'll have to use [tt]Event.current.mousePosition[/tt] in an [tt]OnGUI()[/tt] function instead.
    • Variables that aren't [tt]public[/tt] will reset to their default values when your project refreshes. This means if you edit a script or add a new file to your Assets folder, every variable that is not public will revert back to what you declared it as. For example, if a variable is set to 2 by default, but during your use of the window, you set that variable to 5, then it would reset back to 2 every time the project refreshes.

    Article Update Log

    [b]May 10 2013[/b]: Initial release


      Report Article
    Sign in to follow this  


    User Feedback

    Create an account or sign in to leave a review

    You need to be a member in order to leave a review

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now


    Ocimum

    Report ·

      

    Share this review


    Link to review
    Regashi

    Report ·

      

    Share this review


    Link to review
    Ravklok

    Report ·

      

    Share this review


    Link to review
    incertia

    Report ·

      

    Share this review


    Link to review
    mousetail

    Report ·

      

    Share this review


    Link to review
    Ectara

    Report ·

      

    Share this review


    Link to review
    pizzafan

    Report ·

      

    Share this review


    Link to review
    jbadams

    Report ·

      

    Share this review


    Link to review