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