Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
Olaf Szmidt edited this page Sep 13, 2017 · 11 revisions

The Client Communication

The Unity Client supported talks to the Game component directly using WebSockets protocol. The communication is supported though the Socket IO API, the same communication used by the dummy game Raphael JS view.

We use specialized plugin in the AI:MMO Unity repository.

Integration with Django and Javascript

The AI:MMO Unity repository deploys to WebGL. The Django server servers the deployed Unity client, but the communication script has to tweaked to reference a socket.io Javascipt implementation as the Unity build does not reference all the dependencies.

The template should reference the right locations for the deployed files as below:

<script type='text/javascript'>
var Module = {
  TOTAL_MEMORY: 1000000000,
  errorhandler: handler,  // arguments: err, url, line. This function must return 'true' if the error is handled, otherwise 'false'
  compatibilitycheck: null,
  backgroundColor: "#222C36",
  splashStyle: "Light",
  dataUrl: "/static/unity/Development/Build.data", // this locations should reference the path of the deployment in the project
  codeUrl: "/static/unity/Development/UnityEngine.js",
  asmUrl: "/static/unity/Development/UnityEngine.asm.js",
  memUrl: "/static/unity/Development/UnityEngine.mem",
  dynamicLibraries: ["/static/unity/Development/Build.js"],
};
</script>
<script src="/static/unity/Development/UnityLoader.js"></script> <!-- the loader script should be include after the Web Assembly module declaration -->

We need to supply the client with relevant paths for the Game and the identity of the current player. This variables should be retrieved using the Django template engine:

var GAME_URL_BASE = "{{ game_url_base }}";
var GAME_URL_PATH = "{{ game_url_path }}";
var VIEW_OWNER_ID = "{{ view_owener_id }}";

Finally, to communicate from Unity(WebGL deployment) to Javascript, we can reference Javascript functions from Unity. An example of such reference function is SendConnectAll, which is called from the called from WorldControlls class from AI:MMO Unity repository.

To communicate from Javascript to Unity, we use the function SendMessage. An important detail is that the whole Web Assembly module has to be loaded before the SendMessage function is called, thus we should communicate in order:

  1. Unity to JS referencing Javacript functions from Unity
  2. JS to Unity using SendMessage
function SendAllConnect() {
  console.log("Sending Messages.");
  SendMessage("World Manager", "SetGameURL", host);
  SendMessage("World Manager", "SetGamePort", parseInt(port));
  SendMessage("World Manager", "SetUserId", parseInt(user_id))
  SendMessage("World Manager", "EstablishConnection");
}

An example of referencing JS functions from Unity is:

if (Application.platform == RuntimePlatform.WebGLPlayer) 
{
    Application.ExternalCall("SendAllConnect");
}

A working client can be found here.

Another frequent problem that we encountered is that the Module loader is not aware of the fact that it is running at a different location, so it might request static resources from the wrong folder. A workaround is redirecting the requests from the urls.py:

url(r'^watch/(?P<id>[0-9]+)/(?P<resource>.[0-9A-Za-z/.]+)$',
    RedirectView.as_view(url='/static/unity/%(resource)s', permanent=False)),

Deployment and Miscellaneous

More information on deployment can be found inside the AI:MMO Unity repository. The only CI platform we use in aimmo-unity is Travis. We store the final deployment inside Google Cloud Storage. The Package deployed should be compressed as the compressed size is around 10 times smaller. The build and download scripts are inside the Scripts folder.

Useful links

Socket.IO:

WebGL Unity Deployment:

Clone this wiki locally