CodeBlanks Logo Icon CodeBlanks

Challenge 3: Editor menu

Write a tool method opened via MenuItem.

The MenuItem attribute

The [MenuItem] attribute adds a new row to the editor menu and runs that method when clicked. The method must be static because it is called without an object. The menu path, for example "Tools/Arac", decides where the tool sits in the menu, and the tool is written with the UnityEditor namespace.

[MenuItem("Tools/BasitArac")]
static void BasitArac()
{
    Debug.Log("Arac calisti");
}

This way development tools can be used straight from the menu.

Practice Simple menu tool

In the class EditorAraci, define the path Tools/BasitArac with [MenuItem]. Write a static method named BasitArac and in this method print the text "Arac calisti" with Debug.Log.

Starter code

Player.cs
using UnityEditor;
using UnityEngine;

public class EditorAraci
{
    // MenuItem ozelligi ile Tools/BasitArac yolunu tanimla
    // BasitArac adinda statik bir metod yaz
    // "Arac calisti" metnini yazdir
}

Sample solution

Player.cs
using UnityEditor;
using UnityEngine;

public class EditorAraci
{
    [MenuItem("Tools/BasitArac")]
    static void BasitArac()
    {
        Debug.Log("Arac calisti");
    }
}

Expected output

Arac calisti

Practice Scene check tool

In the class SahneAraci, define the path Tools/SahneKontrol with [MenuItem]. Write a static method named SahneKontrol and in this method print the text "Sahne hazir" with Debug.Log.

Starter code

Player.cs
using UnityEditor;
using UnityEngine;

public class SahneAraci
{
    // MenuItem ozelligi ile Tools/SahneKontrol yolunu tanimla
    // SahneKontrol adinda statik bir metod yaz
    // "Sahne hazir" metnini yazdir
}

Sample solution

Player.cs
using UnityEditor;
using UnityEngine;

public class SahneAraci
{
    [MenuItem("Tools/SahneKontrol")]
    static void SahneKontrol()
    {
        Debug.Log("Sahne hazir");
    }
}

Expected output

Sahne hazir

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 How must a method be defined for a [MenuItem] tool to work?

  • As a static method Correct answer
  • Inside the Update method
  • As a MonoBehaviour component
  • As a constructor

Explanation: The method called from the menu needs no instance, so it is defined as static.

2 The menu attribute

Question
    [____("Tools/Arac")]
Answer
    [MenuItem("Tools/Arac")]

Explanation: The attribute that adds a tool to the menu is MenuItem.

3 Editor tool terms

Term Meaning
MenuItem Attribute adding a method to the menu
Statik metod A method called without an object
Editör aracı A helper used during development
Tools yolu Menu row where the tool appears

All lessons in Expert