- Home /
How can I add copy and paste support for my EditorWindow?
Hi There
I have an EditorWindow for a behaviour tree implementation. Is there an editor/utility class/set of methods for handling copy and paste/clipboard interaction?
I'd quite like to be able to copy a branch of the BT and paste it somewhere else (or cut and paste).
Thanks a lot Bovine
Answer by RudyTheDev · Mar 17, 2016 at 01:45 PM
Unity can do EditorGUIUtility.systemCopyBuffer. You'll need custom implementation for custom content though. If it's serializable, there's stuff like EditorUtility.CopySerialized and SerializedObject.CopyFromSerializedProperty().
Answer by Steff Kempink · Mar 17, 2016 at 03:22 PM
Very late, but I came across this post before figuring out the answer.
This code manually copies whatever the user currently has selected to the OS buffer.
You could easily modify it to act differently on these different inputs.
void OnGUI() {
/*
Your code for drawing fields goes here
*/
var textEditor = EditorGUIUtility.GetStateObject(typeof(TextEditor), EditorGUIUtility.keyboardControl) as TextEditor;
if (textEditor != null) {
if (focusedWindow == this) {
// shift + x
if (Event.current.Equals(Event.KeyboardEvent("#x")))
textEditor.Cut();
// shift + c
if (Event.current.Equals(Event.KeyboardEvent("#c")))
textEditor.Copy();
// shift + v
if (Event.current.Equals(Event.KeyboardEvent("#v")))
textEditor.Paste();
}
}
}
This post got me on track:
http://forum.unity3d.com/threads/copy-textfield-or-textarea-text-to-clipboard.24101/#post-813226 This piece of documentation described how to get the keyboard input: http://docs.unity3d.com/ScriptReference/Event.KeyboardEvent.html
Unfortunately, the command for ctrl+c, "^c" , etc. are suppressed, probably by the inputfield, but I found that using shift as a substite, so "#c", did work, so that's how I left it.
TextEditor.Copy()
uses GUIUtility.systemCopyBuffer = ...
underneath. It only copies the text, not any underlying objects and doesn't work for non-text types of fields. Also, I think Unity has provided Ctrl+x/c/v natively for a while on various typable fields. I assume OP doesn't have a single field for a branch that Unity could natively copy.