@pizzapopcorn/unijs 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/unity.js +5 -7
- package/dist/unity.min.js +1 -1
- package/package.json +1 -1
package/dist/unity.js
CHANGED
|
@@ -90,7 +90,6 @@
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
InvokeMethod(methodName, paramType = "", paramValue = "") {
|
|
93
|
-
const { Unity } = require('./Unity');
|
|
94
93
|
paramType = Unity.types[paramType] || paramType;
|
|
95
94
|
this?.#invokeGameObjectEvent("gameObject.invokeMethod", { methodName: methodName, parameterType: paramType, parameterValue: paramValue });
|
|
96
95
|
}
|
|
@@ -137,11 +136,10 @@
|
|
|
137
136
|
if(typeof payload === "object") payloadJson = JSON.stringify(payload);
|
|
138
137
|
else if(typeof payload !== "string") payloadJson = payload.toString();
|
|
139
138
|
const eventPayload = { eventName: eventName, hierarchyPath: this.hierarchyPath, payloadJson: payloadJson, listenDisabled: true };
|
|
140
|
-
|
|
141
|
-
const { Unity } = require('./Unity');
|
|
139
|
+
|
|
142
140
|
const response = Unity.InvokeEvent(`GOEvent:${this.key}`, JSON.stringify(eventPayload));
|
|
143
141
|
|
|
144
|
-
console.log(`Invoked Event: GOEvent:${this.key}`, eventPayload);
|
|
142
|
+
if(Unity.internalLogs) console.log(`Invoked Event: GOEvent:${this.key}`, eventPayload);
|
|
145
143
|
|
|
146
144
|
if(response === null || !response.hasOwnProperty("ok")){
|
|
147
145
|
console.error(`Invalid JSON response from GameObject event callback: ${response}`);
|
|
@@ -160,7 +158,7 @@
|
|
|
160
158
|
}
|
|
161
159
|
}
|
|
162
160
|
|
|
163
|
-
class Unity {
|
|
161
|
+
let Unity$1 = class Unity {
|
|
164
162
|
/** @type {typeof GameObject} */
|
|
165
163
|
static GameObject = GameObject;
|
|
166
164
|
static internalLogs = false;
|
|
@@ -340,8 +338,8 @@
|
|
|
340
338
|
else console.log(`[Unity] ${message}`);
|
|
341
339
|
}
|
|
342
340
|
|
|
343
|
-
}
|
|
341
|
+
};
|
|
344
342
|
|
|
345
|
-
return Unity;
|
|
343
|
+
return Unity$1;
|
|
346
344
|
|
|
347
345
|
}));
|
package/dist/unity.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Unity=t()}(this,function(){"use strict";class e{static keyGameObjects={};static#e={awake:{},start:{},enable:{},disable:{},destroy:{}};static GetKeyGameObject(t){return e.keyGameObjects.hasOwnProperty(t)?e.keyGameObjects[t]:(console.error(`GameObject with key '${t}' not found`),null)}static onAwake(t,n){e.#e.awake.hasOwnProperty(t)||(e.#e.awake[t]=new Set),e.#e.awake[t].add(n)}static onStart(t,n){e.#e.start.hasOwnProperty(t)||(e.#e.start[t]=new Set),e.#e.start[t].add(n)}static onEnable(t,n){e.#e.enable.hasOwnProperty(t)||(e.#e.enable[t]=new Set),e.#e.enable[t].add(n)}static onDisable(t,n){e.#e.disable.hasOwnProperty(t)||(e.#e.disable[t]=new Set),e.#e.disable[t].add(n)}static onDestroy(t,n){e.#e.destroy.hasOwnProperty(t)||(e.#e.destroy[t]=new Set),e.#e.destroy[t].add(n)}static _register(t,n){e.keyGameObjects[t]=new e(t,n)}static _receiveLifeCycleEvent(t,n){const s=e.keyGameObjects[t];if(!s)return;const a=e.#e[n][t];if(a)for(const e of a)e(s);"destroy"===n&&(s.transform=null,delete e.keyGameObjects[t])}constructor(e,t){this.key=e,this.name=t.name,this.transform=t.transform,this.hierarchyPath=t.hasOwnProperty("hierarchyPath")?t.hierarchyPath:""}SetActive(e){this?.#t("gameObject.setActive",e)}InvokeMethod(e,t="",n=""){
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Unity=t()}(this,function(){"use strict";class e{static keyGameObjects={};static#e={awake:{},start:{},enable:{},disable:{},destroy:{}};static GetKeyGameObject(t){return e.keyGameObjects.hasOwnProperty(t)?e.keyGameObjects[t]:(console.error(`GameObject with key '${t}' not found`),null)}static onAwake(t,n){e.#e.awake.hasOwnProperty(t)||(e.#e.awake[t]=new Set),e.#e.awake[t].add(n)}static onStart(t,n){e.#e.start.hasOwnProperty(t)||(e.#e.start[t]=new Set),e.#e.start[t].add(n)}static onEnable(t,n){e.#e.enable.hasOwnProperty(t)||(e.#e.enable[t]=new Set),e.#e.enable[t].add(n)}static onDisable(t,n){e.#e.disable.hasOwnProperty(t)||(e.#e.disable[t]=new Set),e.#e.disable[t].add(n)}static onDestroy(t,n){e.#e.destroy.hasOwnProperty(t)||(e.#e.destroy[t]=new Set),e.#e.destroy[t].add(n)}static _register(t,n){e.keyGameObjects[t]=new e(t,n)}static _receiveLifeCycleEvent(t,n){const s=e.keyGameObjects[t];if(!s)return;const a=e.#e[n][t];if(a)for(const e of a)e(s);"destroy"===n&&(s.transform=null,delete e.keyGameObjects[t])}constructor(e,t){this.key=e,this.name=t.name,this.transform=t.transform,this.hierarchyPath=t.hasOwnProperty("hierarchyPath")?t.hierarchyPath:""}SetActive(e){this?.#t("gameObject.setActive",e)}InvokeMethod(e,t="",n=""){t=Unity.types[t]||t,this?.#t("gameObject.invokeMethod",{methodName:e,parameterType:t,parameterValue:n})}GetChild(t){const n="string"==typeof t?"gameObject.findChild":"gameObject.getChild",s=this?.#t(n,t);if(null!==s){const t=""===this.hierarchyPath?this.key:this.hierarchyPath;return s.hierarchyPath=t+"/"+s.name,new e(this.key,s)}return null}Translate(e,t,n){this?.#t("transform.translate",{x:e,y:t,z:n})}Rotate(e,t,n){this?.#t("transform.rotate",{x:e,y:t,z:n})}SetLocalScale(e,t,n){this?.#t("transform.setLocalScale",{x:e,y:t,z:n})}SetLocalPosition(e,t,n){this?.#t("transform.setLocalPosition",{x:e,y:t,z:n})}SetText(e){this?.#t("text.setText",e)}Destroy(){this?.#t("gameObject.destroy","")}#t(e,t){if(!this.transform)return null;let n=t;"object"==typeof t?n=JSON.stringify(t):"string"!=typeof t&&(n=t.toString());const s={eventName:e,hierarchyPath:this.hierarchyPath,payloadJson:n,listenDisabled:!0},a=Unity.InvokeEvent(`GOEvent:${this.key}`,JSON.stringify(s));if(Unity.internalLogs&&console.log(`Invoked Event: GOEvent:${this.key}`,s),null===a||!a.hasOwnProperty("ok"))return console.error(`Invalid JSON response from GameObject event callback: ${a}`),null;if(a.ok){let e=a.responseJson;try{e=JSON.parse(a.responseJson)}catch{}return e}return console.error(`Error invoking GameObject event: ${e}`,a.error),null}}return class t{static GameObject=e;static internalLogs=!1;static types={int:"System.Int32",float:"System.Single",double:"System.Double",bool:"System.Boolean",string:"System.String",char:"System.Char",byte:"System.Byte",long:"System.Int64",short:"System.Int16",decimal:"System.Decimal",object:"System.Object",customClass:(e,t="",n="Assembly-CSharp")=>`${""===t?e:`${t}.${e}`}, ${n}.dll`};static#n;static#s=!1;static#a=new Set;static#r={};static LoadInstance(e,n){t.#s=!1;const s=new XMLHttpRequest;s.open("GET",e+"/index.html",!0),s.onreadystatechange=function(){if(4!==s.readyState||200!==s.status)return;document.querySelector(`#${n}`).innerHTML=s.responseText;const t=document.createElement("link");t.rel="stylesheet",t.type="text/css",t.href=e+"/TemplateData/style.css",document.head.appendChild(t);const a=document.createElement("script");a.src=e+"/index.js",document.body.appendChild(a)},s.send()}static GetBuildVersion(){return t.InvokeEvent("InstanceEvent:GetBuildVersion")}static InvokeEvent(e,n=void 0){const s=t.#o(e,n);try{const n=JSON.parse(s);return n.hasOwnProperty("promiseId")&&(console.warn(`Event '${e}' returned a promise. Consider using InvokeEventAsync instead.`),t.onEvent(`PromiseResolvedEvent:${n.promiseId}`,e=>{delete t.#r[`PromiseResolvedEvent:${n.promiseId}`]})),n}catch{return s}}static async InvokeEventAsync(e,n=void 0){return new Promise(s=>{const a=t.#o(e,n);try{const e=JSON.parse(a);e.hasOwnProperty("promiseId")?t.onEvent(`PromiseResolvedEvent:${e.promiseId}`,n=>{s(n),delete t.#r[`PromiseResolvedEvent:${e.promiseId}`]}):s(e)}catch{s(a)}})}static async WaitForEndOfFrameAsync(){return new Promise(e=>{const n=crypto.randomUUID().toString(),s=`EndOfFrameEvent:${n}`;t.onEvent(s,()=>{e(),delete t.#r[s]}),t.InvokeEvent("InstanceEvent:WaitForEndOfFrame",n)})}static async LoadBundleAsync(e){await t.InvokeEventAsync("InstanceEvent:LoadBundle",e)}static async InstantiatePrefabFromBundleAsync(e,n,s=""){await t.InvokeEventAsync("InstanceEvent:InstantiatePrefabFromBundle",{bundleUrl:e,prefabName:n,parentKey:s})}static onInstanceReady(e){t.#s?e():t.#a.add(e)}static onEvent(e,n){t.#r.hasOwnProperty(e)||(t.#r[e]=new Set),t.#r[e].add(n)}static offEvent(e,n){t.#r.hasOwnProperty(e)&&t.#r[e].delete(n)}static#o(e,n){null==n&&(n="");let s=n;"object"==typeof n?s=JSON.stringify(n):"string"!=typeof n&&(s=n.toString());const a=t.#n(e,s);return t.#r[e]?.forEach(e=>e(s)),a}static _instanceReady(){t.#s=!0,t.#a.forEach(e=>e())}static _registerClientListener(e){t.#n=e}static _receiveEvent(e,n){let s=n;try{s=JSON.parse(n)}catch{}t.InvokeEvent(e,s)}static _logFromUnity(e,n){("internal"!==e||t.internalLogs)&&("error"===e?console.error(`[Unity] ${n}`):"warning"===e?console.warn(`[Unity] ${n}`):console.log(`[Unity] ${n}`))}}});
|