今天有点闲,有点闲,有点闲...然后我用一个小时做了一个小功能:用一个电脑A控制另一台电脑B中两个正在运行的程序在屏幕上的显示顺序,也就是点击电脑A中的按钮C,将电脑B中的程序C显示在屏幕最前方;点击电脑A中的按钮D,将电脑B中的程序D显示在屏幕最前方,如下图所示(上传图片限制大小,将就着看吧)【电脑A可以替换成手机,充当一个遥控的作用】
用到的功能:
- 局域网服务器、客户端通信;也可见文章TcpListener与TcpClient
- Windows用户界面相关应用程序接口的运用;
代码如下:
- 服务器端
using System.Collections.Generic;
using UnityEngine;
using System.Net.Sockets;
using System.Net;
using System.Text;
using UnityEngine.UI;
using System.Threading;
public class Server : MonoBehaviour
{
public static Server _instance;
private TcpListener tcpListener;
private TcpClient client1;
private TcpClient client2;
private string ip = "10.1.12.21";//本地ip
private int port = 9001;
private ClientSocket clientSocket1;
private ClientSocket clientSocket2;
private Button buttonA;
private Button buttonB;
public bool clientA = false;
public bool clientB = false;
private void Awake()
{
_instance = this;
buttonA = transform.Find("buttonA").GetComponent<Button>();
buttonA.onClick.AddListener(delegate
{
clientA = true;
});
buttonB = transform.Find("buttonB").GetComponent<Button>();
buttonB.onClick.AddListener(delegate
{
clientB = true;
}