How can I add Copy/Paste clipboard support to my game?

I have the need to add Ctrl+C / Ctrl+V ( command for Mac) copy and paste functionality into my application. Ideally I could also create pop-up menus with right-clicks, but that could be addressed later so long as I have some way of getting clipboard content into the game.

In a .NET windows form application you can use System.Windows.Clipboard, but the System.Windows namespace is not available in Unity (perhaps not even Mono).

Does anyone else know if there’s a way to do this? Thanks!

For Unity version 5.2.0f3, systemCopyBuffer is now a public member of GUIUtility:

  public static string Clipboard
  {
    get { return GUIUtility.systemCopyBuffer; }
    set { GUIUtility.systemCopyBuffer = value; }
  }

It turns out you can do this using reflection to get at a private member of GUIUtility. I lost the link so I cannot site the project I found that did this, please add it if someone finds the original author:

// C#
// ClipboardHelper.cs
using UnityEngine;
using System;
using System.Reflection;

public class ClipboardHelper
{
	private static PropertyInfo m_systemCopyBufferProperty = null;
	private static PropertyInfo GetSystemCopyBufferProperty()
	{
		if (m_systemCopyBufferProperty == null)
		{
			Type T = typeof(GUIUtility);
			m_systemCopyBufferProperty = T.GetProperty("systemCopyBuffer", BindingFlags.Static | BindingFlags.NonPublic);
			if (m_systemCopyBufferProperty == null)
				throw new Exception("Can't access internal member 'GUIUtility.systemCopyBuffer' it may have been removed / renamed");
		}
		return m_systemCopyBufferProperty;
	}
	public static string clipBoard
	{
		get 
		{
			PropertyInfo P = GetSystemCopyBufferProperty();
			return (string)P.GetValue(null,null);
		}
		set
		{
			PropertyInfo P = GetSystemCopyBufferProperty();
			P.SetValue(null,value,null);
		}
	}
}

Here is a drop-in replacement for Baconaise’s answer, using Summit_Peak’s approach, that works in Unity 5.3.1p2 (and, hopefully, newer versions).

// C#
// ClipboardHelper.cs
using UnityEngine;

public class ClipboardHelper
{
	public static string clipBoard
	{
		get 
		{
			return GUIUtility.systemCopyBuffer;
		}
		set
		{
			GUIUtility.systemCopyBuffer = value;
		}
	}
}

The GUIUtility can work through the Unity itself, but if you want to use the system’s clipboard (like on iOS or Android, you want to copy some text in your game and then paste it in other app), it won’t help because the text is only in the GUI system of Unity.

I just posted a cross-platform plugin to solve this mess thing. It is using the system’s clipboard to handle the copy&paste action. See it here in Asset Store, which could support iOS, Android, WP and some desktop platforms as well. It exposes the same API for all, and I hope it could make your life easier.

Sorry for reviving an old question. Not exactly new but I’ve not found a useful snippet. This is what I used:

/// <summary>
/// Add copy-paste functionality to any text field
/// Returns changed text or NULL.
/// Usage: text = HandleCopyPaste (controlID) ?? text;
/// </summary>
public static string HandleCopyPaste(int controlID)
{
    if (controlID == GUIUtility.keyboardControl)
    {
        if (Event.current.type == 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();
                return editor.text;
            }
        }
    }
    return null;
}

Then just make a wrapper around your textfield (or any other keyboard control):

/// <summary>
/// TextField with copy-paste support
/// </summary>
public static string TextField(string value, params GUILayoutOption[] options)
{
    int textFieldID = GUIUtility.GetControlID("TextField".GetHashCode(), FocusType.Keyboard) + 1;
    if (textFieldID == 0)
        return value;

    // Handle custom copy-paste
    value = HandleCopyPaste(textFieldID) ?? value;

    return GUILayout.TextField(value);
}

Should theoretically work down to Unity 4.0 and on all platforms:)

Hope that helps!

Seneral

Here is the slim version of clipboard property:

public string ClipboardValue { get => GUIUtility.systemCopyBuffer; set => GUIUtility.systemCopyBuffer = value; }