Unity添加自定義快捷鍵——UGUI快捷鍵


在Editor下監聽按鍵有以下幾種方式:

  1. 自定義菜單欄功能:
     1 using UnityEngine;
     2  using UnityEditor;
     3  public static class MyMenuCommands {
     4      [MenuItem("My Commands/First Command _p")]
     5      static void FirstCommand() {
     6          Debug.Log("You used the shortcut P");
     7      }
     8      [MenuItem("My Commands/Special Command %g")]
     9      static void SpecialCommand() {
    10          Debug.Log("You used the shortcut Cmd+G (Mac)  Ctrl+G (Win)");
    11      }
    12  }

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

  2. OnSceneGUI在GUI刷新中監聽:
     1 using UnityEngine;
     2  using UnityEditor;
     3  [CustomEditor(typeof(MySpecialMonoBehaviour))]
     4  public class MyCustomEditor : Editor {
     5      void OnSceneGUI() {
     6          Event e = Event.current;
     7          if(EventType.KeyDown == e.type && KeyCode.RightControl == e.keyCode)
     8          {
     9              moveMulti = true;
    10          }
    11          if(EventType.KeyUp == e.type && KeyCode.RightControl == e.keyCode)
    12          {
    13              moveMulti = false;
    14          }
    15      }
    16  }
  3. onSceneGUIDelegate注冊事件:
     1  using UnityEditor;
     2  using UnityEngine;
     3  
     4  [InitializeOnLoad]
     5  public static class EditorHotkeysTracker
     6  {
     7      static EditorHotkeysTracker()
     8      {
     9          SceneView.onSceneGUIDelegate += view =>
    10          {
    11              var e = Event.current;
    12              if (e != null && e.keyCode != KeyCode.None)
    13                  Debug.Log("Key pressed in editor: " + e.keyCode);
    14          };
    15      }
    16  }

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

方式二跟三類似Update()函數,當按鍵按下時可能會被多次執行,且不方便同時監聽多個按鍵,一般來說作為全局快捷鍵應該同時組合ctrl/shift/alt或別的按鍵,以防跟普通按鍵沖突。個人認為方式一是更加簡單可靠。

UGUI在想要創建一個Image或者Text時需要從菜單欄中級級點擊多次才行(若是創建空物體再添加組件的方式只會更麻煩),而且創建的控件還是位於最外層層級中而不是直接成為我當前選中的物體的子物體,每次都得手動拖到父物體之下。另外一種方式就是右鍵點擊一個物體,重彈出菜單中創建,個人覺得這樣子也挺麻煩。

NGUI則大部分的控件創建都有對應的快捷鍵,且直接將新生成的物體放置到當前選中的控件之下,十分高效快捷。

現通過前面介紹的方式一為UGUI的控件創建添加快捷鍵,在創建控件的時你還可以同時進行一些默認初始設置,如改變Text的字體為常用字體,設置其對齊方式顏色等等:

 1 using UnityEngine;
 2 using UnityEditor;
 3 using UnityEngine.UI;
 4 
 5 //同時支持在選中物體上右鍵菜單創建和直接快捷鍵創建
 6 public class UGUIHotKey
 7 {
 8     private static GameObject CheckSelection (MenuCommand menuCommand)
 9     {
10         GameObject selectedObj = menuCommand.context as GameObject;
11         //若當前不是右鍵點擊物體的操作則看當前選中的物體的情況
12         if (selectedObj == null)
13             selectedObj = Selection.activeGameObject;
14         //當前沒有選中物體或者選中的物體不在Canvas之下則返回空,按鍵不響應。(當然也可以不要求存在Canvas,沒有時則先創建一個新的Canvas)
15         if (selectedObj == null || selectedObj != null && selectedObj.GetComponentInParent<Canvas> () == null)
16             return null;
17         return selectedObj;
18     }
19 
20     [MenuItem ("GameObject/UGUI/Image #&i", false, 6)] //參數意義請查閱API文檔,上文有鏈接,函數中的幾個其他接口的調用的含義也有介紹
21     static void CreateImage (MenuCommand menuCommand)
22     {
23         GameObject selectedObj = CheckSelection (menuCommand);
24         if (selectedObj == null)
25             return;
26         GameObject go = new GameObject ("Image");
27         GameObjectUtility.SetParentAndAlign (go, selectedObj);
28         Undo.RegisterCreatedObjectUndo (go, "Create " + go.name);
29         Selection.activeObject = go;
30         go.AddComponent<Image> ();
31     }
32 
33     [MenuItem ("GameObject/UGUI/Text #&t", false, 6)]
34     static void CreateText (MenuCommand menuCommand)
35     {
36         GameObject selectedObj = CheckSelection (menuCommand);
37         if (selectedObj == null)
38             return;
39         GameObject go = new GameObject ("Text");
40         GameObjectUtility.SetParentAndAlign (go, selectedObj);
41         Undo.RegisterCreatedObjectUndo (go, "Create " + go.name);
42         Selection.activeObject = go;
43 
44         Text t = go.AddComponent<Text> ();
45         Font font = AssetDatabase.LoadAssetAtPath ("Assets/ArtSources/Font/xxxx.ttf", typeof (Font)) as Font;
46         t.font = font;
47         t.fontSize = 24;
48         t.alignment = TextAnchor.MiddleCenter;
49         t.color = Color.white;
50         t.text = "New Text";
51         t.rectTransform.sizeDelta = new Vector2 (150f, 30f);
52     }
53 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM