- Home /
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!
Yeah, I had found that thread. It had information but nothing actionable. I posted this question in hopes that someone had solved this.
Answer by Baconaise · Jun 25, 2012 at 04:37 PM
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);
}
}
}
Returned an error in 5.3.3 , its irrelevant now too, demote it from best anser. just used myString = GUIUtility.systemCopyBuffer; was easier.
THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 - THE ABOVE CODE IS IRRELEVANT SINCE 2016 -
Answer by Summit_Peak · Sep 14, 2015 at 01:48 PM
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; }
}
in Unity 5.3.0 this is no longer public. I haven't confirmed if the previous 'reflection' approach works.
the previous 'reflection' approach doesn't work in 5.3
Is this code work for iOS?? because in OSX its working but in iPhone its not working.
For iOS you'll need to write a plugin. Or you can buy one from the asset store. I usually make plugins myself since plugins from the asset store tend to not work well with other plugins. But it'll take a lot of time if you haven't done it before. I added iOS clipboard support a year ago and I totally forgot how to do it but I do remember it being a very tedious task.
@jespertheend2, thanks for your reply. Let see what other members say about this otherwise I purchase this plugin:
Answer by clintonb · Feb 24, 2016 at 05:50 PM
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;
}
}
}
Answer by onevcat · Jul 10, 2014 at 03:54 AM
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.
Hi mate,
I coulnd't find your package on the Asset store anymore, but i found your github page. Are you still supporting the UniClipboard?
I posted an issue there, hoping you could have a look and give me a pointer on how to fix my iOS problem : https://github.com/sanukin39/UniClipboard/issues/7
Answer by arutyunef · Jul 23, 2019 at 10:59 AM
Here is the slim version of clipboard property:
public string ClipboardValue { get => GUIUtility.systemCopyBuffer; set => GUIUtility.systemCopyBuffer = value; }
Your answer
Follow this Question
Related Questions
Copy/Paste Key Combos in Webplayer 1 Answer
Use Unity UI For 2D Games Or Custom Objects Instead? 2 Answers
gameobject references runtime script in scene file 15 Answers
unity scrollview doesnt bounce 0 Answers