Games in Unity
AirConsole Unity Plugin
20 min
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 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 \[ ] 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 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?) 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 unity store 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 assets > import package > custom package and choose the downloaded unitypackage unitypackage file make sure that all assets are selected and click on "import" 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 create > create other > airconsole 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 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) 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 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 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 airconsole > settings 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 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