插件使用——编辑器的扩展及应用

插件使用——编辑器的扩展及应用

    • 一,菜单栏操作
    • 二,组件操作
    • 三,快捷键添加

一,菜单栏操作

名称:MenuItem
功能:在上方菜单栏中添加新的菜单功能
用法:放在Editor文件夹下

using UnityEditor;
using UnityEngine;

public class Tools
{
    // [MenuItem ("Tools/test/test1")]
    //static public void Test1()
    // {
    //     Debug.Log("测试功能1");
    // }
    // [MenuItem("Tools/test/test2")]
    // static public void Test2()
    // {
    //     Debug.Log("测试功能2");
    // }
    // [MenuItem("Window/test/test1")]
    // static public void Test3()
    // {
    //     Debug.Log("测试功能3");
    // }
    //[MenuItem("GameObject/mytool")]
    //static public void Test4()
    //{
    //    Debug.Log("测试功能4");
    //}
    //每个菜单栏的priority优先级默认为1000,值越小越在上面
    [MenuItem("Tools/test1",false,1)]//在最上方菜单栏上
    static public void Test1()
    {
        Debug.Log("test1");
    }
    [MenuItem("Tools/test2",false,2)]
    static public void Test2()
    {
        Debug.Log("test2");
    }
    [MenuItem("Tools/test3", false, 13)]
    static public void Test3()
    {
        Debug.Log("test3");
    }
    [MenuItem("GameObject/3D Object/test1", false, 0)]//在右键创建物体上
    static public void TestGameObject1()
    {
        Debug.Log("GameObeject3");
    }
    [MenuItem("Assets/test3", false, 1)]
    static public void TestAssets1()
    {
        Debug.Log("Assets3");
    }
}

注:
通过设置组间隔(11为界限)来进行分割,
当处于GameObject/Assets里时,设置priovity使其在第一组,可右键创建。

二,组件操作

名称:CONTEXT
功能:组件操作
用法:放在Editor文件夹下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;


public class PlayerEditor 
{
    [MenuItem("CONTEXT/PlayerHealth/Init")]

    static  void InitHealthAndSpeed( MenuCommand cmd)//menucommand 时当前正在操作的组件
    {
        //context为组件
     CompleteProject. PlayerHealth health = cmd.context as CompleteProject. PlayerHealth;
        health.startingHealth = 200;
        health.currentHealth = 10;

        Debug.Log(cmd.context.GetType().FullName);
        Debug.Log("Init");
    }
    //在刚体上添加组件功能
    [MenuItem("CONTEXT/Rigidbody/Clear %M")]//添加上了Ctrl+M的快捷键
    static void ClearMassAndGravity(MenuCommand cmd)
    {
        Rigidbody rgd = cmd.context as Rigidbody;
        rgd.mass = 0;
        rgd.useGravity = false;
    }
}

三,快捷键添加

快捷键使用添加:
Ctrl+M:在菜单命令的后面加上%M,请注意%M之间无空格,但是%与菜单名称之间有空格隔开。
Alt+M:改用&M
Shift+M:#M
想用更为复杂的组合键,就把#%&组合使用吧。#代表Shift,%代表Ctrl,&代表Alt。

   [MenuItem("TestFunction/Test1 %M")]// 在菜单栏上提示Ctrl+M功能键了

你可能感兴趣的:(插件,Unity)