146 lines
3.8 KiB
C#
146 lines
3.8 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; // 模型上挂载 ModelController
|
|
}
|
|
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 { break; }
|
|
|
|
if (bytesRead == 0) break;
|
|
|
|
string data = Encoding.UTF8.GetString(message, 0, bytesRead);
|
|
latestMsg = data;
|
|
}
|
|
|
|
tcpClient.Close();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (string.IsNullOrEmpty(latestMsg)) return;
|
|
|
|
if (latestMsg.StartsWith("Page:"))
|
|
{
|
|
string pageName = latestMsg.Replace("Page:", "");
|
|
ShowPage(pageName);
|
|
}
|
|
else if (latestMsg.StartsWith("Model:")) // 改成根据模型名播放动画
|
|
{
|
|
string modelName = latestMsg.Replace("Model:", "");
|
|
PlayModelAnimation(modelName);
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void PlayModelAnimation(string modelName)
|
|
{
|
|
PageModel pm = pageModels.Find(p => p.model != null && p.model.name == modelName);
|
|
if (pm != null)
|
|
{
|
|
ModelController mc = pm.model.GetComponent<ModelController>();
|
|
if (mc != null) mc.PlayAnimation(); // 播放模型默认动画
|
|
}
|
|
}
|
|
|
|
private void SetAnimationProgress(float progress)
|
|
{
|
|
PageModel pm = pageModels.Find(p => p.pageName == currentPage);
|
|
if (pm != null && pm.model != null)
|
|
{
|
|
ModelController mc = pm.model.GetComponent<ModelController>();
|
|
if (mc != null) mc.SetAnimationProgress(progress);
|
|
}
|
|
}
|
|
|
|
private void OnApplicationQuit()
|
|
{
|
|
isRunning = false;
|
|
listener.Stop();
|
|
}
|
|
}
|