赞
踩
unity的编辑器文本输入框GUILayout.TextField不能快捷键复制粘贴,很不好,教大家解决这个问题
m_txt = MyGUILayout.TextField(m_txt);
其中MyGUILayout
代码如下
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MyGUILayout { public static string HandleCopyPaste(int controlID) { if (controlID == GUIUtility.keyboardControl) { if (Event.current.type == UnityEngine.EventType.KeyUp && (Event.current.modifiers == EventModifiers.Control || Event.current.modifiers == EventModifiers.Command)) { if (Event.current.keyCode == KeyCode.C) { Event.current.Use(); TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); editor.Copy(); } else if (Event.current.keyCode == KeyCode.V) { Event.current.Use(); TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); editor.Paste(); #if UNITY_5_3_OR_NEWER || UNITY_5_3 return editor.text; //以及更高的unity版本中editor.content.text已经被废弃,需使用editor.text代替 #else return editor.content.text; #endif } else if (Event.current.keyCode == KeyCode.A) { Event.current.Use(); TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); editor.SelectAll(); } } } return null; } /// <summary> /// TextField复制粘贴的实现 /// </summary> public static string TextField(string value, params GUILayoutOption[] options) { int textFieldID = GUIUtility.GetControlID("TextField".GetHashCode(), FocusType.Keyboard) + 1; if (textFieldID == 0) return value; //处理复制粘贴的操作 value = HandleCopyPaste(textFieldID) ?? value; return GUILayout.TextField(value, options); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。