Created
January 11, 2022 03:25
-
-
Save mukaschultze/afa3056b0729662f781332d1f1b0d67c to your computer and use it in GitHub Desktop.
Unity Clipboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| #if UNITY_WEBGL && !UNITY_EDITOR | |
| using System.Runtime.InteropServices; | |
| #endif | |
| public static class Clipboard { | |
| public static string ClipboardText { | |
| get { return clipboard_read_text(); } | |
| set { clipboard_write_text(value); } | |
| } | |
| #if UNITY_WEBGL && !UNITY_EDITOR | |
| [DllImport("__Internal")] public static extern string clipboard_read_text(); | |
| [DllImport("__Internal")] public static extern void clipboard_write_text(string text); | |
| #else | |
| public static string clipboard_read_text() { return GUIUtility.systemCopyBuffer; } | |
| public static void clipboard_write_text(string text) { GUIUtility.systemCopyBuffer = text; } | |
| #if UNITY_EDITOR | |
| private static string m_EmbeddedLib = @" | |
| var ClipboardLib = { | |
| clipboard_read_text: function () { | |
| // NOOP | |
| }, | |
| clipboard_write_text: function (str) { | |
| navigator.clipboard.writeText(Pointer_stringify(str)); | |
| }, | |
| }; | |
| mergeInto(LibraryManager.library, ClipboardLib); | |
| "; | |
| [UnityEditor.InitializeOnLoadMethod] | |
| private static void ExtractJSLib() { | |
| var path = System.IO.Path.Combine(Application.dataPath, "Plugins"); | |
| var folder = new System.IO.DirectoryInfo(path); | |
| if (!folder.Exists) | |
| folder.Create(); | |
| path = System.IO.Path.Combine(path, "Clipboard.jslib"); | |
| if (System.IO.File.Exists(path)) | |
| return; | |
| Debug.Log("Clipboard.jslib does not exist in the plugins folder, extracting"); | |
| System.IO.File.WriteAllText(path, m_EmbeddedLib); | |
| UnityEditor.AssetDatabase.Refresh(); | |
| UnityEditor.EditorApplication.RepaintProjectWindow(); | |
| } | |
| #endif | |
| #endif | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment