标签:
在Editor下监听按键有以下几种方式:
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
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 }
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 }
标签:
原文地址:http://www.cnblogs.com/suoluo/p/5663207.html