Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by tarmo-jussila · Feb 19, 2016 at 01:47 PM · c#editorguicopy5.3copy-paste

Copy to Clipboard with a Button (Unity 5.3 solution)

I've seen multiple games which make things very easy for the player by providing Copy to Clipboard buttons next to IP addresses inside a multiplayer lobby for example. It's an extremely convenient solution because the printed out IP might not be interactable with mouse.

The example code below works perfectly in the Unity editor, but not in the built version. I'm using EditorGUIUtility.systemCopyBuffer to access the clipboard - which is an Editor feature only. At least in Unity 5.2 systemCopyBuffer used to be a public member of GUIUtility. In Unity 5.3 this is no longer the case.

Before you point to me other older posts about the subject, please note that the Reflection approach doesn't seem to work with Unity 5.3 onward either. I'm looking for the most modern approach to this with the latest Unity version (currently 5.3.2f1).

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEditor;
 
 public class GetIP : MonoBehaviour
 {
     public bool isExternalAddress;
     private string ipAddress;
     private Text textIP;
 
     void Start()
     {
         textIP = GetComponent<Text>();
 
         if (isExternalAddress)
         {
             ipAddress = Network.player.externalIP;
             textIP.text = "INTERNET: " + ipAddress;
         }
         else
         {
             ipAddress = Network.player.ipAddress;
             textIP.text = "LAN: " + ipAddress;
         }
     }
 
     public void CopyToClipboard()
     {
         EditorGUIUtility.systemCopyBuffer = ipAddress;
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mezzostatic · Jan 09, 2017 at 10:05 AM 0
Share

Clipboard.GetText(); msdn c#

5 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by IgorAherne · Jan 09, 2017 at 10:15 AM

using UnityEngine; TextEditor te = new TextEditor(); te.text = "plug your text here"; te.SelectAll(); te.Copy();

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anisoropos · May 19, 2018 at 08:03 AM 2
Share

This worked like a charm. Here's it in a more convenient format.

 using UnityEngine;

 // This needs to be added to a public static class to be used like an extension
 public static void CopyToClipboard(this string s)
 {
     TextEditor te = new TextEditor();
     te.text = s;
     te.SelectAll();
     te.Copy();
 }
 
 /*
 Example Use Case 1 :: 
 string s = "https://answers.unity.com";
 s.CopyToClipboard();
 
 Example Use Case 2 ::
 Text textField;
 string s = textField.text;
 s.CopyToClipboard();
 */

avatar image thinkcode Anisoropos · Dec 21, 2019 at 03:13 PM 1
Share

Thank you!

avatar image
2

Answer by mezzostatic · Jan 09, 2017 at 10:19 AM

Clipboard.GetText();

i dont have enough permission to post as a comment, that's the C# way to do it. sorry for being dumb.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TeamTekkit · Oct 14, 2017 at 04:09 PM 1
Share

But how do you implement the Clipboard function?!

avatar image MaxLohMusic · Apr 07 at 05:05 AM 0
Share

What? How did you get Clipboard?

avatar image
1

Answer by karsnen · Feb 10, 2020 at 10:13 PM

    internal static string Clipboard
         {//https://flystone.tistory.com/138
             get
             {
                 TextEditor _textEditor = new TextEditor();
                 _textEditor.Paste();
                 return _textEditor.text;
             }
             set
             {
                 TextEditor _textEditor = new TextEditor
                 {text = value};
 
                 _textEditor.OnFocus();
                 _textEditor.Copy();
             }
         }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by MaxLohMusic · Apr 07 at 05:18 AM

I had to find this from another thread:

 GUIUtility.systemCopyBuffer = "text"

Much more useful than the select all method because you can put text from multiple boxes or really anything at all.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by codemaker2015 · Sep 20, 2021 at 08:49 AM

 void CopyToClipboard(string s) {
         TextEditor te = new TextEditor();
         te.text = s;
         te.SelectAll();
         te.Copy();
 }
 
  //Calling the method
  CopyToClipboard("Hello World");
  
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Copy Material 1 Answer

C#: UnityEditor using window editor functions across classes in ONGUI() 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges