Skip to content Skip to sidebar Skip to footer

Widget HTML #1

Unity Editor Scripting - Beginner to Expert

Unity Editor Scripting - Beginner to Expert

This course will take you from beginner to expert in Unity Editor Scripting. Throughout the course we will build 9 custom editor tools hands-on that you can use ...

Enroll Now

Unity Editor scripting allows developers to customize the Unity Editor to suit their workflow and streamline game development processes. By leveraging the Unity Editor scripting API, you can create custom tools, automate repetitive tasks, and enhance the overall efficiency of your project. In this guide, we will take you from a beginner to an expert in Unity Editor scripting.

Getting Started: Understanding the Basics

Before diving into scripting, it's crucial to have a basic understanding of Unity and its interface. Unity Editor scripting primarily involves writing C# scripts that extend the functionality of the Unity Editor.

Setting Up Your Development Environment
  1. Install Unity: Ensure you have the latest version of Unity installed.
  2. IDE: Use an Integrated Development Environment (IDE) like Visual Studio or Rider for scripting.

Creating Your First Editor Script

To create an editor script, you need to place your script in a folder named "Editor". Unity automatically recognizes scripts in this folder as editor scripts.

  1. Create the "Editor" Folder: In your project's Assets directory, create a folder named "Editor".
  2. Create a Script: Inside the "Editor" folder, create a C# script. For example, MyEditorScript.cs.
csharp
using UnityEditor; using UnityEngine; public class MyEditorScript : EditorWindow { [MenuItem("Window/My Editor Script")] public static void ShowWindow() { GetWindow<MyEditorScript>("My Editor Script"); } void OnGUI() { GUILayout.Label("Hello, Editor Scripting!", EditorStyles.boldLabel); } }

This script creates a custom editor window with a simple label.

Understanding Key Components
  1. EditorWindow: This class represents a custom window in the Unity Editor.
  2. MenuItem: This attribute adds a menu item to the Unity Editor menu.
  3. OnGUI: This method is where you define the GUI layout and handle user input.

Intermediate Concepts: Building Useful Tools

As you become more comfortable with editor scripting, you can start building more complex tools.

Custom Inspectors

Custom inspectors allow you to customize the Inspector window for specific components.

  1. Create a Custom Inspector: Create a script that derives from Editor.
csharp
using UnityEditor; using UnityEngine; [CustomEditor(typeof(MyComponent))] public class MyComponentEditor : Editor { public override void OnInspectorGUI() { MyComponent myComponent = (MyComponent)target; myComponent.myValue = EditorGUILayout.IntField("My Value", myComponent.myValue); if (GUILayout.Button("Do Something")) { myComponent.DoSomething(); } // Draw default inspector DrawDefaultInspector(); } }
  1. Target Component: Ensure you have a component named MyComponent.
csharp
using UnityEngine; public class MyComponent : MonoBehaviour { public int myValue; public void DoSomething() { Debug.Log("Button Clicked!"); } }
Property Drawers

Property drawers allow you to create custom GUI for specific property types.

  1. Create a Property Drawer: Create a script that derives from PropertyDrawer.
csharp
using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(MyProperty))] public class MyPropertyDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label); var valueRect = new Rect(position.x, position.y, position.width, position.height); EditorGUI.PropertyField(valueRect, property.FindPropertyRelative("myValue"), GUIContent.none); EditorGUI.EndProperty(); } }
  1. Define the Property Class:
csharp
using UnityEngine; [System.Serializable] public class MyProperty { public int myValue; }
Editor Extensions

You can create more complex editor extensions by combining multiple editor scripts.

  1. Editor Utilities: Create utility functions to streamline your workflow.
csharp
using UnityEditor; using UnityEngine; public static class EditorUtilities { [MenuItem("Tools/Reset Player Prefs")] public static void ResetPlayerPrefs() { PlayerPrefs.DeleteAll(); Debug.Log("Player Prefs Reset"); } }
  1. Custom Windows with Multiple Tabs:
csharp
using UnityEditor; using UnityEngine; public class MultiTabWindow : EditorWindow { private int selectedTab = 0; private string[] tabNames = { "Tab1", "Tab2" }; [MenuItem("Window/Multi Tab Window")] public static void ShowWindow() { GetWindow<MultiTabWindow>("Multi Tab Window"); } void OnGUI() { selectedTab = GUILayout.Toolbar(selectedTab, tabNames); switch (selectedTab) { case 0: GUILayout.Label("Content for Tab 1"); break; case 1: GUILayout.Label("Content for Tab 2"); break; } } }

Advanced Techniques: Mastering Editor Scripting

As you progress, you can utilize advanced techniques to create highly customized and powerful tools.

ScriptableObjects for Editor Data

ScriptableObjects can store data and configurations for your custom tools.

  1. Create a ScriptableObject:
csharp
using UnityEngine; [CreateAssetMenu(fileName = "NewConfig", menuName = "Editor/Config")] public class EditorConfig : ScriptableObject { public string configName; public int configValue; }
  1. Using ScriptableObjects in Editor Scripts:
csharp
using UnityEditor; using UnityEngine; public class ConfigEditor : EditorWindow { private EditorConfig config; [MenuItem("Window/Config Editor")] public static void ShowWindow() { GetWindow<ConfigEditor>("Config Editor"); } void OnEnable() { config = AssetDatabase.LoadAssetAtPath<EditorConfig>("Assets/Editor/NewConfig.asset"); } void OnGUI() { if (config != null) { config.configName = EditorGUILayout.TextField("Config Name", config.configName); config.configValue = EditorGUILayout.IntField("Config Value", config.configValue); if (GUILayout.Button("Save")) { EditorUtility.SetDirty(config); AssetDatabase.SaveAssets(); } } else { GUILayout.Label("Config not found!"); } } }
Editor Coroutines

Implement coroutines in the editor to handle asynchronous tasks.

  1. Create Editor Coroutine Utility:
csharp
using System.Collections; using UnityEditor; using UnityEngine; public static class EditorCoroutineUtility { public static void StartEditorCoroutine(IEnumerator coroutine) { EditorApplication.update += () => { if (!coroutine.MoveNext()) EditorApplication.update -= null; }; } }
  1. Using Editor Coroutines:
csharp
using UnityEditor; using UnityEngine; public class CoroutineEditor : EditorWindow { [MenuItem("Window/Coroutine Editor")] public static void ShowWindow() { GetWindow<CoroutineEditor>("Coroutine Editor"); } void OnGUI() { if (GUILayout.Button("Start Coroutine")) { EditorCoroutineUtility.StartEditorCoroutine(TestCoroutine()); } } private IEnumerator TestCoroutine() { Debug.Log("Coroutine started"); yield return new EditorWaitForSeconds(2); Debug.Log("Coroutine ended"); } }
Advanced Property Drawers

Create complex property drawers with enhanced functionality.

  1. Custom Property Drawer with Multiple Fields:
csharp
using UnityEditor; using UnityEngine; [CustomPropertyDrawer(typeof(MyComplexProperty))] public class MyComplexPropertyDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); var firstFieldRect = new Rect(position.x, position.y, position.width / 2, position.height); var secondFieldRect = new Rect(position.x + position.width / 2, position.y, position.width / 2, position.height); EditorGUI.PropertyField(firstFieldRect, property.FindPropertyRelative("firstField"), GUIContent.none); EditorGUI.PropertyField(secondFieldRect, property.FindPropertyRelative("secondField"), GUIContent.none); EditorGUI.EndProperty(); } }
  1. Define the Complex Property Class:
csharp
using UnityEngine; [System.Serializable] public class MyComplexProperty { public int firstField; public float secondField; }

Conclusion

Unity Editor scripting is a powerful feature that can significantly enhance your development workflow. From creating simple custom windows to building complex tools, mastering editor scripting can lead to a more efficient and streamlined game development process. As you progress from a beginner to an expert, you will be able to create customized tools tailored to your specific needs, ultimately boosting productivity and enabling more creative freedom. Happy coding!