---
title: AirConsole Unity Plugin
slug: airconsole-unity-plugin
docTags: 
createdAt: 2024-07-30T14:44:34.215Z
---

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.

:::BlockQuote
**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:<br /><br /><font color="#eb144c">`mainTemplate.gradle is using the old aaptOptions noCompress property definition does not include types defined in unityStreamingAssets constant.`</font>                                                                                                     | Edit **Assets/Plugins/Android/mainTemplate.gradle** and **launcherTemplate.gradle** to add/replace the **aaptOptions** section to the following:<br />:::BlockQuote
aaptOptions \{&#xA;    noCompress = \['.ress', '.resource', '.obb'] + unityStreamingAssets.tokenize(', ')&#xA;    ignoreAssetsPattern = "!.svn:!.git:!.ds\_store:!\*.scc:.\*:!CVS:!thumbs.db:!picasa.ini:!\*\~"&#xA;}\*\*PACKAGING\_OPTIONS\*\*
:::<br /><br />Check [Unity support](https://developers2.airconsole.com/faq-help) for more details. |
| Unity 2021+       | After importing the AirConsole plugin into Unity you may get this error: <br /><br /><font color="#eb144c">`Multiple precompiled assemblies with the same name Newtonsoft.Json.dll included on the current platform. [...]`</font>                                                                                                                          | Delete the following plugin file (It is now included in Unity directly and is not needed in newer Unity versions): <font color="#eb144c">`Assets/AirConsole/plugins/Newtonsoft.Json.dll`</font>                                                                                                                                                                                                                                                                                                                         |
| Unity 2018.3      | After importing the AirConsole plugin into Unity you may get:<br /><font color="#eb144c">`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?)`</font> | Set the **API Compatibility Level** to **.NET 4.x** in the Player Settings of your selected Platform.<br /><br />Check [AirConsole FAQ](https://developers2.airconsole.com/faq-help)  for more details.                                                                                                                                                                                                                                                                                                                 |

### *Download*

**Download from Github**

Download the newest AirConsole-Unity-Plugin on our [GitHub repository](http://github.com/AirConsole/airconsole-unity-plugin). You can find the newest ".unitypackage" file in the "Builds" folder.

**Download from Assetstore**

[Unity Store](https://assetstore.unity.com/packages/tools/game-toolkits/airconsole-42776)

### *Video Tutorial*

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

::embed[]{url="https://www.youtube.com/embed/omKvqRZbfY8"}

### *Installation*

To import the plugin, click on <font color="#eb144c">`Assets > Import Package -> Custom Package`</font> and choose the downloaded <font color="#eb144c">`*.unitypackage`</font> file.

::Image[]{src="https://api.archbee.com/api/optimize/eODGJqzvP6Walum1Gn5N9/DLJWExYYGEHZ5ObQU96KQ_image.png" size="80" width="456" height="246" position="center" showCaption="false"}

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

::Image[]{src="https://api.archbee.com/api/optimize/eODGJqzvP6Walum1Gn5N9/X8uCcFWbrtCI_jfMYyZ27_image.png" size="70" width="366" height="384" position="center" showCaption="false"}

**Instantiate AirConsole object**

To set up the communication inside a scene, you have to instantiate the AirConsole object. In the Hierarchy window, click on <font color="#eb144c">`Create > Create Other > AirConsole`</font>.

::Image[]{src="https://api.archbee.com/api/optimize/eODGJqzvP6Walum1Gn5N9/DoLR-NMNEqNINm-PjJUhD_image.png" size="60" width="350" height="270" position="center" showCaption="false"}

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.                                                                                                                           |

::Image[]{src="https://api.archbee.com/api/optimize/eODGJqzvP6Walum1Gn5N9/78kUQccxPrshav9aLnUWq_image.png" size="60" width="321" height="283" position="center" showCaption="false"}

:::BlockQuote
**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:

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

**/controller.html**

:::BlockQuote
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:

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

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

:::BlockQuote
void Start() \{
&#x20;   AirConsole.instance.onMessage += OnMessage;
}

void OnMessage(int from, JToken data) \{
&#x20;   AirConsole.instance.Message(from, "Full of pixels!");
}
:::

:::BlockQuote
**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](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:

:::BlockQuote
// Javascript
var message = \{
&#x20; 'action': 'move',
&#x20; 'info': \{ 'amount': 5, 'torque': 234.8 }
};

air\_console.message(AirConsole.SCREEN, message);
:::

You can parse the object in Unity like this:

:::BlockQuote
// C#
void OnMessage(int from, JToken data) \{
&#x20; string action = (string) data\["action"];
&#x20; int amount = (int) data\["info"]\["amount"];
&#x20; float torque = (float) data\["info"]\["torque"];
}
:::

:::BlockQuote
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:

:::BlockQuote
// C#
var message = new \{
&#x20;   action = "move",
&#x20;   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):

::Image[]{src="https://api.archbee.com/api/optimize/eODGJqzvP6Walum1Gn5N9/0A4IF7aN04TvX6GBCPwQm_image.png" size="80" width="400" height="379" position="center" showCaption="false"}

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.

::Image[]{src="https://api.archbee.com/api/optimize/eODGJqzvP6Walum1Gn5N9/rbRSVhPH6n-smRLYYvL54_image.png" size="50" width="250" height="178" position="center" showCaption="false"}

:::BlockQuote
**Note:** the "Open Exported Port" button is only available as long as you don’t change the last target location of your exported build.
:::

:::BlockQuote
**Note:** AirConsole uses the Unity WebGL Custom Progress Bar by Alexander Ocias for WebGL builds: https\://ocias.com/
:::

:::BlockQuote
**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.

::Image[]{src="https://api.archbee.com/api/optimize/eODGJqzvP6Walum1Gn5N9/uOouAd15U-Yv5oP61kD32_image.png" size="60" width="895" height="557" position="center" showCaption="false"}

### *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 <font color="#eb144c">`AirConsole > Settings`</font>.

::Image[]{src="https://api.archbee.com/api/optimize/eODGJqzvP6Walum1Gn5N9/qXF2nNM3Genls_VRzoHra_image.png" size="60" width="417" height="147" position="center" showCaption="false"}

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

:::BlockQuote
**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.

::Image[]{src="https://api.archbee.com/api/optimize/eODGJqzvP6Walum1Gn5N9/rEp6aJltjg85SAZsIlk6E_image.png" size="60" width="336" height="278" position="center" showCaption="false"}

### *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"](https://developers2.airconsole.com/unity-game-on-android-tv) documentation to adapt your game for AndroidTV.
