Search Unity

copy/paste from windows clipboard? runtime

Discussion in 'UGUI & TextMesh Pro' started by CaoMengde777, Jan 16, 2015.

  1. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
    wait.... so you cant copy/paste from windows clipboard... like i ctrl+C from text file and the ctrl+V on the UI inputfield .... nothing happens, it pastes a 0 ? .. i guess i copied a 0 inside unity ?

    seems like a mistake... copy/paste is sooo standard.. ??

    i cant find much information about this either..
    i have a GUI.Button that can copy from Unity runtime to windows clipboard, but i cant figure how to get from clipboard to unity runtime... suppose my floats/string converting is incorrect?

    LOOL im making a calculator, because windows calculator doesnt do what i want it to... and i figure instead of downloading and trying other calculator programs, id try to write one for what i need... (passion to learn it all)
    i have it all done.. just cant copy/paste ... lame!!


    i read someone say you CAN paste from clipboard already, using GUI.Textfield
    doesnt work for me
     
    Last edited: Jan 16, 2015
  2. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
    i got it to work.... with OnGUI() ,, suppose its just as easy to use with new UI

    someone posted here about it
    http://answers.unity3d.com/questions/266244/how-can-i-add-copypaste-clipboard-support-to-my-ga.html


    this is my codes im using for simple calculator... its meant to help me with 3d modelling, the purpose is to have one value be always present, and paste in the other value... so that one of the values dont have to be entered Every time, its always there..

    suppose it could be better?? .. i did it quickly, and experimentally/hacky , just trying stuff lol

    Calculator code etc.. see bottom of it for copy/paste
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Calculator : MonoBehaviour
    6. {
    7.     public float valueA = 0.0f;
    8.     public float valueB = 0.0f;
    9.    
    10.     public float resultant = 0.0f;
    11.  
    12.     public string testValue;
    13.  
    14.  
    15.     void Start()
    16.     {
    17.         Screen.SetResolution (256, 140, false);                //make the program a little window dealy
    18.     }
    19.    
    20.  
    21.     void OnGUI()
    22.     {
    23.         string resultantString = resultant.ToString();
    24.        
    25.    
    26.         GUI.SetNextControlName("valueAField");
    27.         valueA = float.Parse (GUI.TextField(new Rect(10, 10, 100, 20), valueA.ToString ("0.00000")));        //number top
    28.        
    29.         GUI.SetNextControlName("valueBField");
    30.         valueB = float.Parse (GUI.TextField(new Rect(10, 30, 100, 20), valueB.ToString ("0.00000")));        //number bot
    31.  
    32.         GUI.Label(new Rect(15, 50, 200, 20), resultantString );                                        //result number
    33.        
    34.        
    35.         GUI.Label(new Rect(130, 10, 250, 20), "- dividend / minuend");            //labels
    36.         GUI.Label(new Rect(130, 30, 250, 20), "- divisor / subtrahend");
    37.         GUI.Label(new Rect(130, 50, 280, 20), "- Result");
    38.        
    39.  
    40.        
    41.         if (GUI.Button(new Rect(10, 75, 30, 30), " + "))            //mathamagicks
    42.         {
    43.             resultant = valueA + valueB;
    44.         }
    45.         if (GUI.Button(new Rect(50, 75, 30, 30), " - "))
    46.         {
    47.             resultant = valueA - valueB;
    48.         }
    49.         if (GUI.Button(new Rect(90, 75, 30, 30), "x "))
    50.         {
    51.             resultant = valueA * valueB;
    52.         }
    53.         if (GUI.Button(new Rect(130, 75, 30, 30), " / "))
    54.         {
    55.             resultant = valueA / valueB;
    56.         }
    57.  
    58.          //copy to windows clipboard
    59.         if (GUI.Button(new Rect(170, 110, 80, 30), "CopyResult"))                              
    60.         {
    61.             GUI.FocusControl("resultantField");
    62.             TextEditor te2 = new TextEditor { content = new GUIContent(resultantString) };
    63.             te2.SelectAll();
    64.             te2.Copy();
    65.         }
    66.  
    67.        //paste to here from windows clipboard[
    68.         if (GUI.Button(new Rect(10, 110, 70, 30), "Paste Top"))                                
    69.         {
    70.             string topStringACB = ClipboardHelper.clipBoard;
    71.             valueA = float.Parse (topStringACB);
    72.         }
    73.        
    74.         if (GUI.Button(new Rect(85, 110, 70, 30), "Paste Bot"))
    75.         {
    76.             string botStringBCB = ClipboardHelper.clipBoard;
    77.             valueB = float.Parse (botStringBCB);
    78.         }  
    79.     }
    80.  
    81. }
    82.  

    code from the site linked
    Code (CSharp):
    1. // C#
    2. // ClipboardHelper.cs
    3. using UnityEngine;
    4. using System;
    5. using System.Reflection;
    6.  
    7. [System.Serializable]
    8. public class ClipboardHelper
    9. {
    10.     private static PropertyInfo m_systemCopyBufferProperty = null;
    11.     private static PropertyInfo GetSystemCopyBufferProperty()
    12.     {
    13.         if (m_systemCopyBufferProperty == null)
    14.         {
    15.             Type T = typeof(GUIUtility);
    16.             m_systemCopyBufferProperty = T.GetProperty("systemCopyBuffer", BindingFlags.Static | BindingFlags.NonPublic);
    17.             if (m_systemCopyBufferProperty == null)
    18.                 throw new Exception("Can't access internal member 'GUIUtility.systemCopyBuffer' it may have been removed / renamed");
    19.         }
    20.         return m_systemCopyBufferProperty;
    21.     }
    22.     public static string clipBoard
    23.     {
    24.         get
    25.         {
    26.             PropertyInfo P = GetSystemCopyBufferProperty();
    27.             return (string)P.GetValue(null,null);
    28.         }
    29.         set
    30.         {
    31.             PropertyInfo P = GetSystemCopyBufferProperty();
    32.             P.SetValue(null,value,null);
    33.         }
    34.     }
    35. }
    36.  
    37.  



     
    Last edited: Jan 16, 2015
    kilik128 likes this.
  3. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    THERE IS A SIMPLER VERSION...

    One line:

    myString = GUIUtility.systemCopyBuffer;

    I did copy the revious post code and it triggered an error even when the clipboard was not null, had a directory string.
     
    AdrienVR, Zubair_Malik907 and sj631 like this.
  4. Zubair_Malik907

    Zubair_Malik907

    Joined:
    Jun 19, 2019
    Posts:
    15
    it works perfectly fine!