├── .gitignore ├── LICENSE ├── README.md ├── package.json ├── plugin.xml ├── src └── ios │ └── Unity.m └── www └── Unity.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Olivier Louvignes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Cordova Unity](https://github.com/mgcrea/cordova-unity) [![Release](https://img.shields.io/github/release/mgcrea/cordova-unity.svg)](https://github.com/mgcrea/cordova-unity/releases) 2 | 3 | This plugin provides a simple way to integrate and communicate with an Unity view. 4 | 5 | * This plugin is built for Cordova >= v2.1.0 with ARC, it has been tested to work without ARC. 6 | 7 | * This plugin currently supports iOS. 8 | 9 | 10 | ## Plugin setup 11 | 12 | Using this plugin requires [Cordova iOS](https://github.com/apache/cordova-ios). 13 | 14 | 1. `cordova plugin add io.mgcrea.Unity` 15 | 16 | 17 | ## Javascript interface 18 | 19 | // After device ready, create a local alias 20 | var Unity = cordova.plugins.Unity; 21 | 22 | Unity.show({x: 200, y: 200, width: 512, height: 384}); 23 | Unity.sendMessage("foo;bar"); 24 | Unity.pause(true); 25 | Unity.hide(); 26 | 27 | * Check [source](https://github.com/mgcrea/cordova-unity/tree/master/www/Unity.js) for additional configuration. 28 | 29 | 30 | ## Communication 31 | 32 | - If you **need help**, use [Stack Overflow](http://stackoverflow.com/questions/tagged/cordova). (Tag `cordova`) 33 | - If you'd like to **ask a general question**, use [Stack Overflow](http://stackoverflow.com/questions/tagged/cordova). 34 | - If you **found a bug**, open an issue. 35 | - If you **have a feature request**, open an issue. 36 | - If you **want to contribute**, submit a pull request. 37 | 38 | 39 | ## Contributing 40 | 41 | Patches welcome! Send a pull request. Since this is not a part of Cordova Core (which requires a CLA), this should be easier. 42 | 43 | Please submit all pull requests the against master branch. If your pull request contains JavaScript patches or features, you should include relevant unit tests. Thanks! 44 | 45 | 46 | ## Authors 47 | 48 | **Olivier Louvignes** 49 | 50 | + http://olouv.com 51 | + http://github.com/mgcrea 52 | 53 | 54 | ## Copyright and license 55 | 56 | The MIT License (MIT) 57 | 58 | Copyright (c) 2015 Olivier Louvignes 59 | 60 | Permission is hereby granted, free of charge, to any person obtaining a copy 61 | of this software and associated documentation files (the "Software"), to deal 62 | in the Software without restriction, including without limitation the rights 63 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 64 | copies of the Software, and to permit persons to whom the Software is 65 | furnished to do so, subject to the following conditions: 66 | 67 | The above copyright notice and this permission notice shall be included in all 68 | copies or substantial portions of the Software. 69 | 70 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 71 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 72 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 73 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 74 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 75 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 76 | SOFTWARE. 77 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cordova-plugin-unity", 3 | "version": "0.1.1", 4 | "description": "Cordova Unity Plugin", 5 | "cordova": { 6 | "id": "cordova-plugin-unity", 7 | "platforms": [ 8 | "ios" 9 | ] 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/mgcrea/cordova-plugin-unity" 14 | }, 15 | "keywords": [ 16 | "cordova", 17 | "unity", 18 | "ecosystem:cordova", 19 | "cordova-ios" 20 | ], 21 | "engines": [ 22 | { 23 | "name": "cordova-ios", 24 | "version": ">=3.6.0" 25 | } 26 | ], 27 | "author": "Apache Software Foundation", 28 | "license": "Apache 2.0" 29 | } -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Unity 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/ios/Unity.m: -------------------------------------------------------------------------------- 1 | /********* Unity.m Cordova Plugin Implementation *******/ 2 | 3 | #import 4 | #import "UI/UnityView.h" 5 | #import "UnityAppController.h" 6 | 7 | @interface Unity : CDVPlugin { 8 | UnityAppController* _appDelegate; 9 | UnityView* _unityView; 10 | UIView* _rootView; 11 | } 12 | 13 | @property (nonatomic, strong) UIViewController* unityViewController; 14 | - (void)show:(CDVInvokedUrlCommand*)command; 15 | - (void)hide:(CDVInvokedUrlCommand*)command; 16 | - (void)sendMessage:(CDVInvokedUrlCommand*)command; 17 | @end 18 | 19 | @implementation Unity 20 | 21 | - (CDVPlugin*)initWithWebView:(UIWebView*)theWebView 22 | { 23 | DLog(@"initWithWebView: theWebView"); 24 | self = [super initWithWebView:theWebView]; 25 | if (self) { 26 | _appDelegate = (UnityAppController *)[[UIApplication sharedApplication] delegate]; 27 | _unityView = _appDelegate.unityView; 28 | _rootView = _appDelegate.rootView; 29 | } 30 | return self; 31 | } 32 | 33 | - (void)show:(CDVInvokedUrlCommand*)command 34 | { 35 | CDVPluginResult* pluginResult = nil; 36 | 37 | float x = [[command argumentAtIndex:0] floatValue]; 38 | float y = [[command argumentAtIndex:1] floatValue]; 39 | float width = [[command argumentAtIndex:2] floatValue]; 40 | float height = [[command argumentAtIndex:3] floatValue]; 41 | DLog(@"show: [%f,%f,%f,%f]", x, y, width, height); 42 | 43 | CGRect unityFrame = CGRectMake(x, y, width, height); 44 | _unityView.frame = unityFrame; 45 | [_rootView addSubview:_unityView]; 46 | 47 | pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:YES]; 48 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 49 | } 50 | 51 | - (void)hide:(CDVInvokedUrlCommand*)command 52 | { 53 | CDVPluginResult* pluginResult = nil; 54 | DLog(@"hide:"); 55 | 56 | [_unityView removeFromSuperview]; 57 | 58 | pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:YES]; 59 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 60 | } 61 | 62 | - (void)pause:(CDVInvokedUrlCommand*)command 63 | { 64 | CDVPluginResult* pluginResult = nil; 65 | 66 | bool state = [[command argumentAtIndex:0] boolValue]; 67 | DLog(@"pause: [%d]", state ? 1 : 0); 68 | 69 | UnityPause(state); 70 | 71 | pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:YES]; 72 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 73 | } 74 | 75 | - (void)sendMessage:(CDVInvokedUrlCommand*)command 76 | { 77 | CDVPluginResult* pluginResult = nil; 78 | NSString* message = [command.arguments objectAtIndex:0]; 79 | DLog(@"sendMessage: [%@]", message); 80 | 81 | UnitySendMessage("DeviceCommunication", "MobileToUnity", [message UTF8String]); 82 | 83 | pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:YES]; 84 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; 85 | } 86 | 87 | @end 88 | -------------------------------------------------------------------------------- /www/Unity.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var exec = require('cordova/exec'); 4 | 5 | function defaults(object, source) { 6 | if(!object) object = {}; 7 | for(var prop in source) { 8 | if(typeof object[prop] === 'undefined') { 9 | object[prop] = source[prop]; 10 | } 11 | } 12 | return object; 13 | } 14 | 15 | exports.show = function(options, success, error) { 16 | defaults(options, { 17 | x: 200, 18 | y: 200, 19 | width: 512, 20 | height: 384 21 | }); 22 | var args = [options.x, options.y, options.width, options.height]; 23 | exec(success, error, 'Unity', 'show', args); 24 | }; 25 | 26 | exports.hide = function(arg0, success, error) { 27 | exec(success, error, 'Unity', 'hide', []); 28 | }; 29 | 30 | exports.pause = function(state, success, error) { 31 | exec(success, error, 'Unity', 'pause', [!!state]); 32 | }; 33 | 34 | exports.sendMessage = function(message, success, error) { 35 | exec(success, error, 'Unity', 'sendMessage', [message + '']); 36 | }; 37 | --------------------------------------------------------------------------------