function _WebGLComm() { this.gameInstance; this.callbacks = {}; /* * Invokes the function registered as 'functionId', used by the * WebGLComm plugin as enter point to Unity WebGL. * * Captures any exception and logs it to the console. * * If a 'callback' parameters is defined in teh parameters, its * value is used as the function id of a function in Unity WebGL * that will be invoked passing the returned values as parameters. * This callback receives the returned parameters in the key * 'returned' if an object, that also includes the key 'isCallback' * to indicate to WebGLComm that this is a callback and has to be * managed so. * * @param functionId Key of the function. * @objParams One object with the parameters passed to the * function. */ this.call = function(functionId, objParams){ try { var returned = this.callbacks[functionId](objParams); if ('undefined' !== objParams['callback']) { this.callToUnity(objParams['callback'], { returned : returned, isCallback: true }); } } catch (e) { console.log(e + ', ' + functionId + ',' + objParams); } } /* * Associates a key to a function. * * @param functionId Key of of the function. * @param callback The function to be invoked (receives one * object as paramters) */ this.addListener = function(functionId, callback) { this.callbacks[functionId] = callback; } /* * Entry point to invoke a function in Unity WebGL. * * tgis.gameInstance must have been initialized with an instance * returned by UnityLoader.instantiate. * * The game object in the scene that will receive the message * must be named 'WebGLComm' and must contain the component * WebGLComm. * * @functionId Key of the function to be invoked in Unity * WebGL (must be previously registered as listener in Unity * WebGL) * @params One object that contains the parameters for the * function invoked in Unity WebGL. */ this.callToUnity = function(functionId, params) { if ('undefined' === this.gameInstance) { console.log('Undefined this.gameInstance; must be an instance returned by UnityLoader.instantiate'); return; } if ('object' !== typeof params) { console.log('Object expected, received ' + (typeof params)); return; } params['functionId'] = functionId; this.gameInstance.SendMessage('WebGLComm', 'MessageFromWeb', JSON.stringify(params)); } } var WebGLComm = new _WebGLComm();