티스토리 뷰

카테고리 없음

How to copy a string to Clipboard

제로 제로코인 2020. 3. 18. 14:58

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) { var textEditor = new TextEditor(); textEditor.text = str; textEditor.SelectAll(); textEditor.Copy(); } }

 

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!

댓글
댓글쓰기 폼