unity-webgl
Advanced tools
Comparing version
@@ -10,3 +10,2 @@ import _typeof from '@babel/runtime/helpers/typeof'; | ||
// const UNITY_GLOBAL_NAME = 'UnityWebGL' | ||
var UNITY_GLOBAL_NAME = '__UnityLib__'; | ||
@@ -45,4 +44,4 @@ | ||
if (window[UNITY_GLOBAL_NAME] !== undefined) { | ||
window[UNITY_GLOBAL_NAME][eventName] = eventListener; | ||
if (window[this.global_name] !== undefined) { | ||
window[this.global_name][eventName] = eventListener; | ||
} | ||
@@ -160,6 +159,11 @@ | ||
if (!src) { | ||
reject && reject(new Error('UnityLoader: src not found.')); | ||
reject && reject(new Error('UnityWebgl: loaderUrl not found.')); | ||
return null; | ||
} | ||
if (typeof window.createUnityInstance === 'function') { | ||
console.warn('UnityWebgl: Unity Loader already exists'); | ||
resolve && resolve(); | ||
} | ||
function handler(code) { | ||
@@ -169,3 +173,3 @@ if (code === 'ready') { | ||
} else { | ||
reject && reject(new Error("'UnityLoader: ".concat(src, "' load failed."))); | ||
reject && reject(new Error("'UnityWebgl: ".concat(src, "' loading failure."))); | ||
} | ||
@@ -250,2 +254,8 @@ } | ||
var DefaultConfig = { | ||
streamingAssetsUrl: 'StreamingAssets', | ||
companyName: 'Unity.com', | ||
productName: 'Unity' | ||
}; | ||
var UnityWebgl = /*#__PURE__*/function (_EventSystem) { | ||
@@ -287,5 +297,5 @@ _inherits(UnityWebgl, _EventSystem); | ||
if (isPlayObject(canvas)) { | ||
_this.config = _objectSpread({}, canvas); | ||
_this.config = Object.assign({}, DefaultConfig, canvas); | ||
} else { | ||
_this.config = _objectSpread({}, options); | ||
_this.config = Object.assign({}, DefaultConfig, options); | ||
@@ -639,3 +649,4 @@ var _canvas = queryCanvas(canvas); | ||
UnityWebgl.install = install; | ||
UnityWebgl.VueUnity = __vue_component__; | ||
export { __vue_component__ as VueUnity, UnityWebgl as default }; |
@@ -1,1 +0,1 @@ | ||
var UnityWebgl=function(e){"use strict";function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},t(e)}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function i(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}function o(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function u(e,t){return u=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},u(e,t)}function c(e,n){if(n&&("object"===t(n)||"function"==typeof n))return n;if(void 0!==n)throw new TypeError("Derived constructors may only return object or undefined");return o(e)}function a(e){return a=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},a(e)}function s(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var l="__UnityLib__",f=function(){function e(){n(this,e),s(this,"eventMap",new Map),void 0!==window&&void 0===window.__UnityLib__&&(window.__UnityLib__={})}return i(e,[{key:"global_name",get:function(){return l}},{key:"on",value:function(e,t){return this.eventMap.set(e,t),void 0!==window.__UnityLib__&&(window.__UnityLib__[e]=t),this}},{key:"once",value:function(e,t){return this.on(e,function n(){t&&t.apply(this,arguments),this.off(e,n)}.bind(this)),this}},{key:"off",value:function(e){return this.eventMap.delete(e),void 0!==window.__UnityLib__&&delete window.__UnityLib__[e],this}},{key:"clear",value:function(){return void 0!==window.__UnityLib__&&this.eventMap.forEach((function(e,t){delete window.__UnityLib__[t]})),this.eventMap.clear(),this}},{key:"emit",value:function(e){var t=this.eventMap.get(e);if(void 0!==t){for(var n=arguments.length,r=new Array(n>1?n-1:0),i=1;i<n;i++)r[i-1]=arguments[i];t.apply(void 0,r)}return this}}]),e}();function d(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}();return function(){var n,r=a(e);if(t){var i=a(this).constructor;n=Reflect.construct(r,arguments,i)}else n=r.apply(this,arguments);return c(this,n)}}function y(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function v(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?y(Object(n),!0).forEach((function(t){s(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):y(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}f.global_name=l;var h=function(e){return"[object Object]"===Object.prototype.toString.call(e)};function p(e){return e instanceof HTMLCanvasElement?e:"string"==typeof e?document.querySelector(e):null}var b=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&u(e,t)}(c,e);var r=d(c);function c(e){var t,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(n(this,c),s(o(t=r.call(this)),"unityLoader",null),s(o(t),"canvasElement",null),s(o(t),"unityInstance",null),h(e))t.config=v({},e);else{t.config=v({},i);var u=p(e);u&&t.create(u)}return t}return i(c,[{key:"create",value:function(e){if(this.unityInstance&&this.canvasElement&&this.unityLoader)return console.warn("UnityWebgl: Unity Instance already exists"),!1;var t=p(e);if(!t)return console.warn("UnityWebgl: CanvasElement not found."),!1;this.canvasElement=t;var n,r,i=this,o=((r=v({},(n=this).config)).print=function(e){n.emit("debug",e)},r.printErr=function(e){n.emit("error",e)},r);this.unityLoader=function(e,t){var n=t.resolve,r=t.reject;if(!e)return r&&r(new Error("UnityLoader: src not found.")),null;function i(t){"ready"===t?n&&n():r&&r(new Error("'UnityLoader: ".concat(e,"' load failed.")))}var o=document.querySelector('script[src="'.concat(e,'"]'));if(null===o){(o=document.createElement("script")).src=e,o.async=!0,o.setAttribute("data-status","loading"),document.body.appendChild(o);var u=function(e){var t,n="load"===e.type?"ready":"error";null===(t=o)||void 0===t||t.setAttribute("data-status",n)};o.addEventListener("load",u),o.addEventListener("error",u)}else i(o.getAttribute("data-status"));var c=function(e){i("load"===e.type?"ready":"error")};return o.addEventListener("load",c),o.addEventListener("error",c),function(){o&&(o.removeEventListener("load",c),o.removeEventListener("error",c),document.body.removeChild(o))}}(o.loaderUrl,{resolve:function(){try{window.createUnityInstance(t,o,(function(e){return i._setProgression(e)})).then((function(e){i.emit("created"),i.unityInstance=e})).catch((function(e){i.emit("error",e),i.unityInstance=null}))}catch(e){i.emit("error",e),i.unityInstance=null}},reject:function(e){console.error("UnityWebgl: ",null==e?void 0:e.message)}})}},{key:"_setProgression",value:function(e){1===e&&this.emit("loaded"),this.emit("progress",e)}},{key:"send",value:function(e,n,r){if(null!==this.unityInstance)if(null==r)this.unityInstance.SendMessage(e,n);else{var i="object"===t(r)?JSON.stringify(r):r;this.unityInstance.SendMessage(e,n,i)}return this}},{key:"requestPointerLock",value:function(){null!==this.canvasElement&&this.canvasElement.requestPointerLock()}},{key:"takeScreenshot",value:function(e,t){var n;return null!==this.canvasElement?(!0!==(null===(n=this.config.webglContextAttributes)||void 0===n?void 0:n.preserveDrawingBuffer)&&console.warn("Taking a screenshot requires 'preserveDrawingBuffer'."),this.canvasElement.toDataURL(e,t)):null}},{key:"setFullscreen",value:function(e){null!==this.unityInstance&&this.unityInstance.SetFullscreen(e?1:0)}},{key:"quitUnityInstance",value:function(){var e=this;null!==this.unityInstance&&this.unityInstance.Quit().then((function(){e.unityInstance=null,e.emit("destroyed")}))}},{key:"destroy",value:function(){this.unityLoader&&(this.unityLoader(),this.unityLoader=null),this.quitUnityInstance()}}]),c}(f),_=0;function m(e){return/^\d+(px|em|%|vw|vh|rem)?$/.test(e)?isNaN(e)?e:e+"px":""}function w(e,t,n,r,i,o,u,c,a,s){"boolean"!=typeof u&&(a=c,c=u,u=!1);const l="function"==typeof n?n.options:n;let f;if(e&&e.render&&(l.render=e.render,l.staticRenderFns=e.staticRenderFns,l._compiled=!0,i&&(l.functional=!0)),r&&(l._scopeId=r),o?(f=function(e){(e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),t&&t.call(this,a(e)),e&&e._registeredComponents&&e._registeredComponents.add(o)},l._ssrRegister=f):t&&(f=u?function(e){t.call(this,s(e,this.$root.$options.shadowRoot))}:function(e){t.call(this,c(e))}),f)if(l.functional){const e=l.render;l.render=function(t,n){return f.call(n),e(t,n)}}else{const e=l.beforeCreate;l.beforeCreate=e?[].concat(e,f):[f]}return n}var g=w({},undefined,{name:"UnityWebgl",props:{unity:Object,width:{type:[String,Number],default:"100%"},height:{type:[String,Number],default:"100%"}},computed:{canvasStyle:function(){var e=this.width,t=this.height;return{width:m(e),height:m(t)}}},mounted:function(){var e=this.$refs.canvas,t=this.unity;e&&(null==t||t.create(e)),this.$once("hook:beforeDestroy",(function(){null==t||t.destroy()}))},render:function(e){return _++,e("canvas",{ref:"canvas",attrs:{id:"unity-canvas-".concat(_)},style:this.canvasStyle})}},undefined,undefined,undefined,!1,void 0,void 0,void 0),O=function e(t){e.installed||(e.installed=!0,t.component("Unity",g))};return"undefined"!=typeof window&&window.Vue&&Vue.use(O),b.install=O,e.VueUnity=g,e.default=b,Object.defineProperty(e,"__esModule",{value:!0}),e}({}); | ||
var UnityWebgl=function(e){"use strict";function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},t(e)}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function i(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}function o(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function u(e,t){return u=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},u(e,t)}function a(e,n){if(n&&("object"===t(n)||"function"==typeof n))return n;if(void 0!==n)throw new TypeError("Derived constructors may only return object or undefined");return o(e)}function c(e){return c=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},c(e)}function s(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var l="__UnityLib__",f=function(){function e(){n(this,e),s(this,"eventMap",new Map),void 0!==window&&void 0===window.__UnityLib__&&(window.__UnityLib__={})}return i(e,[{key:"global_name",get:function(){return l}},{key:"on",value:function(e,t){return this.eventMap.set(e,t),void 0!==window[this.global_name]&&(window[this.global_name][e]=t),this}},{key:"once",value:function(e,t){return this.on(e,function n(){t&&t.apply(this,arguments),this.off(e,n)}.bind(this)),this}},{key:"off",value:function(e){return this.eventMap.delete(e),void 0!==window.__UnityLib__&&delete window.__UnityLib__[e],this}},{key:"clear",value:function(){return void 0!==window.__UnityLib__&&this.eventMap.forEach((function(e,t){delete window.__UnityLib__[t]})),this.eventMap.clear(),this}},{key:"emit",value:function(e){var t=this.eventMap.get(e);if(void 0!==t){for(var n=arguments.length,r=new Array(n>1?n-1:0),i=1;i<n;i++)r[i-1]=arguments[i];t.apply(void 0,r)}return this}}]),e}();function d(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}();return function(){var n,r=c(e);if(t){var i=c(this).constructor;n=Reflect.construct(r,arguments,i)}else n=r.apply(this,arguments);return a(this,n)}}function y(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}f.global_name=l;var v=function(e){return"[object Object]"===Object.prototype.toString.call(e)};function h(e){var t=function(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?y(Object(n),!0).forEach((function(t){s(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):y(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}({},e.config);return t.print=function(t){e.emit("debug",t)},t.printErr=function(t){e.emit("error",t)},t}function p(e){return e instanceof HTMLCanvasElement?e:"string"==typeof e?document.querySelector(e):null}var b={streamingAssetsUrl:"StreamingAssets",companyName:"Unity.com",productName:"Unity"},m=function(e){!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&u(e,t)}(a,e);var r=d(a);function a(e){var t,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(n(this,a),s(o(t=r.call(this)),"unityLoader",null),s(o(t),"canvasElement",null),s(o(t),"unityInstance",null),v(e))t.config=Object.assign({},b,e);else{t.config=Object.assign({},b,i);var u=p(e);u&&t.create(u)}return t}return i(a,[{key:"create",value:function(e){if(this.unityInstance&&this.canvasElement&&this.unityLoader)return console.warn("UnityWebgl: Unity Instance already exists"),!1;var t=p(e);if(!t)return console.warn("UnityWebgl: CanvasElement not found."),!1;this.canvasElement=t;var n=this,r=h(this);this.unityLoader=function(e,t){var n=t.resolve,r=t.reject;if(!e)return r&&r(new Error("UnityWebgl: loaderUrl not found.")),null;function i(t){"ready"===t?n&&n():r&&r(new Error("'UnityWebgl: ".concat(e,"' loading failure.")))}"function"==typeof window.createUnityInstance&&(console.warn("UnityWebgl: Unity Loader already exists"),n&&n());var o=document.querySelector('script[src="'.concat(e,'"]'));if(null===o){(o=document.createElement("script")).src=e,o.async=!0,o.setAttribute("data-status","loading"),document.body.appendChild(o);var u=function(e){var t,n="load"===e.type?"ready":"error";null===(t=o)||void 0===t||t.setAttribute("data-status",n)};o.addEventListener("load",u),o.addEventListener("error",u)}else i(o.getAttribute("data-status"));var a=function(e){i("load"===e.type?"ready":"error")};return o.addEventListener("load",a),o.addEventListener("error",a),function(){o&&(o.removeEventListener("load",a),o.removeEventListener("error",a),document.body.removeChild(o))}}(r.loaderUrl,{resolve:function(){try{window.createUnityInstance(t,r,(function(e){return n._setProgression(e)})).then((function(e){n.emit("created"),n.unityInstance=e})).catch((function(e){n.emit("error",e),n.unityInstance=null}))}catch(e){n.emit("error",e),n.unityInstance=null}},reject:function(e){console.error("UnityWebgl: ",null==e?void 0:e.message)}})}},{key:"_setProgression",value:function(e){1===e&&this.emit("loaded"),this.emit("progress",e)}},{key:"send",value:function(e,n,r){if(null!==this.unityInstance)if(null==r)this.unityInstance.SendMessage(e,n);else{var i="object"===t(r)?JSON.stringify(r):r;this.unityInstance.SendMessage(e,n,i)}return this}},{key:"requestPointerLock",value:function(){null!==this.canvasElement&&this.canvasElement.requestPointerLock()}},{key:"takeScreenshot",value:function(e,t){var n;return null!==this.canvasElement?(!0!==(null===(n=this.config.webglContextAttributes)||void 0===n?void 0:n.preserveDrawingBuffer)&&console.warn("Taking a screenshot requires 'preserveDrawingBuffer'."),this.canvasElement.toDataURL(e,t)):null}},{key:"setFullscreen",value:function(e){null!==this.unityInstance&&this.unityInstance.SetFullscreen(e?1:0)}},{key:"quitUnityInstance",value:function(){var e=this;null!==this.unityInstance&&this.unityInstance.Quit().then((function(){e.unityInstance=null,e.emit("destroyed")}))}},{key:"destroy",value:function(){this.unityLoader&&(this.unityLoader(),this.unityLoader=null),this.quitUnityInstance()}}]),a}(f),g=0;function _(e){return/^\d+(px|em|%|vw|vh|rem)?$/.test(e)?isNaN(e)?e:e+"px":""}function w(e,t,n,r,i,o,u,a,c,s){"boolean"!=typeof u&&(c=a,a=u,u=!1);const l="function"==typeof n?n.options:n;let f;if(e&&e.render&&(l.render=e.render,l.staticRenderFns=e.staticRenderFns,l._compiled=!0,i&&(l.functional=!0)),r&&(l._scopeId=r),o?(f=function(e){(e=e||this.$vnode&&this.$vnode.ssrContext||this.parent&&this.parent.$vnode&&this.parent.$vnode.ssrContext)||"undefined"==typeof __VUE_SSR_CONTEXT__||(e=__VUE_SSR_CONTEXT__),t&&t.call(this,c(e)),e&&e._registeredComponents&&e._registeredComponents.add(o)},l._ssrRegister=f):t&&(f=u?function(e){t.call(this,s(e,this.$root.$options.shadowRoot))}:function(e){t.call(this,a(e))}),f)if(l.functional){const e=l.render;l.render=function(t,n){return f.call(n),e(t,n)}}else{const e=l.beforeCreate;l.beforeCreate=e?[].concat(e,f):[f]}return n}var O=w({},undefined,{name:"UnityWebgl",props:{unity:Object,width:{type:[String,Number],default:"100%"},height:{type:[String,Number],default:"100%"}},computed:{canvasStyle:function(){var e=this.width,t=this.height;return{width:_(e),height:_(t)}}},mounted:function(){var e=this.$refs.canvas,t=this.unity;e&&(null==t||t.create(e)),this.$once("hook:beforeDestroy",(function(){null==t||t.destroy()}))},render:function(e){return g++,e("canvas",{ref:"canvas",attrs:{id:"unity-canvas-".concat(g)},style:this.canvasStyle})}},undefined,undefined,undefined,!1,void 0,void 0,void 0),U=function e(t){e.installed||(e.installed=!0,t.component("Unity",O))};return"undefined"!=typeof window&&window.Vue&&Vue.use(U),m.install=U,m.VueUnity=O,e.VueUnity=O,e.default=m,Object.defineProperty(e,"__esModule",{value:!0}),e}({}); |
@@ -109,3 +109,2 @@ (function (global, factory) { | ||
// const UNITY_GLOBAL_NAME = 'UnityWebGL' | ||
var UNITY_GLOBAL_NAME = '__UnityLib__'; | ||
@@ -144,4 +143,4 @@ | ||
if (window[UNITY_GLOBAL_NAME] !== undefined) { | ||
window[UNITY_GLOBAL_NAME][eventName] = eventListener; | ||
if (window[this.global_name] !== undefined) { | ||
window[this.global_name][eventName] = eventListener; | ||
} | ||
@@ -259,6 +258,11 @@ | ||
if (!src) { | ||
reject && reject(new Error('UnityLoader: src not found.')); | ||
reject && reject(new Error('UnityWebgl: loaderUrl not found.')); | ||
return null; | ||
} | ||
if (typeof window.createUnityInstance === 'function') { | ||
console.warn('UnityWebgl: Unity Loader already exists'); | ||
resolve && resolve(); | ||
} | ||
function handler(code) { | ||
@@ -268,3 +272,3 @@ if (code === 'ready') { | ||
} else { | ||
reject && reject(new Error("'UnityLoader: ".concat(src, "' load failed."))); | ||
reject && reject(new Error("'UnityWebgl: ".concat(src, "' loading failure."))); | ||
} | ||
@@ -349,2 +353,8 @@ } | ||
var DefaultConfig = { | ||
streamingAssetsUrl: 'StreamingAssets', | ||
companyName: 'Unity.com', | ||
productName: 'Unity' | ||
}; | ||
var UnityWebgl = /*#__PURE__*/function (_EventSystem) { | ||
@@ -386,5 +396,5 @@ _inherits(UnityWebgl, _EventSystem); | ||
if (isPlayObject(canvas)) { | ||
_this.config = _objectSpread({}, canvas); | ||
_this.config = Object.assign({}, DefaultConfig, canvas); | ||
} else { | ||
_this.config = _objectSpread({}, options); | ||
_this.config = Object.assign({}, DefaultConfig, options); | ||
@@ -738,2 +748,3 @@ var _canvas = queryCanvas(canvas); | ||
UnityWebgl.install = install; | ||
UnityWebgl.VueUnity = __vue_component__; | ||
@@ -740,0 +751,0 @@ exports.VueUnity = __vue_component__; |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"main": "dist/UnityWebgl.umd.js", | ||
@@ -14,0 +14,0 @@ "module": "dist/UnityWebgl.esm.js", |
110
README.md
# unity-webgl | ||
Unity WebGL provides an easy solution for embedding Unity WebGL builds in your webApp or Vue.js project, with two-way communication between your webApp and Unity application with advanced API's. | ||
Unity WebGL provides an easy solution for embedding Unity WebGL builds in your webApp or `Vue.js` project, with two-way communication between your webApp and Unity application with advanced API's. | ||
UnityWebGL.js 提供了一种简单的解决方案,用于在 webApp 或 Vue.js 项目中嵌入 Unity WebGL,并通过API在 webApp 和 Unity 之间进行双向通信。 | ||
UnityWebgl.js 提供了一种简单的解决方案,用于在 webApp 或 Vue.js 项目中嵌入 Unity WebGL,并通过API在 webApp 和 Unity 之间进行双向通信。 | ||
@@ -10,7 +10,55 @@ based on [react-unity-webgl](https://github.com/jeffreylanters/react-unity-webgl) | ||
## features | ||
- Simple and flexible to use | ||
- two-way communication (webApp, Unity) | ||
- Built-in event handler | ||
- Available for Vue.js | ||
- 💊 Simple and flexible to use | ||
- 📮 two-way communication (webApp, Unity) | ||
- 🛠 Built-in event handler | ||
- 🧬 Available for `Vue.js` | ||
## API | ||
### Unity Config | ||
* `loaderUrl: string` The url to the build json file generated by Unity | ||
* `dataUrl: string` : The url to the build data file generated by Unity | ||
* `frameworkUrl: string` : The url to the framework file generated by Unity | ||
* `codeUrl: string` : The url to the unity code file generated by Unity | ||
* `streamingAssetsUrl?: string` : The url where the streaming assets can be found | ||
* `companyName?: string` : The applications company name | ||
* `productName?: string` : The applications product name | ||
* `productVersion?: string` : The applications product version | ||
* `webglContextAttributes?: IWebGLContextAttributes` : This object allow you to configure WebGLRenderingContext creation options. see [MDN@WebGLRenderingContext](https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/getContextAttributes) | ||
* `devicePixelRatio?: number` : Uncomment this to override low DPI rendering on high DPI displays. see [MDN@devicePixelRatio](https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio) | ||
* `matchWebGLToCanvasSize?: boolean` : Uncomment this to separately control WebGL canvas render size and DOM element size. see [unity3d@matchWebGLToCanvasSize](https://issuetracker.unity3d.com/issues/webgl-builds-dont-allow-separate-control-on-canvas-render-buffer-size) | ||
### Unity Instance | ||
**Methods:** | ||
* `on(eventName: string, eventListener: function)` | ||
* `once(eventName: string, eventListener: function)` | ||
* `off(eventName: string)` | ||
* `clear()` | ||
* `emit(eventName: string)` | ||
--- | ||
* `create(canvasElement: HTMLCanvasElement | string)` | ||
* `send(objectName: string, methodName: string, params: any)` | ||
* `setFullscreen()` | ||
* `destroy()` | ||
**Events:** | ||
- `progress(value: number)` : loading progress. | ||
- `loaded()` : loading completed. | ||
- `created()` : Unity instance is created. | ||
- `destroyed()` : Quits the Unity Instance and clears it from memory. | ||
### vue component | ||
**props** | ||
* `unity: UnityWebgl` | ||
* `width?: string|number ` , default: `100%` | ||
* `height?: string|number ` , default: `100%` | ||
## Install | ||
@@ -42,7 +90,3 @@ ``` | ||
frameworkUrl: "Build/OUT_BIM.framework.js", | ||
codeUrl: "Build/OUT_BIM.wasm", | ||
streamingAssetsUrl: "StreamingAssets", | ||
companyName: "DefaultCompany", | ||
productName: "BIM", | ||
productVersion: "0.1", | ||
codeUrl: "Build/OUT_BIM.wasm" | ||
}) | ||
@@ -87,3 +131,3 @@ | ||
companyName: "DefaultCompany", | ||
productName: "BIM", | ||
productName: "Unity", | ||
productVersion: "0.1", | ||
@@ -101,3 +145,3 @@ }) | ||
<template> | ||
<Unity :unity="unityContext" width="800px" heighht="600px" /> | ||
<Unity :unity="unityContext" width="800px" height="600px" /> | ||
</template> | ||
@@ -112,7 +156,3 @@ | ||
frameworkUrl: "Build/OUT_BIM.framework.js", | ||
codeUrl: "Build/OUT_BIM.wasm", | ||
streamingAssetsUrl: "StreamingAssets", | ||
companyName: "DefaultCompany", | ||
productName: "BIM", | ||
productVersion: "0.1", | ||
codeUrl: "Build/OUT_BIM.wasm" | ||
}) | ||
@@ -134,31 +174,6 @@ | ||
## Methods | ||
events: | ||
* `on(eventName: string, eventListener: function)` | ||
* `once(eventName: string, eventListener: function)` | ||
* `off(eventName: string)` | ||
* `clear()` | ||
* `emit(eventName: string)` | ||
instance: | ||
* `create(canvasElement: HTMLCanvasElement | string)` | ||
* `send(objectName: string, methodName: string, params: any)` | ||
* `setFullscreen()` | ||
* `destroy()` | ||
## Events | ||
- `progress(value: number)` : loading progress. | ||
- `loaded()` : loading completed. | ||
- `created()` : Unity instance is created. | ||
- `destroyed()` : Quits the Unity Instance and clears it from memory. | ||
## Communication | ||
1. Unity application call js functions. | ||
在Unity中调用js方法。 | ||
1. In Unity call js functions. | ||
在Unity中调用js方法。 | ||
@@ -193,4 +208,4 @@ ```js | ||
2. JS call Unity public methods. | ||
在web页面内调用 Unity public方法。 | ||
2. JS call Unity public methods. | ||
在web页面内调用 Unity public方法。 | ||
@@ -216,1 +231,2 @@ ```js | ||
@@ -1,2 +0,1 @@ | ||
// const UNITY_GLOBAL_NAME = 'UnityWebGL' | ||
const UNITY_GLOBAL_NAME = '__UnityLib__' | ||
@@ -28,4 +27,4 @@ | ||
this.eventMap.set(eventName, eventListener) | ||
if (window[UNITY_GLOBAL_NAME] !== undefined) { | ||
window[UNITY_GLOBAL_NAME][eventName] = eventListener | ||
if (window[this.global_name] !== undefined) { | ||
window[this.global_name][eventName] = eventListener | ||
} | ||
@@ -32,0 +31,0 @@ return this |
@@ -16,4 +16,5 @@ import UnityWebgl from './UnityWebgl.js' | ||
UnityWebgl.install = install | ||
UnityWebgl.VueUnity = VueUnity | ||
export { VueUnity } | ||
export default UnityWebgl |
@@ -13,6 +13,11 @@ import EventSystem from './Events.js' | ||
if (!src) { | ||
reject && reject(new Error('UnityLoader: src not found.')) | ||
reject && reject(new Error('UnityWebgl: loaderUrl not found.')) | ||
return null | ||
} | ||
if (typeof window.createUnityInstance === 'function') { | ||
console.warn('UnityWebgl: Unity Loader already exists') | ||
resolve && resolve() | ||
} | ||
function handler(code) { | ||
@@ -22,3 +27,3 @@ if (code === 'ready') { | ||
} else { | ||
reject && reject(new Error(`'UnityLoader: ${src}' load failed.`)) | ||
reject && reject(new Error(`'UnityWebgl: ${src}' loading failure.`)) | ||
} | ||
@@ -100,2 +105,8 @@ } | ||
const DefaultConfig = { | ||
streamingAssetsUrl: 'StreamingAssets', | ||
companyName: 'Unity.com', | ||
productName: 'Unity' | ||
} | ||
export default class UnityWebgl extends EventSystem { | ||
@@ -125,5 +136,5 @@ unityLoader = null | ||
if (isPlayObject(canvas)) { | ||
this.config = { ...canvas } | ||
this.config = Object.assign({}, DefaultConfig, canvas) | ||
} else { | ||
this.config = { ...options } | ||
this.config = Object.assign({}, DefaultConfig, options) | ||
const _canvas = queryCanvas(canvas) | ||
@@ -130,0 +141,0 @@ if (_canvas) { |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
81051
3.13%1762
1.61%225
7.66%1
Infinity%