手機/平板網頁 <--WebSocket--> Node.js 伺服器 <--WebSocket--> Unity
- 從 nodejs.org 下載並安裝 LTS 版本
mkdir unity-control-server
cd unity-control-server
npm init -y
npm install ws
將 server.js
檔案儲存到專案目錄
node server.js
成功後會顯示:
🚀 WebSocket 伺服器已啟動於 ws://0.0.0.0:3000
Windows:
ipconfig
尋找「IPv4 位址」,例如:192.168.1.100
Mac/Linux:
ifconfig
# 或
ip addr show
打開 HTML 檔案,修改第 190 行的伺服器位址:
const serverUrl = 'ws://192.168.1.100:3000'; // 改成你的內網 IP
選項 A:使用 Node.js 提供網頁(推薦)
建立 web-server.js
:
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 8080;
const server = http.createServer((req, res) => {
const filePath = path.join(__dirname, 'index.html');
fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(500);
res.end('Error loading page');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content);
}
});
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`🌐 網頁伺服器運行於 http://0.0.0.0:${PORT}`);
});
啟動:
node web-server.js
用手機瀏覽器開啟:http://192.168.1.100:8080
選項 B:使用任何 Web 伺服器
- IIS、Apache、Nginx 都可以
- 只需將 HTML 放在網頁根目錄
使用 Package Manager 安裝:
- 打開 Unity
- Window → Package Manager
- 點擊左上角
+
→ Add package from git URL - 輸入:
https://github.com/endel/NativeWebSocket.git#upm
- 在 Unity 專案中建立
Scripts
資料夾 - 建立新的 C# 腳本
UnityModeController.cs
- 複製提供的程式碼
- 在 Hierarchy 中建立空物件,命名為
ModeController
- 將
UnityModeController.cs
拖曳到該物件上 - 在 Inspector 中修改
Server Url
為你的內網位址:ws://192.168.1.100:3000
在 ApplyMode1()
~ ApplyMode4()
函式中實作你的邏輯,例如:
void ApplyMode1()
{
// 範例:切換 Skybox
RenderSettings.skybox = mode1Skybox;
// 範例:調整 HDRP Volume
Volume volume = FindObjectOfType<Volume>();
if (volume.profile.TryGet<ColorAdjustments>(out var colorAdj))
{
colorAdj.saturation.value = 0f; // 黑白模式
}
// 範例:切換光照
DirectionalLight.intensity = 1.5f;
}
- 啟動 Node.js 伺服器
- 啟動 Unity(Play Mode)
- 用手機開啟網頁
- 網頁應顯示「已連接」綠色狀態
- Unity Console 應顯示「✅ WebSocket 已連接」
- 在手機上點擊任一模式按鈕
- Unity Console 應顯示模式切換訊息
- Unity 場景應即時改變
- 檢查防火牆是否允許 3000 port
- 確認手機和電腦在同一個區域網路
- 確認 IP 位址正確
Windows 防火牆設定:
netsh advfirewall firewall add rule name="Unity Control" dir=in action=allow protocol=TCP localport=3000
netsh advfirewall firewall add rule name="Unity Web" dir=in action=allow protocol=TCP localport=8080
- 檢查
serverUrl
是否正確 - 檢查 NativeWebSocket 套件是否安裝成功
- 查看 Unity Console 錯誤訊息
- 確認網頁伺服器已啟動
- 在電腦瀏覽器先測試
http://localhost:8080
- 確認手機連接到相同 Wi-Fi
在伺服器加入檔案儲存:
const fs = require('fs');
fs.writeFileSync('mode.json', JSON.stringify({ mode: currentMode }));
防止未授權存取:
ws.on('message', (message) => {
const data = JSON.parse(message);
if (data.token !== 'YOUR_SECRET_TOKEN') {
ws.close();
return;
}
// 處理訊息...
});
為每個 Unity 實例分配 ID:
const unityClients = new Map();
ws.on('connection', (ws) => {
ws.on('message', (message) => {
const data = JSON.parse(message);
if (data.type === 'register') {
unityClients.set(data.unityId, ws);
}
});
});
✅ 即時響應 - WebSocket 推送,無延遲
✅ 低資源消耗 - 不需要輪詢
✅ 連線狀態可視 - 即時顯示連接狀況
✅ 易於擴展 - 可輕鬆加入更多功能
✅ 跨平台 - 任何有瀏覽器的裝置都能使用
如果遇到問題:
- 檢查所有 Console 的錯誤訊息
- 確認網路連接狀態
- 使用
ping
指令測試網路連通性 - 檢查防火牆設定