[펌] Unity – How to copy a string to Clipboard
In Unity there is a cross-platform way to copy a string to Clipboard. Using the GUIUtility 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, Android and iOS!
[출처] https://thatfrenchgamedev.com/785/unity-2018-how-to-copy-string-to-clipboard/
'Unity3D > Extensions' 카테고리의 다른 글
[I2 Localization] iOS Privacy 로컬라이징 (0) | 2024.07.08 |
---|---|
[링크] Unity - Dictionary 를 Inspector 에 간단하게.. (0) | 2023.08.03 |
[펌] Find objects with DontDestroyOnLoad (0) | 2021.11.26 |
[Unity] Play Streaming Music From Server (0) | 2018.02.06 |
[펌] ADDING TO UNITY'S BUILT-IN CLASSES USING EXTENSION METHODS (0) | 2016.10.20 |