在Editor下監聽按鍵有以下幾種方式:
1.自定義菜單欄功能:
比如F5鍵暫停編輯器
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參考:http://docs.unity3d.com/Documentation/ScriptReference/MenuItem.html
快速打開某個場景,並且運行游戲
using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; using UnityEngine.SceneManagement; public class CustomKeys { [MenuItem("Custom快捷鍵/快速 _F1")] static void SpeedGameCommand() { if (EditorApplication.isPlaying) { EditorApplication.isPlaying = false; } else { EditorSceneManager.OpenScene("Assets/Scenes/LandInit.unity"); EditorApplication.isPlaying = true; } } }
2.OnSceneGUI在GUI刷新中監聽:
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注冊事件:
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); }; } }
詳見:http://answers.unity3d.com/questions/381630/listen-for-a-key-in-edit-mode.html
方式二跟三類似Update()函數,當按鍵按下時可能會被多次執行,且不方便同時監聽多個按鍵,一般來說作為全局快捷鍵應該同時組合ctrl/shift/alt或別的按鍵,以防跟普通按鍵沖突。個人認為方式一是更加簡單可靠。
UGUI在想要創建一個Image或者Text時需要從菜單欄中級級點擊多次才行(若是創建空物體再添加組件的方式只會更麻煩),而且創建的控件還是位於最外層層級中而不是直接成為我當前選中的物體的子物體,每次都得手動拖到父物體之下。另外一種方式就是右鍵點擊一個物體,重彈出菜單中創建,個人覺得這樣子也挺麻煩。
NGUI則大部分的控件創建都有對應的快捷鍵,且直接將新生成的物體放置到當前選中的控件之下,十分高效快捷。
現通過前面介紹的方式一為UGUI的控件創建添加快捷鍵,在創建控件的時你還可以同時進行一些默認初始設置,如改變Text的字體為常用字體,設置其對齊方式顏色等等:
using UnityEngine; using UnityEditor; using UnityEngine.UI; //同時支持在選中物體上右鍵菜單創建和直接快捷鍵創建 public class UGUIHotKey { private static GameObject CheckSelection (MenuCommand menuCommand) { GameObject selectedObj = menuCommand.context as GameObject; //若當前不是右鍵點擊物體的操作則看當前選中的物體的情況 if (selectedObj == null) selectedObj = Selection.activeGameObject; //當前沒有選中物體或者選中的物體不在Canvas之下則返回空,按鍵不響應。(當然也可以不要求存在Canvas,沒有時則先創建一個新的Canvas) if (selectedObj == null || selectedObj != null && selectedObj.GetComponentInParent<Canvas> () == null) return null; return selectedObj; } [MenuItem ("GameObject/UGUI/Image #&i", false, 6)] //參數意義請查閱API文檔,上文有鏈接,函數中的幾個其他接口的調用的含義也有介紹 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); } }