---
title: "编辑器扩展"
author: "Perrin Yong"
author_profile: https://www.pystone.net/profile/
published_by: "Perrin Yong"
canonical: https://www.pystone.net/notes/unity-editor-extension/
type: note
content_role: unspecified
visibility: public
id_stability: rename-stable
source_path: "10-计算机、信息技术与工程/05-游戏图形与运行时/Unity/编辑器扩展.md"
content_hash: ff32b140ec4b1399b1f2318b4279abe8f37ea5fdb394067b784caa5734389586
knowledge_version: 224c990773de.5fa8af6e39fa
site_commit: 224c990773de166d23a886306577dd90379529ce
notes_commit: 5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a
---
# 编辑器扩展
> 创建时间：2020/6/3 15:15

## 编辑器扩展

## 菜单项

### MenuItem

#### 常用的菜单那位置

  1. 整个Unity编辑器顶头的菜单栏；

  2. Inspector挂Component的；

  3. Hierarchy游戏物体节点树的；

  4. Project游戏资源文件的。

#### 验证菜单栏

为某个菜单项写一个bool方法。
第二个参数表示，是否为验证函数

```csharp
class Program
{
    static void Main(string[] args)
    {
        dynamic dyn = 1;
        object obj = 1;
        dyn = dyn + 3;
        //obj = obj + 3; 报错

        // Rest the mouse pointer over dyn and obj to see their
        // types at compile time.
        System.Console.WriteLine(dyn.GetType());
        System.Console.WriteLine(obj.GetType());
    }
}

```

#### 自定义快捷键

菜单名称+空格+快捷键
_g 按下字母g
修饰符: % Ctrl; # Shift; & Alt
LEFT、RIGHT、UP、DOWN、F1..F12、HOME、END、PGUP、PGDN

```csharp
class Program
{
    static void Main(string[] args)
    {
        dynamic dyn = 1;
        object obj = 1;
        dyn = dyn + 3;
        //obj = obj + 3; 报错

        // Rest the mouse pointer over dyn and obj to see their
        // types at compile time.
        System.Console.WriteLine(dyn.GetType());
        System.Console.WriteLine(obj.GetType());
    }
}

```

#### 优先级

默认为1000
差距为11以上分组

#### 给组件或脚本添加右键方法（Inspector）

传入参数 MenuCommand
command.context : 被右键点击的脚本对象

```csharp
class Program
{
    static void Main(string[] args)
    {
        dynamic dyn = 1;
        object obj = 1;
        dyn = dyn + 3;
        //obj = obj + 3; 报错

        // Rest the mouse pointer over dyn and obj to see their
        // types at compile time.
        System.Console.WriteLine(dyn.GetType());
        System.Console.WriteLine(obj.GetType());
    }
}

```

#### 其他位置

```csharp
class Program
{
    static void Main(string[] args)
    {
        dynamic dyn = 1;
        object obj = 1;
        dyn = dyn + 3;
        //obj = obj + 3; 报错

        // Rest the mouse pointer over dyn and obj to see their
        // types at compile time.
        System.Console.WriteLine(dyn.GetType());
        System.Console.WriteLine(obj.GetType());
    }
}

```

### ContextMenu & ContextMenuItem

  * 写在本身Component脚本中，隶属于UnityEngine命名空间，会被打包。

  * 功能和参数与MenuItem完全一致

  * 非静态

ContextMenuItem: 给你在右键变量名称时添加菜单
参数:1.菜单的名称；2.菜单执行的方法，要求脚本中必须有；3.按钮的先后顺序。

```csharp
class Program
{
    static void Main(string[] args)
    {
        dynamic dyn = 1;
        object obj = 1;
        dyn = dyn + 3;
        //obj = obj + 3; 报错

        // Rest the mouse pointer over dyn and obj to see their
        // types at compile time.
        System.Console.WriteLine(dyn.GetType());
        System.Console.WriteLine(obj.GetType());
    }
}

```

### 注册撤销操作

Undo.RegisterCreatedObjectUndo方法注册撤销操作
