copy/paste from windows clipboard? runtime

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

i got it to work… with OnGUI() , suppose its just as easy to use with new UI

someone posted here about it

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

```csharp
*using UnityEngine;
using System.Collections;

public class Calculator : MonoBehaviour
{
public float valueA = 0.0f;
public float valueB = 0.0f;

public float resultant = 0.0f;

public string testValue;


void Start()
{
    Screen.SetResolution (256, 140, false);                //make the program a little window dealy
}


void OnGUI()
{
    string resultantString = resultant.ToString();
   

    GUI.SetNextControlName("valueAField");
    valueA = float.Parse (GUI.TextField(new Rect(10, 10, 100, 20), valueA.ToString ("0.00000")));        //number top
   
    GUI.SetNextControlName("valueBField");
    valueB = float.Parse (GUI.TextField(new Rect(10, 30, 100, 20), valueB.ToString ("0.00000")));        //number bot

    GUI.Label(new Rect(15, 50, 200, 20), resultantString );                                        //result number
   
   
    GUI.Label(new Rect(130, 10, 250, 20), "- dividend / minuend");            //labels
    GUI.Label(new Rect(130, 30, 250, 20), "- divisor / subtrahend");
    GUI.Label(new Rect(130, 50, 280, 20), "- Result");
   

   
    if (GUI.Button(new Rect(10, 75, 30, 30), " + "))            //mathamagicks
    {
        resultant = valueA + valueB;
    }
    if (GUI.Button(new Rect(50, 75, 30, 30), " - "))
    {
        resultant = valueA - valueB;
    }
    if (GUI.Button(new Rect(90, 75, 30, 30), "x "))
    {
        resultant = valueA * valueB;
    }
    if (GUI.Button(new Rect(130, 75, 30, 30), " / "))
    {
        resultant = valueA / valueB;
    }

     //copy to windows clipboard
    if (GUI.Button(new Rect(170, 110, 80, 30), "CopyResult"))                              
    {
        GUI.FocusControl("resultantField");
        TextEditor te2 = new TextEditor { content = new GUIContent(resultantString) };
        te2.SelectAll();
        te2.Copy();
    }

   //paste to here from windows clipboard[
    if (GUI.Button(new Rect(10, 110, 70, 30), "Paste Top"))                                
    {
        string topStringACB = ClipboardHelper.clipBoard;
        valueA = float.Parse (topStringACB);
    }
   
    if (GUI.Button(new Rect(85, 110, 70, 30), "Paste Bot"))
    {
        string botStringBCB = ClipboardHelper.clipBoard;
        valueB = float.Parse (botStringBCB);
    }   
}

}*
```

code from the site linked

```csharp
*// C#
// ClipboardHelper.cs
using UnityEngine;
using System;
using System.Reflection;

[System.Serializable]
public class ClipboardHelper
{
private static PropertyInfo m_systemCopyBufferProperty = null;
private static PropertyInfo GetSystemCopyBufferProperty()
{
if (m_systemCopyBufferProperty == null)
{
Type T = typeof(GUIUtility);
m_systemCopyBufferProperty = T.GetProperty(“systemCopyBuffer”, BindingFlags.Static | BindingFlags.NonPublic);
if (m_systemCopyBufferProperty == null)
throw new Exception(“Can’t access internal member ‘GUIUtility.systemCopyBuffer’ it may have been removed / renamed”);
}
return m_systemCopyBufferProperty;
}
public static string clipBoard
{
get
{
PropertyInfo P = GetSystemCopyBufferProperty();
return (string)P.GetValue(null,null);
}
set
{
PropertyInfo P = GetSystemCopyBufferProperty();
P.SetValue(null,value,null);
}
}
}*
```

1 Like

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.

3 Likes

it works perfectly fine!