Unity Editor Script Example: Master Tips and Code Snippets

Robert Jul 13, 2026

Delving into the world of Unity development often involves scripting to extend its capabilities. Unity's editor scripting, written in C#, allows you to create tools and conveniences that streamline your workflow within the Unity editor itself. Here, we'll explore a Unity editor script example that demonstrates how to create a simple menu in the Unity editor.

Unity Editor Scripting - What You Need to Know (Pt.1)
Unity Editor Scripting - What You Need to Know (Pt.1)

Before we dive into the script, it's essential to understand that Unity editor scripts are not executed within the game but rather in the editor environment. They are useful for creating custom windows, tools, and inspectors to enhance your development experience.

Unity 2D Basics Cheat Sheet
Unity 2D Basics Cheat Sheet

Setting up the Project and Environment

To get started, create a new Unity project and import the following namespaces to your script: `UnityEditor` and `UnityEngine`. These namespaces provide the necessary classes and methods for editor scripting.

Unity Visual Scripting
Unity Visual Scripting

Also, ensure that your script is placed in the `Editor` folder within your project's `Assets` directory. Unity automatically detects scripts in this folder and allows them to be executed in the editor.

Creating a Custom Editor Window

an image of a computer screen with the text'unifying button click event c script '
an image of a computer screen with the text'unifying button click event c script '

A common use case for Unity editor scripts is creating custom windows. Let's create a simple window titled "My Custom Window" with a "Show Details" toggle button.

Creating the Script

Create a new C# script and name it `MyCustomWindow`. Import the necessary namespaces at the top of the script:

Unity Visual Scripting Tutorial : FlowCanvas
Unity Visual Scripting Tutorial : FlowCanvas

```csharp using UnityEditor; using UnityEngine; ```

Next, create a public bool variable to represent the toggle state:

```csharp public class MyCustomWindow : EditorWindow { public bool showDetails = false; ```

Implementing the OnGUI Method

The `OnGUI` method is where you draw the UI elements for your custom window. Here's how to implement it for our example:

Simple Player Movement Script in Unity (Beginner Guide)
Simple Player Movement Script in Unity (Beginner Guide)

```csharp public void OnGUI() { EditorGUILayout.LabelField("This is a custom window!", EditorStyles.boldLabel); showDetails = EditorGUILayout.Toggle("Show Details", showDetails); } ```

The `EditorGUILayout.LabelField` method displays a label with the text "This is a custom window!", and the `EditorGUILayout.Toggle` method creates a toggle button with the label "Show Details".

Now, let's create a method to display the window using a menu item:

Unity Inventory Script Tutorial
Unity Inventory Script Tutorial
TUTORIAL UNITY 5 - MENU PRINCIPAL (Unity 5.3 ou superior) (UI)
TUTORIAL UNITY 5 - MENU PRINCIPAL (Unity 5.3 ou superior) (UI)
Outline – The Journey Begins
Outline – The Journey Begins
an image of a document with the words script example and arrows pointing in different directions
an image of a document with the words script example and arrows pointing in different directions
Free Script - Attribute Connection Script Demo
Free Script - Attribute Connection Script Demo
Unity Learn
Unity Learn
Unity Shader Graph: Displacement UV
Unity Shader Graph: Displacement UV
Pre-Production Tips: The Basics of Breaking Down a Script
Pre-Production Tips: The Basics of Breaking Down a Script
Write Better Scripts: 30-Year Expert AI Prompt
Write Better Scripts: 30-Year Expert AI Prompt
Yik Chan - Yik Chan added a new photo.
Yik Chan - Yik Chan added a new photo.
an old typewriter with writing on it and some words written in black ink next to the
an old typewriter with writing on it and some words written in black ink next to the
Screenwriting 101: Script Elements
Screenwriting 101: Script Elements
YouTube Script Generation Prompt
YouTube Script Generation Prompt
Unity C# Editor Scripting Masterclass
Unity C# Editor Scripting Masterclass
a computer screen with an image of a person flying through the air in front of stars
a computer screen with an image of a person flying through the air in front of stars
an image of a computer screen with text on it
an image of a computer screen with text on it
Adding Textures to Unity 5.4.1
Adding Textures to Unity 5.4.1
Essential DaVinci Resolve Keyboard Shortcuts
Essential DaVinci Resolve Keyboard Shortcuts
a movie script with the title how to write a montage for a movie script
a movie script with the title how to write a montage for a movie script
Fundamentals of youtube scriptwriting
Fundamentals of youtube scriptwriting

Adding a Menu Item to Access the Window

Create a new method called `ShowMyWindow` inside the `MyCustomWindow` class:

```csharp [MenuItem("Examples/My Custom Window")] public static void ShowMyWindow() { GetWindow(); } ```

The `[MenuItem]` attribute associates the `ShowMyWindow` method with a menu item in the Unity editor. The menu item will appear under "Examples" in the "Window" menu. The `GetWindow` method displays the custom window.

Using the Custom Window

To use the custom window, save the script and go to the "Window" menu in Unity. Navigate to "Examples" and click "My Custom Window" to open the new window. Toggle the "Show Details" button to hide or show a detailed panel (currently, only a label is displayed).

Conclusion and Further Learning

Unity editor scripting is an essential skill for Unity developers, enabling the creation of custom tools and workflow optimizations. This simple example demonstrates how to create a custom window with a toggle button. Explore the Unity documentation and experiments with more advanced editor scripting concepts like creating custom inspectors and tool windows to further enhance your development experience.

Delve into the vast world of Unity editor scripting today, and watch as your productivity soars with your new-found ability to create personalized tools tailored to your specific workflow. Happy scripting!