LoginSignup
31
33

More than 5 years have passed since last update.

Editor拡張でショートカットキーをカスタマイズ

Last updated at Posted at 2014-03-08

Unityのデフォルトショートカットキーが扱いにくいので
自分用のEditor拡張Scriptを作成してみた

変更または新たに定義したいショートカットキー

  • ゲームオブジェクトの作成、削除、有効/無効
  • Prefab(プレハブ)のApply
  • コンソール出力のクリア
  • 再インポート
  • 各Windowのフォーカス変更

Menu Item

ショートカットキーはMenu Item関数を利用して定義できる
&#g で Alt + Shift + g みたいな感じで。詳細は本家で

ホットキー キー名
% Command or Windows key
# Shift key
& Alt key

以下のスクリプトを /Assets/Editor に配置する

using UnityEditor;
using UnityEngine;
using System.Text.RegularExpressions;
using System.Reflection;

public class UnityKeyRemapEditor : EditorWindow
{
    // オブジェクトの共通Openコマンド
    [MenuItem("KeyRemap/Open &o")]
    static void KeyRemapOpen()
    {
        foreach (var aObj in Selection.objects)
        {
            var aObjPath = AssetDatabase.GetAssetPath(aObj);
            if (Regex.IsMatch(aObjPath, @"^.*\.unity"))  { EditorApplication.OpenScene(aObjPath); }
            if (Regex.IsMatch(aObjPath, @"^.*\.cs"))     { AssetDatabase.OpenAsset(aObj); }
            if (Regex.IsMatch(aObjPath, @"^.*\.prefab"))
            {
                PrefabUtility.InstantiatePrefab(aObj);
                CommonExecuteMenuItem("Window/Hierarchy");
            }
        }
    }

    // ゲームオブジェクト作成
    [MenuItem("KeyRemap/CreateGameObject &g")]
    static void KeyRemapCreateGameObject() { CommonExecuteMenuItem("GameObject/Create Empty"); }

    // ゲームオブジェクトの削除
    [MenuItem("KeyRemap/Delete &h")]
    static void KeyRemapDelete()
    {
        foreach (var aObj in Selection.objects)
        {
            GameObject aGameObject = aObj as GameObject;
            if (aGameObject) { GameObject.DestroyImmediate(aGameObject); }
        }
    }

    // ゲームオブジェクトの有効、無効
    [MenuItem("KeyRemap/ActiveToggle &t")]
    static void KeyRemapActiveToggle()
    {
        foreach (var aObj in Selection.objects)
        {
            GameObject aGameObject = aObj as GameObject;
            if (aGameObject) { aGameObject.SetActive(!aGameObject.activeSelf); }
        }
    }

    // PrefabのApply
    [MenuItem("KeyRemap/PrefabApply &a")]
    static void KeyRemapPrefabApply() { CommonExecuteMenuItem("GameObject/Apply Changes To Prefab"); }

    // コンソール出力のクリア
    [MenuItem("KeyRemap/ClearConsoleLogs &c")]
    private static void ClearConsoleLogs()
    {
        var type = Types.GetType("UnityEditorInternal.LogEntries", "UnityEditor");
        var info = type.GetMethod("Clear", BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
        info.Invoke(null, new object[0]);
    }

    // 再インポート
    [MenuItem("KeyRemap/Reimport &r")]
    static void KeyRemapReimport() { CommonExecuteMenuItem("Assets/Reimport"); }

    // フォーカス変更
    [MenuItem("KeyRemap/Scene #&s")]
    static void KeyRemapScene() { CommonExecuteMenuItem("Window/Scene"); }

    [MenuItem("KeyRemap/Scene #&g")]
    static void KeyRemapGame() { CommonExecuteMenuItem("Window/Game"); }

    [MenuItem("KeyRemap/Inspector #&i")]
    static void KeyRemapInspector() { CommonExecuteMenuItem("Window/Inspector"); }

    [MenuItem("KeyRemap/Hierarchy #&h")]
    static void KeyRemapHierarchy() { CommonExecuteMenuItem("Window/Hierarchy"); }

    [MenuItem("KeyRemap/Project #&p")]
    static void KeyRemapProject() { CommonExecuteMenuItem("Window/Project"); }

    [MenuItem("KeyRemap/Animation #&a")]
    static void KeyRemapAnimation() { CommonExecuteMenuItem("Window/Animation"); }

    [MenuItem("KeyRemap/Console #&c")]
    static void KeyRemapConsole() { CommonExecuteMenuItem("Window/Console"); }

    static void CommonExecuteMenuItem(string iStr)
    {
        EditorApplication.ExecuteMenuItem(iStr);
    }
}
31
33
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
33