Unity - Copy string to clipboard

Unity 2019 – 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 also work in Unity 2018.

By using the TextEditor 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>
    /// <param name="str"></param>
    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 and mobile!

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
      Where To Stay In London 08th August 2020 at 09:50 -

      Siemens Business Services: Siemens is a well known name in Enterprise Resource Planning service providers.

      Reply
    2. Avatar

      Онлайн фильм смотреть Большая маленькая ложь смотреть онлайн в хорошем качестве Смотреть фильмы, сериалы, мультфильмы, шоу, передачи онлайн

      Reply
    3. Avatar
      MatthewLenly 23rd July 2020 at 18:38 -

      [b][/b]

      Reply
    4. Avatar
      오피 17th July 2020 at 09:23 -

      Even the things you don’t like about yourself make you interesting.
      You are the reason I am smiling today.
      You make me 오피
      up like I’m on millions of bubbles (We got this one from one of our kids after he got a new coat.)
      I enjoy spending time with you.
      Your outlook on life is amazing.
      I like the way you are.
      When it comes to cooking, no one’s meals are quite as delicious.

      Reply
    5. Avatar
      GlennaButty 11th July 2020 at 23:51 -

      [url=https://www.cleaning-master.com]Move-out in cleaning[/url]
      Service Cleaning Staten Island was created in 2012 with a clear task: to perform a highly professional cleaning services in friendly manner that is not only which not just delights and satisfies all ours client! With deep cleaning, our employees cleansing elements refrigerator in the house . Competent employees “Cleaning Service” always ready decide varied problem, associated with guidance order. You always can call in “Cleaning Service” – our employees to the conscience cope with the work of any volume. Our Created Cleaning the holding located give you any help and solve this a problem! Our firm we carry out cleaning services(MAIDS DOWNTOWN) exclusively by means of eco-means, they absolutely safe and not even litter environment atmosphere. This the specialized company Williamsburg presents large set service on cleaning, you left only to choose the right, appropriate to you type of cleaning, and contact to our company for professional help. In our the company hourly work employee. This a highly large cleaning , that will leave your family hearth spotless . Regardless on , moving you or not Clean Master can be there to help to bring your personal old or new house exemplary appearance.We have involved only qualified masters, having the required practical experience. CLEANING MASTER Bococa apply excellent, not harmful detergents and scouring materials, reliable, professional and high quality equipment.Our organization provides services for cleaning – professional (industrial) cleaning. Cleaning famous company Clinton Hill- carries out production activity on cleaning.

      Reply
    6. 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
    7. Avatar
      Skjalg 26th September 2019 at 10:43 -

      This is just a long winded way of writing GUIUtility.systemCopyBuffer = text;

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

      ty

      Reply

    Leave a Reply