0%

Websocket将Unity日志输出到网页

Unity中查看日志时如果很多就会特别卡,或者在手机上时查看日志很费劲,尝试了一下用Websocket输出到网页上,方便查看。

服务端

服务端为了方便使用node.js,只需要十几行代码即可完成。

index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});

var userId = 0;
io.on('connection', function(socket){
socket.userId = userId ++;
console.log('a user connected, user id: ' + socket.userId);

socket.on('log', function(msg){
console.log('message from user#' + socket.userId + ": " + msg);
io.emit('log', {
id: socket.userId,
msg: msg
});
});
});

http.listen(3000, function(){
console.log('listening on *:3000');
});

index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>

<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('log', $('#m').val());
$('#m').val('');
return false;
});
socket.on('log', function(data){
$('#messages').append($('<li>').text("user#" + data.id + ": " + data.msg));
});
});
</script>

<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
</body>
</html>

package.json

1
2
3
4
5
6
7
8
9
{
"name": "socket.io-unity-test-server",
"version": "0.0.1",
"description": "test server in node.js for socket.io-unity",
"dependencies": {
"express": "^4.15.3",
"socket.io": "^2.0.1"
}
}

运行服务端

1
2
npm install
node index.js

客户端

C#代码,挂在场景中的某个组件上即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Quobject.SocketIoClientDotNet.Client;

public class SocketIOScript : MonoBehaviour {
public string serverURL = "http://localhost:3000";
protected Socket socket = null;
float time = 0;

void Destroy() {
DoClose ();
}

// Use this for initialization
void Start () {
DoOpen ();
}

// Update is called once per frame
void Update () {
time += Time.deltaTime;
if (time >= 5)
{
SendChat(Time.time.ToString());
time = 0;
}
}

void DoOpen() {
if (socket == null) {
socket = IO.Socket (serverURL);
socket.On (Socket.EVENT_CONNECT, () => {
SendChat("================");
});
}
}

void DoClose() {
if (socket != null) {
socket.Disconnect ();
socket = null;
}
}

void SendChat(string str) {
if (socket != null) {
socket.Emit ("log", str);
}
}
}

最后把Unity的Debug.Log封装一下,里面在输出日志的时候SendChat函数即可。

客户端使用Websocket依赖的库:下载地址

Snipaste_2019-11-29_16-10-13.png

目前缺点是这几个库很容易跟其他插件冲突,暂时还没找到C#合适的Websocket库。

运行效果

Snipaste_2019-11-29_16-21-22.png