129 lines
3.0 KiB
C#
129 lines
3.0 KiB
C#
using UnityEngine;
|
||
using System.Net.Sockets;
|
||
using System.Net;
|
||
using System.Text;
|
||
using System.Threading;
|
||
|
||
public class DisplayServer : MonoBehaviour
|
||
{
|
||
private TcpListener listener;
|
||
private Thread listenThread;
|
||
private bool isRunning = true;
|
||
|
||
<<<<<<< HEAD
|
||
public Transform background;
|
||
private string latestMsg = "";//最新信息
|
||
|
||
public Animator modelAnimator;//模型动画
|
||
=======
|
||
>>>>>>> 6d1a14ffd35fa4fa432a593073532c7106ac1d6d
|
||
|
||
void Start()
|
||
{
|
||
|
||
listener = new TcpListener(IPAddress.Any, 8888);
|
||
listener.Start();
|
||
|
||
listenThread = new Thread(ListenForClients);
|
||
listenThread.Start();
|
||
|
||
Debug.Log("显示端服务器已启动,等待触摸端连接...");
|
||
}
|
||
|
||
private void ListenForClients()//连接触摸控制端
|
||
{
|
||
while (isRunning)
|
||
{
|
||
TcpClient client = listener.AcceptTcpClient();
|
||
Debug.Log("触摸端已连接!");
|
||
|
||
Thread clientThread = new Thread(HandleClientComm);
|
||
clientThread.Start(client);
|
||
}
|
||
}
|
||
|
||
private void HandleClientComm(object clientObj)//处理触摸端发送的信息
|
||
{
|
||
TcpClient tcpClient = (TcpClient)clientObj;
|
||
NetworkStream clientStream = tcpClient.GetStream();
|
||
|
||
byte[] message = new byte[4096];
|
||
int bytesRead;
|
||
|
||
while (isRunning)
|
||
{
|
||
try
|
||
{
|
||
bytesRead = clientStream.Read(message, 0, 4096);
|
||
}
|
||
catch
|
||
{
|
||
Debug.Log("触摸端断开连接");
|
||
break;
|
||
}
|
||
|
||
if (bytesRead == 0) break;
|
||
|
||
string data = Encoding.UTF8.GetString(message, 0, bytesRead);
|
||
Debug.Log("收到指令: " + data);
|
||
|
||
<<<<<<< HEAD
|
||
private void Update()
|
||
{
|
||
if (!string.IsNullOrEmpty(latestMsg))
|
||
{
|
||
if (latestMsg.StartsWith("Page:"))
|
||
{
|
||
string pageName = latestMsg.Replace("Page:", "");
|
||
ShowPage(pageName);
|
||
}
|
||
else if (latestMsg.StartsWith("Anim:"))
|
||
{
|
||
string animName = latestMsg.Replace("Anim:", "");
|
||
PlayAnimation(animName);
|
||
}
|
||
|
||
latestMsg = "";
|
||
}
|
||
}
|
||
|
||
private void ShowPage(string pageName)//
|
||
{
|
||
if (background == null)
|
||
{
|
||
Debug.LogError($"BG未绑定!");
|
||
return;
|
||
}
|
||
|
||
foreach (Transform child in background)
|
||
{
|
||
child.gameObject.SetActive(child.name == pageName);
|
||
}
|
||
Debug.Log($"显示端切换页面:{pageName}");
|
||
}
|
||
|
||
private void PlayAnimation(string animName)
|
||
{
|
||
if (modelAnimator == null)
|
||
{
|
||
Debug.LogError("未绑定 Animator!");
|
||
return;
|
||
}
|
||
|
||
modelAnimator.Play(animName); // 播放指定动画
|
||
Debug.Log($"显示端播放动画:{animName}");
|
||
}
|
||
|
||
=======
|
||
|
||
}
|
||
}
|
||
|
||
>>>>>>> 6d1a14ffd35fa4fa432a593073532c7106ac1d6d
|
||
private void OnApplicationQuit()
|
||
{
|
||
isRunning = false;
|
||
listener.Stop();
|
||
}
|
||
}
|