AnimalSimulation/Assets/Scripts/DisplayServer.cs

165 lines
4.2 KiB
C#

using UnityEngine;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Collections.Generic;
public class DisplayServer : MonoBehaviour
{
private TcpListener listener;
private Thread listenThread;
private bool isRunning = true;
public Transform background;
[System.Serializable]
public class PageModel
{
public string pageName;
public GameObject model;
}
public List<PageModel> pageModels = new List<PageModel>();
private string latestMsg = "";
private string currentPage = "";
void Start()
{
Screen.SetResolution(3328, 1352, false);
listener = new TcpListener(IPAddress.Any, 8888);
listener.Start();
listenThread = new Thread(ListenForClients);
listenThread.Start();
Debug.Log("显示端服务器已启动,等待触摸端连接...");
ShowPage("首页");
}
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);
latestMsg = data;
}
}
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);
}
else if (latestMsg.StartsWith("Progress:"))
{
if (float.TryParse(latestMsg.Replace("Progress:", ""), out float progress))
{
SetAnimationProgress(progress);
}
}
latestMsg = "";
}
}
private void ShowPage(string pageName)
{
foreach (Transform child in background)
{
child.gameObject.SetActive(child.name == pageName);
}
currentPage = pageName;
foreach (var pageModel in pageModels)
{
if (pageModel.model != null)
{
bool isActive = pageModel.pageName == pageName;
pageModel.model.SetActive(isActive);
ModelController mc = pageModel.model.GetComponent<ModelController>();
if (mc != null)
{
mc.ResetAnimation();
}
}
}
Debug.Log($"显示端切换页面:{pageName}");
}
private void PlayAnimation(string animName)
{
PageModel pageModel = pageModels.Find(p => p.pageName == currentPage);
if (pageModel == null || pageModel.model == null) return;
ModelController mc = pageModel.model.GetComponent<ModelController>();
if (mc != null)
{
mc.PlayAnimation(animName);
}
}
private void SetAnimationProgress(float progress)
{
PageModel pageModel = pageModels.Find(p => p.pageName == currentPage);
if (pageModel == null || pageModel.model == null) return;
ModelController mc = pageModel.model.GetComponent<ModelController>();
if (mc != null)
{
mc.SetAnimationProgress(progress);
}
}
private void OnApplicationQuit()
{
isRunning = false;
listener.Stop();
}
}