Example project using:
- Unity 2018.x
- Node 8.x
- Ws (Websockets) 6.1.x
npm install
- Create a new Unity project: Unity > File > New Project
- Install Simple Web Sockets for Unity WebGL: https://assetstore.unity.com/packages/essentials/tutorial-projects/simple-web-sockets-for-unity-webgl-38367
- Open the EchoTest Scene and update the EchoTest.cs file to support objects
using UnityEngine;
using System.Collections;
using System;
public class Message
{
public string type;
public string data;
}
public class EchoTest : MonoBehaviour {
IEnumerator Start () {
WebSocket w = new WebSocket(new Uri("ws://localhost:3000"));
yield return StartCoroutine(w.Connect());
Message sendObject = new Message();
sendObject.type = "init";
sendObject.data = "unity";
w.SendString(JsonUtility.ToJson(sendObject));
Debug.Log("Init");
int i = 0;
while (true)
{
string reply = w.RecvString();
if (reply != null)
{
Message replyObject = JsonUtility.FromJson<Message>(reply);
Debug.Log("Received: " + replyObject.type + " - " + replyObject.data);
if (replyObject.type == "action" && replyObject.data == "move")
{
Camera.current.transform.Translate(new Vector3(1.0f, 0.0f, 0.0f));
}
// Send screen capture back via websockets
yield return new WaitForEndOfFrame();
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
byte[] bytes = tex.EncodeToPNG();
UnityEngine.Object.Destroy(tex);
w.Send(bytes);
}
if (w.error != null)
{
Debug.LogError ("Error: "+w.error);
break;
}
yield return 0;
}
w.Close();
}
}
- Apply a patch to support string messages: sta/websocket-sharp#181 (comment)
Create a build at: Unity > File > Build Settings > WebGL and build to this project folder:
/src
Run the websocket backend using:
node server.js
In a second terminal window, run the Unity WebGL frontend:
node client.js
View the frontend at:
http://localhost:8080/
Create a build at: Unity > File > Build Settings > Standalone and build to this project folder:
/src-standalone
Run the websocket backend using:
node server.js
In a second terminal window, run the frontend:
node client-standalone.js
In a third terminal window, run the Unity standalone app:
open src-standalone/unity.app
View the frontend at:
http://localhost:8080/
For more information please contact kmturley