Games in Unity
AirConsole Unity Plugin
19 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 true 220,220,221 unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type download download from github download the newest airconsole unity plugin on our http //github com/airconsole/airconsole unity plugin you can find the newest " unitypackage" file in the "builds" folder download from assetstore 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 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 true 330,331 unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type unhandled content type 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 https //developers2 airconsole com/unity game on android tv documentation to adapt your game for androidtv
