unity3D使用通过UDP协议实现远程唤醒计算机。
using UnityEngine; using UnityEngine.UI; using System.Net.Sockets; /// /// 唤醒主机 /// public class WakeUp : MonoBehaviour { public Text macText; public Text resultText;//提示文字 public void RemoteWakeUpComputer() { byte[] macByte = FormatMac(macText.text); UdpClient client = new UdpClient(); client.Connect(System.Net.IPAddress.Broadcast, 50000); byte[] packet = new byte[17 * 6]; for (int index = 0; index < 6; index++) packet[index] = 0xFF; for (int i = 1; i <= 16; i++) for (int j = 0; j < 6; j++) packet[i * 6 + j] = macByte[j]; int sendLength = client.Send(packet, packet.Length); if (sendLength != packet.Length) resultText.text = "唤醒失败"; else resultText.text = "正在唤醒"; } byte[] FormatMac(string macInput) { byte[] mac = new byte[6]; string str = macInput; string[] strArray = str.Split('-'); for (int index = 0; index < 6; index++) { var byteValue = System.Convert.ToByte(strArray[index], 16); mac[index] = byteValue; } return mac; } void Update() { if (macText.text.Length == 0) resultText.text = null; else if (macText.text.Length != 17) resultText.text = "输入的Mac地址错误!格式:34-97-F6-59-76-9B"; } } 需要在同一个局域网中UDP的广播才能唤醒你需要的计算机。 如果不在同一个路由中,就需要添加代码了, IPAddress ip = new IPAddress(new byte[]{192,168,1,255});//在这里设置你需要的网段(你需要唤醒的计算机在那个网段上就设置什么网段) client.Connect(ip, 50000);