Unity free custom add Editor shortcut key

There are several ways to monitor keys under Editor:

1. Customize menu bar function:

For example, the F5 key pauses the editor

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

public class CustomKeys
{
    [MenuItem("Custom快捷键/暂停 _F5")]
    static void EditorPauseCommand()
    {
        EditorApplication.isPaused = !EditorApplication.isPaused;
    }
}

api reference: http://docs.unity3d.com/Documentation/ScriptReference/MenuItem.html

 

2. OnSceneGUI monitors in GUI refresh:

using UnityEngine;
 using UnityEditor;
 [CustomEditor(typeof(MySpecialMonoBehaviour))]
 public class MyCustomEditor : Editor {
     void OnSceneGUI() {
         Event e = Event.current;
         if(EventType.KeyDown == e.type && KeyCode.RightControl == e.keyCode)
         {
             moveMulti = true;
         }
         if(EventType.KeyUp == e.type && KeyCode.RightControl == e.keyCode)
         {
             moveMulti = false;
         }
     }
 }

 

3.onSceneGUIDelegate registration event:

using UnityEditor;
 using UnityEngine;
 
 [InitializeOnLoad]
 public static class EditorHotkeysTracker
 {
     static EditorHotkeysTracker()
     {
         SceneView.onSceneGUIDelegate += view =>
         {
             var e = Event.current;
             if (e != null && e.keyCode != KeyCode.None)
                 Debug.Log("Key pressed in editor: " + e.keyCode);
         };
     }
 }

See: http://answers.unity3d.com/questions/381630/listen-for-a-key-in-edit-mode.html

The second and third methods are similar to the Update () function. When the button is pressed, it may be executed multiple times, and it is not convenient to monitor multiple buttons at the same time. Generally speaking, as a global shortcut key, you should combine ctrl / shift / alt or other buttons To prevent conflicts with ordinary keys. Personally think that the first way is more simple and reliable.

When you want to create an Image or Text, UGUI needs to click multiple times from the menu bar level (if you create an empty object and add components, it will only be more troublesome), and the created control is still located in the outermost level instead of It directly becomes the child object of the currently selected object, and I have to manually drag it under the parent object every time. Another way is to right-click an object and re-create it in the pop-up menu. Personally, I think this is quite troublesome.

For NGUI, most of the control creation has corresponding shortcut keys, and placing the newly generated object directly under the currently selected control is very efficient and fast.

Now add the shortcut keys for the UGUI control creation through the method I introduced earlier. When creating the control, you can also make some default initial settings at the same time, such as changing the Text font to a common font, setting its alignment color, and so on:

using UnityEngine;
 using UnityEditor;
 using UnityEngine.UI; 

// support both right-click menu creation and direct shortcut key creation on the selected object 
public  class UGUIHotKey 
{ 
    private  static GameObject CheckSelection (MenuCommand menuCommand) 
    { 
        GameObject selectedObj = menuCommand.context as GameObject;
         / / If it is not the operation of right-clicking on the object, the situation of the currently selected object is seen 
        if (selectedObj == null ) 
            selectedObj = Selection.activeGameObject;
         // If there is no currently selected object or the selected object is not under Canvas, it returns empty, the button is not response. (Of course, there is no need to have a Canvas, if not, create a new Canvas first)
        if (selectedObj == null || selectedObj! = null && selectedObj.GetComponentInParent <Canvas> () == null )
             return  null ;
         return selectedObj; 
    } 

    [MenuItem ( " GameObject / UGUI / Image # & i " , false , 6 )] // Please refer to the API documentation for the meaning of the parameter. The link above, the meaning of the call of several other interfaces in the function are also introduced 
    static  void CreateImage (MenuCommand menuCommand) 
    { 
        GameObject selectedObj = CheckSelection (menuCommand);
         if (selectedObj ==null)
            return;
        GameObject go = new GameObject ("Image");
        GameObjectUtility.SetParentAndAlign (go, selectedObj);
        Undo.RegisterCreatedObjectUndo (go, "Create " + go.name);
        Selection.activeObject = go;
        go.AddComponent<Image> ();
    }

    [MenuItem ("GameObject/UGUI/Text #&t", false, 6)]
    static void CreateText (MenuCommand menuCommand)
    {
        GameObject selectedObj = CheckSelection (menuCommand);
        if (selectedObj == null)
            return;
        GameObject go = new GameObject ("Text");
        GameObjectUtility.SetParentAndAlign (go, selectedObj);
        Undo.RegisterCreatedObjectUndo (go, "Create " + go.name);
        Selection.activeObject = go;

        Text t = go.AddComponent<Text> ();
        Font font = AssetDatabase.LoadAssetAtPath ("Assets/ArtSources/Font/xxxx.ttf", typeof (Font)) as Font;
        t.font = font;
        t.fontSize = 24;
        t.alignment = TextAnchor.MiddleCenter;
        t.color = Color.white;
        t.text = "New Text";
        t.rectTransform.sizeDelta = new Vector2 (150f, 30f);
    }
}

 

Guess you like

Origin www.cnblogs.com/sanyejun/p/12718787.html