背景
使用CI/CD
構建unity
專案, shell
指令碼呼叫npm script
拉起unity
後UnityEngin.Debug.Log
的輸出只會展示在unity
的控制檯裡, 那麼如何將其輸出到終端以便監控其構建過程呢
解決方案
將要輸出日誌寫入到
log.txt
檔案
public static void Print(string str) { using (StreamWriter logFile = new StreamWriter('log.txt', true)) logFile.WriteLine($"[{DateTime.Now}] {str}"); UnityEngine.Debug.Log(str); }
使用
node.js
監聽log.txt
檔案並列印到終端
要注意避免檔案系統多次觸發change
事件導致的重複列印, 另外要注意停止檔案監聽以便退出構建流程
程式碼如下
let watcher; function unityConsoleLog() { const fs = require("fs"); const readline = require("readline"); const path = require("path"); const filePath = path.join(__dirname, "log.txt"); let lastKnownPosition = 0; // 檢查檔案是否存在 if (fs.existsSync(filePath)) { // 檔案存在,清空檔案內容 fs.writeFile(filePath, "", (err) => { if (err) { console.error("Failed to clear file:", err); } else { console.log(filePath + " File content has been cleared."); } }); } else { // 檔案不存在,建立檔案 fs.writeFile(filePath, "", (err) => { if (err) { console.error("Failed to create file:", err); } else { console.log(filePath + " File has been created."); } }); } let processing = false; let lastLineHashes = new Set(); function readNewLines(start) { const stream = fs.createReadStream(filePath, { start: start, encoding: "utf8", }); const rl = readline.createInterface({ input: stream, output: process.stdout, terminal: false, }); processing = true; rl.on("line", (line) => { const lineHash = hashLine(line); if (!lastLineHashes.has(lineHash)) { console.log("Unity Build Log: ", line); lastLineHashes.add(lineHash); } }); rl.on("close", () => { lastKnownPosition = fs.statSync(filePath).size; processing = false; }); } function hashLine(line) { let hash = 0; for (let i = 0; i < line.length; i++) { let char = line.charCodeAt(i); hash = (hash << 5) - hash + char; hash |= 0; } return hash; } watcher = fs.watch(filePath, (eventType, filename) => { if (eventType === "change" && !processing) { readNewLines(lastKnownPosition); } }); // Initialize the start of the file watching fs.stat(filePath, (err, stats) => { if (err) { console.error("File not found:", err); return; } lastKnownPosition = stats.size; }); } function stopUnityConsoleLog() { if (watcher) { watcher.close(); console.log("Stopped watching file."); } else { console.log("No watcher to stop."); } }