Unity - Copy string to clipboard

Unity – How to copy a string to Clipboard

In Unity there is a cross-platform way to copy a string to Clipboard. Using the TextEditor class I’m going to show you how to copy a string into the user’s Clipboard. This should work in Unity 2017 and beyond.

By using the GUIUtility class, from UnityEngine, we can fit any C# object with a ToString() function into the Clipboard!

Clipboard Extension

To make it easily accessible I made the function as a string extension. By doing it this way we can use the function on any string anywhere in the code.

using UnityEngine;

public static class ClipboardExtension
{
    /// <summary>
    /// Puts the string into the Clipboard.
    /// </summary>
    public static void CopyToClipboard(this string str)
    {
        GUIUtility.systemCopyBuffer = str;
    }
}

Example

Here is an example on how to copy different elements into the Clipboard using the ClipboardExtension:

public string GetSomeString()
{
    return "This is a string coming from a function!";
}

public void TestCopyToClipboard()
{
    // + Using a standard string
    string testString = "Am I in the Clipboard?";
    testString.CopyToClipboard();
    // The content of test1 is in the Clipboard now!

    // + Using a method to get a string
    GetSomeString().CopyToClipboard();
    // The content returned by GetSomeString() is in the Clipboard now!

    // + Using a C# object with a ToString() method
    Color colorTest = Color.red;
    colorTest.ToString().CopyToClipboard();
    // The string version of the object colorTest is in the clipboard now!
}

You can try out this code for yourself! Run it, then try pasting your Clipboard into a notepad. It has been tested and works on PC, WebGL, Android and iOS!

Pierre Planeau

Hello,
My name is Pierre and I run this website. You can learn more about me over here and some of my personal projects over there. 🙂

Other posts

  • Hello!
    FIrst of all, thank you very much for the post!

    Though I am using it and it is not working.
    It copies the text inside Unity but using WebGL in a browser is not working.
    I have also tried with this other plugin and nothing neither. https://github.com/sanukin39/UniClipboard

    Do you know further information?
    Thank you so much!

  • COMMENTS (3)

    1. Avatar
      dissertation writerswriting an essay 11th January 2021 at 07:19 -

      I don’t know if it’s just me or if perhaps everyone else experiencing issues with your blog.
      It appears like some of the text within your content
      are running off the screen. Can somebody else please provide feedback and let
      me know if this is happening to them as well?
      This might be a issue with my internet browser because I’ve had this happen before.

      Kudos
      https://argumentativeresearch.com/
      dissertation writerswriting an essay
      dissertation writerswriting aan essay https://writemyessaybest.com/

      Reply
    2. Avatar
      Critical writing essay 08th January 2021 at 02:29 -

      With thanks, Plenty of facts.

      Beest Essy writing
      Critical writing essay https://marketwritingservices.com/summer.essay.writing.html

      Reply
    3. Avatar
      Isabel 10th June 2020 at 12:15 -

      Hello!
      FIrst of all, thank you very much for the post!

      Though I am using it and it is not working.
      It copies the text inside Unity but using WebGL in a browser is not working.
      I have also tried with this other plugin and nothing neither. https://github.com/sanukin39/UniClipboard

      Do you know further information?
      Thank you so much!

      Reply
      • Pierre Planeau
        Pierre Planeau 14th June 2020 at 20:05

        Sorry for the late reply, try using:

        GUIUtility.systemCopyBuffer = “My Copied text”;

        Reply
    4. Avatar
      17th September 2019 at 18:45 -

      ty

      Reply

    Leave a Reply