Update/Deselect SelectableLabel

Say you have an EditorGUILayout.SelectableLabel in your GUI. The value for the label will not change if the text is currently selected, or if it has been previously selected.

How do you update a selectable label or manually deselect it so that it can be updated?

The SelectableLabel is just a wrapper for a TextArea which also just wraps the internal function DoTextField. The problem is that DoTextField uses an internal class (RecycledTextEditor) which is actually a persistent instance which holds an internal copy of the text.

To copy the selected text into the clipboard this editor class uses the internal property GUIUtility.systemCopyBuffer which wraps the systems clipboard. If you just want to copy a string into the clipboard you might be able to use Reflection to use GUIUtility.systemCopyBuffer. You just need to call the set method with your desired text.

edit

I’ve just created a wrapper for the internal property GUIUtility.systemCopyBuffer. You can simply read and write to the systems clipboard like this:

//C#
// Read
string s = ClipboardHelper.clipBoard;

// Write
ClipboardHelper.clipBoard = "Text to copy";

I’ve tested it in the editor and it works pretty well ;). Just place this script somewhere in your project (UnityScript users of course in the Standard Assets folder).

// C#
// ClipboardHelper.cs
using UnityEngine;
using System;
using System.Reflection;

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);
        }
    }
}

edit2
I just figured out that you can cancel editing by resetting GUIUtility.hotControl and GUIUtility.keyboardControl to 0.

GUIUtility.hotControl = 0;
GUIUtility.keyboardControl = 0;

This should naturally deselect the editfield.