Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.
Alexandru Dan edited this page Aug 29, 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 Djnago 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.

Deployment and Miscellaneous

Clone this wiki locally