Unity Editor—extended menu buttons to quickly modify image formats

Three overloads of MenuItem

public MenuItem(string itemName);
//     itemName是表示为路径名的菜单项
//     例如 "GameObject/Do Something".
public MenuItem(string itemName, bool isValidateFunction);
//	isValidateFunction 控制方法是否激活。 默认为false。
//	为true时菜单栏不显示本选项或者本选项置灰不可以被点击
public MenuItem(string itemName, bool isValidateFunction, int priority);
//	 priority 控制菜单的显示上下的顺序。值越小越在上面显示

== NOTE ==

  1. The code must be placed under "Assets/Editor".
  2. MenuItem bootstrap methods must be static.
Add to menu bar

Insert image description here

public class UIUtils 
{
    
    
    // 添加到快捷栏
    [MenuItem("Test/item0", false)]
    static void TestFunc0()
    {
    
    

    }

    [MenuItem("Test/item1", true)]
    static void TestFunc1()
    {
    
    

    }

Add to Assets directory

Insert image description here

    // 添加到Assets目录下
    [MenuItem("Assets/图片修改为Sprite", priority = 1)]
    public static void changeTextureType()
    {
    
    
        Object[] m_objects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
        for (int i = 0; i < m_objects.Length; i++)
        {
    
    
            string path_tex = AssetDatabase.GetAssetPath(m_objects[i]);
            //Debug.Log("--" + m_objects[i].GetType());
            if (m_objects[i].GetType() == typeof(Texture2D))
            {
    
    
                TextureImporter texture = AssetImporter.GetAtPath(path_tex) as TextureImporter;
                // 修改图片格式为Sprite
                texture.textureType = TextureImporterType.Sprite;
                texture.spritePixelsPerUnit = 1;
                texture.filterMode = FilterMode.Trilinear;
                texture.mipmapEnabled = false;
                AssetDatabase.ImportAsset(path_tex);
            }
        }

        // 提示框
        EditorUtility.DisplayDialog("图片工具", "图片格式转换完毕!", "确认");
    }

Added to the right-click shortcut menu of the Hierarchy panel

Insert image description here

 	[MenuItem("GameObject/UIUtils/BtnComp", false, priority = 10)]
    static void creatBtnComp(MenuCommand menuCommand)
    {
    
    
        // Load Prefabs
        // AssetDatabase.LoadAssetAtPath<GameObject>("UIPrefabs/ImageButton");
        GameObject go = new GameObject();
        go.name = "BtnComp";

        // menuCommand.context是当前鼠标左键选中的GameObjet游戏物体
        // 设置为当前选中物体的子物体
        GameObjectUtility.SetParentAndAlign(go, menuCommand.context as GameObject);

        // 注册到U3D的Undo系统中,方便撤销操作
        Undo.RegisterCreatedObjectUndo(go, "Creat " + go.name);
        go.AddComponent<Image>();
        go.GetComponent<RectTransform>().sizeDelta = new Vector2(250, 100);
        go.AddComponent<BtnComp>();
        // 设置选中对象为新创建的go
        Selection.activeObject = go;
    }
Add to Inspector options

Added by == MenuItem("COMTEXT/component name/right-click menu name") ==
Insert image description here

    [MenuItem("CONTEXT/BtnComp/SetScale")]
    public static void ReName(MenuCommand command)
    {
    
    
        BtnComp btn = (BtnComp)command.context as BtnComp;
        btn.scaleBtn = !btn.scaleBtn;
    }

Custom shortcut key method

Insert image description here

    [MenuItem("Test/item2 %L")]
    static void TestFunc2()
    {
    
    

    }
Custom shortcut key rules
//     [MenuItem("菜单名/右键选项名称  快捷键组合")]
//     [MenuItem("Test/item2 %L")] 快捷键为 Ctrl + L

% + Key
under Win
Ctrl + Key
Under macOS
Cmd + Key
_ + Key
Set a single key as a shortcut key
# + Key
Shift + Key
# LEFT
shift + left
Other single keys are: LEFT, RIGHT, UP, DOWN, F1 ~ F12, HOME, END, PGUP, PGDN, etc.
& + Key
Alt + Key

Guess you like

Origin blog.csdn.net/the_vnas/article/details/131558222