Games in Unity

AirConsole Unity Plugin

20min

The Unity-Plugin is a C# wrapper for the AirConsole Javascript API. With the plugin, Unity serves you the needed screen.html file inside the Editor and creates a screen.html file after an WebGL build export.

Note: The plugin comes with an embedded webserver / websocket-server for the communication between the AirConsole backend and the Unity-Editor. You don't need to install any other webserver or services. The plugin was tested with Unity 5.1 and above for the Windows and OSX-Editor.

Known issues and solutions

Before you start installing the plugin, be aware that because we support such a wide range of Unity versions, some Unity versions have some known issues when importing the plugin or when building for a specific platform. Below you will find the list of known issues and their fixes:

Affected versions

Description

Solution

Unity 2022+

After trying to build for Android TV you may get this error: mainTemplate.gradle is using the old aaptOptions noCompress property definition does not include types defined in unityStreamingAssets constant.

Edit Assets/Plugins/Android/mainTemplate.gradle and launcherTemplate.gradle to add/replace the aaptOptions section to the following:

aaptOptions { noCompress = ['.ress', '.resource', '.obb'] + unityStreamingAssets.tokenize(', ') ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~" }**PACKAGING_OPTIONS**

Check Unity support for more details.

Unity 2021+

After importing the AirConsole plugin into Unity you may get this error:

Multiple precompiled assemblies with the same name Newtonsoft.Json.dll included on the current platform. [...]

Delete the following plugin file (It is now included in Unity directly and is not needed in newer Unity versions): Assets/AirConsole/plugins/Newtonsoft.Json.dll

Unity 2018.3

After importing the AirConsole plugin into Unity you may get: The type or namespace name 'ILGenerator' could not be found (are you missing a using directive or an assembly reference?) and The type or namespace name 'DynamicMethod' could not be found (are you missing a using directive or an assembly reference?)

Set the API Compatibility Level to .NET 4.x in the Player Settings of your selected Platform. Check AirConsole FAQ for more details.

Download

Download from Github

Download the newest AirConsole-Unity-Plugin on our GitHub repository. You can find the newest ".unitypackage" file in the "Builds" folder.

Download from Assetstore

Video Tutorial

Learn the basics of how to make an AirConsole game in Unity in just 4 minutes.



Installation

To import the plugin, click on Assets > Import Package -> Custom Package and choose the downloaded *.unitypackage file.

Document image


Make sure that all assets are selected and click on "Import".

Document image


Instantiate AirConsole object

To set up the communication inside a scene, you have to instantiate the AirConsole object. In the Hierarchy window, click on Create > Create Other > AirConsole.

Document image


The AirConsole object offers you the following options:

Option

Description

Browser Start Mode

As soon as you press the “Play” button in Unity, your default browser will open the AirConsole simulator in the chosen mode.

Controller Html

Link your controller.html file to this inspector variable, otherwise you can’t start the AirConsole simulator and you will get an error message. (Note: if your controller needs additional files like images or fonts, put them in the folder “Assets/WebGLTemplates/AirConsole”)

Auto Scale Canvas

This option is enabled by default and scales your final webgl canvas automatically to the window size. The aspect ratio will be kept during the scaling.

Document image

Note: the AirConsole object survives a scene change by default, so you don’t have to worry about the connections and device states when changing a level inside Unity).

Quick example

Here is a simple example where the controller sends "How are you?" to the screen and the screen replies with "Full of pixels!"

Include the AirConsole Javascript API in your controller.html file:

<script type="text/javascript" src="https://www.airconsole.com/api/airconsole-1.9.0.js"></script>

/controller.html

var air_console = new AirConsole(); air_console.message(AirConsole.SCREEN, "How are you?");

Unity

To be able to receive a message in Unity, create a new Unity MonoBehaviour class and include these namespaces:

using NDream.AirConsole; using Newtonsoft.Json.Linq;

Add an event handler in the Start method of the MonoBehaviour class:

void Start() { AirConsole.instance.onMessage += OnMessage; } void OnMessage(int from, JToken data) { AirConsole.instance.Message(from, "Full of pixels!"); }
Note: make sure to only send JSON supported data types between the controller.html file and Unity. The AirConsole-Unity-Plugin exchanges data via a modifed version of the popular JSON.NET library. The plugin delivers received data inside Unity as JToken objects. You can check the documentation of JSON.NET here: http://www.newtonsoft.com/json/help/html/N_Newtonsoft_Json_Linq.htm

JToken parse example

When you send an object like this one on the controller.html side:

// Javascript var message = { 'action': 'move', 'info': { 'amount': 5, 'torque': 234.8 } }; air_console.message(AirConsole.SCREEN, message);

You can parse the object in Unity like this:

// C# void OnMessage(int from, JToken data) { string action = (string) data["action"]; int amount = (int) data["info"]["amount"]; float torque = (float) data["info"]["torque"]; }
Note: when you receive JToken data in Unity, you have to cast the object to get the right data type.

To send the same data from Unity you can use dynamic vars:

// C# var message = new { action = "move", info = new { amount = 5, torque = 234.8f } }; AirConsole.instance.Message(device_id, message);

Test your game

To test your game, just hit the “Play” button inside the Unity-Editor. If you want to test the final WebGL-Build, choose the right AirConsole Template in the PlayerSettings first (For Unity versions 2020 and up choose the AirConsole-2020 template, for previous Unity versions choose the AirConsole template):

Document image


Now you can export your Unity project in the Build-Settings (choose “Build” instead of the “Build and run” button). As soon as your build is exported, the button “Open Exported Port” on the AirConsole object in your scene will appear. By pressing this button, the AirConsole simulator will start with your final build.

Document image

Note: the "Open Exported Port" button is only available as long as you don’t change the last target location of your exported build.
Note: AirConsole uses the Unity WebGL Custom Progress Bar by Alexander Ocias for WebGL builds: https://ocias.com/
IMPORTANT: before you can build the WebGL port, you have to start the game at least once in the Editor to generate all important metafiles for the final build.

Examples



  • AirConsole/Examples/Basic/basic.scene: In this example all possible API functions are listed & used.
  • AirConsole/Examples/Pong/pong.scene: This example contains a simple pong game with a specific controller.
Document image


Settings

You can open the settings window by clicking on the "Settings" button on the inspector of the AirConsole object or by navigating over the menu bar AirConsole > Settings.

Document image


In the "Settings" window, the ports for the internal web socket and web server can be changed, should the default ports already be occupied.

Note: These ports are only needed for the AirConsole simulator inside the Unity Editor. The final WebGL port will not use any of these ports.

You can stop and restart the internal web server in case the AirConsole simulator isn't working properly.

Document image


AirConsole, Unity and AndroidTV

AirConsole also provides an AndroidTV app, so that user can play your games also on a smart-tv. Please read the "AirConsole Unity on AndroidTv" documentation to adapt your game for AndroidTV.