@pizzapopcorn/unijs 0.0.4 → 0.0.6
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 +581 -24
- package/dist/unity.min.js +1 -1
- package/package.json +1 -1
package/dist/unity.js
CHANGED
|
@@ -4,17 +4,316 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Unity = factory());
|
|
5
5
|
})(this, (function () { 'use strict';
|
|
6
6
|
|
|
7
|
+
class Transform {
|
|
8
|
+
|
|
9
|
+
constructor(gameObject) {
|
|
10
|
+
this.gameObject = gameObject;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Gets the GameObject's world position.
|
|
15
|
+
* @returns {object}
|
|
16
|
+
*/
|
|
17
|
+
get position() {
|
|
18
|
+
return this.#tryInvokeGameObjectEvent("transform.getPosition", "");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Sets the GameObject's world position.
|
|
23
|
+
* @param {object} value
|
|
24
|
+
*/
|
|
25
|
+
set position(value) {
|
|
26
|
+
this.#assertVector3(value);
|
|
27
|
+
this.#tryInvokeGameObjectEvent("transform.setPosition", value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Gets the GameObject's local position.
|
|
32
|
+
* @returns {object}
|
|
33
|
+
*/
|
|
34
|
+
get localPosition() {
|
|
35
|
+
return this.#tryInvokeGameObjectEvent("transform.getLocalPosition", "");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Sets the GameObject's local position.
|
|
40
|
+
* @param {object} value
|
|
41
|
+
*/
|
|
42
|
+
set localPosition(value) {
|
|
43
|
+
this.#assertVector3(value);
|
|
44
|
+
this.#tryInvokeGameObjectEvent("transform.setLocalPosition", value);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Gets the GameObject's local rotation.
|
|
49
|
+
* @returns {object}
|
|
50
|
+
*/
|
|
51
|
+
get rotation() {
|
|
52
|
+
return this.#tryInvokeGameObjectEvent("transform.getRotation", "");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Sets the GameObject's local rotation.
|
|
57
|
+
* @param {object} value
|
|
58
|
+
*/
|
|
59
|
+
set rotation(value) {
|
|
60
|
+
this.#assertQuaternion(value);
|
|
61
|
+
this.#tryInvokeGameObjectEvent("transform.setRotation", value);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Gets the GameObject's euler angles in degrees.
|
|
66
|
+
* @returns {object}
|
|
67
|
+
*/
|
|
68
|
+
get eulerAngles() {
|
|
69
|
+
return this.#tryInvokeGameObjectEvent("transform.getEulerAngles", "");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Sets the GameObject's euler angles.
|
|
74
|
+
* @param {object} value
|
|
75
|
+
*/
|
|
76
|
+
set eulerAngles(value) {
|
|
77
|
+
console.warn("[Unity] eulerAngles is read-only.");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Gets the GameObject's local scale.
|
|
82
|
+
* @returns {object}
|
|
83
|
+
*/
|
|
84
|
+
get localScale() {
|
|
85
|
+
return this.#tryInvokeGameObjectEvent("transform.getLocalScale", "");
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Sets the GameObject's local scale.
|
|
90
|
+
* @param {object} value
|
|
91
|
+
*/
|
|
92
|
+
set localScale(value) {
|
|
93
|
+
this.#assertVector3(value);
|
|
94
|
+
this.#tryInvokeGameObjectEvent("transform.setLocalScale", value);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Gets the GameObject's lossy scale (Read Only).
|
|
99
|
+
* @returns {object}
|
|
100
|
+
*/
|
|
101
|
+
get lossyScale() {
|
|
102
|
+
return this.#tryInvokeGameObjectEvent("transform.getLossyScale", "");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Sets the GameObject's lossy scale.
|
|
107
|
+
* @param {object} value
|
|
108
|
+
*/
|
|
109
|
+
set lossyScale(value) {
|
|
110
|
+
console.warn("[Unity] lossyScale is read-only.");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Translates the GameObject's position by the specified amount on each axis.
|
|
115
|
+
* @param {number} x
|
|
116
|
+
* @param {number} y
|
|
117
|
+
* @param {number} z
|
|
118
|
+
*/
|
|
119
|
+
Translate(x, y, z) {
|
|
120
|
+
this.#tryInvokeGameObjectEvent("transform.translate", { x: x, y: y, z: z });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Rotates the GameObject around its local axis by the specified amount in degrees.
|
|
125
|
+
* @param {number} x
|
|
126
|
+
* @param {number} y
|
|
127
|
+
* @param {number} z
|
|
128
|
+
*/
|
|
129
|
+
Rotate(x, y, z) {
|
|
130
|
+
this.#tryInvokeGameObjectEvent("transform.rotate", { x: x, y: y, z: z });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
#tryInvokeGameObjectEvent(eventName, payload) {
|
|
135
|
+
if(!this.gameObject) {
|
|
136
|
+
console.error("[Unity] The GameObject associated with this Transform is null");
|
|
137
|
+
}
|
|
138
|
+
return this.gameObject._invokeGameObjectEvent(eventName, payload);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
#assertVector3(value) {
|
|
142
|
+
if (typeof value !== "object" || value === null || typeof value.x !== "number" || typeof value.y !== "number" || typeof value.z !== "number") {
|
|
143
|
+
throw new Error(`[Unity] Invalid Vector3: ${JSON.stringify(value)}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
#assertQuaternion(value) {
|
|
148
|
+
if (typeof value !== "object" || value === null || typeof value.x !== "number" || typeof value.y !== "number" || typeof value.z !== "number" || typeof value.w !== "number") {
|
|
149
|
+
throw new Error(`[Unity] Invalid Quaternion: ${JSON.stringify(value)}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
class Rigidbody {
|
|
155
|
+
|
|
156
|
+
constructor(gameObject) {
|
|
157
|
+
this.gameObject = gameObject;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Gets the mass of the RigidBody.
|
|
162
|
+
* @returns {number}
|
|
163
|
+
*/
|
|
164
|
+
get mass() {
|
|
165
|
+
return this.#tryInvokeGameObjectEvent("physics.getMass", "");
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Gets whether the RigidBody is affected by gravity.
|
|
170
|
+
* @returns {boolean}
|
|
171
|
+
*/
|
|
172
|
+
get useGravity() {
|
|
173
|
+
return this.#tryInvokeGameObjectEvent("physics.getUseGravity", "");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Gets whether the RigidBody is kinematic.
|
|
178
|
+
* @returns {boolean}
|
|
179
|
+
*/
|
|
180
|
+
get isKinematic() {
|
|
181
|
+
return this.#tryInvokeGameObjectEvent("physics.getIsKinematic", "");
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Gets the linear damping of the RigidBody.
|
|
186
|
+
* @returns {number}
|
|
187
|
+
*/
|
|
188
|
+
get linearDamping() {
|
|
189
|
+
return this.#tryInvokeGameObjectEvent("physics.getLinearDamping", "");
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Gets the angular damping of the RigidBody.
|
|
194
|
+
* @returns {number}
|
|
195
|
+
*/
|
|
196
|
+
get angularDamping() {
|
|
197
|
+
return this.#tryInvokeGameObjectEvent("physics.getAngularDamping", "");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Adds a force to the RigidBody.
|
|
202
|
+
* @param {number} x
|
|
203
|
+
* @param {number} y
|
|
204
|
+
* @param {number} z
|
|
205
|
+
*/
|
|
206
|
+
AddForce(x, y, z) {
|
|
207
|
+
this.#tryInvokeGameObjectEvent("physics.addForce", { x: x, y: y, z: z });
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Adds a torque to the RigidBody.
|
|
212
|
+
* @param {number} x
|
|
213
|
+
* @param {number} y
|
|
214
|
+
* @param {number} z
|
|
215
|
+
*/
|
|
216
|
+
AddTorque(x, y, z) {
|
|
217
|
+
this.#tryInvokeGameObjectEvent("physics.addTorque", { x: x, y: y, z: z });
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Sets the linear velocity of the RigidBody.
|
|
222
|
+
* @param {number} x
|
|
223
|
+
* @param {number} y
|
|
224
|
+
* @param {number} z
|
|
225
|
+
*/
|
|
226
|
+
SetVelocity(x, y, z) {
|
|
227
|
+
this.#tryInvokeGameObjectEvent("physics.setVelocity", { x: x, y: y, z: z });
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Gets the linear velocity of the RigidBody.
|
|
232
|
+
* @returns {object}
|
|
233
|
+
*/
|
|
234
|
+
GetVelocity() {
|
|
235
|
+
return this.#tryInvokeGameObjectEvent("physics.getVelocity", "");
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Sets the angular velocity of the RigidBody.
|
|
240
|
+
* @param {number} x
|
|
241
|
+
* @param {number} y
|
|
242
|
+
* @param {number} z
|
|
243
|
+
*/
|
|
244
|
+
SetAngularVelocity(x, y, z) {
|
|
245
|
+
this.#tryInvokeGameObjectEvent("physics.setAngularVelocity", { x: x, y: y, z: z });
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Gets the angular velocity of the RigidBody.
|
|
250
|
+
* @returns {object}
|
|
251
|
+
*/
|
|
252
|
+
GetAngularVelocity() {
|
|
253
|
+
return this.#tryInvokeGameObjectEvent("physics.getAngularVelocity", "");
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Sets whether the RigidBody is affected by gravity.
|
|
258
|
+
* @param {boolean} useGravity
|
|
259
|
+
*/
|
|
260
|
+
SetUseGravity(useGravity) {
|
|
261
|
+
this.#tryInvokeGameObjectEvent("physics.setUseGravity", useGravity);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Sets whether the RigidBody is kinematic.
|
|
266
|
+
* @param {boolean} isKinematic
|
|
267
|
+
*/
|
|
268
|
+
SetIsKinematic(isKinematic) {
|
|
269
|
+
this.#tryInvokeGameObjectEvent("physics.setIsKinematic", isKinematic);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Sets the mass of the RigidBody.
|
|
274
|
+
* @param {number} mass
|
|
275
|
+
*/
|
|
276
|
+
SetMass(mass) {
|
|
277
|
+
this.#tryInvokeGameObjectEvent("physics.setMass", mass);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Sets the linear damping of the RigidBody.
|
|
282
|
+
* @param {number} linearDamping
|
|
283
|
+
*/
|
|
284
|
+
SetLinearDamping(linearDamping) {
|
|
285
|
+
this.#tryInvokeGameObjectEvent("physics.setLinearDamping", linearDamping);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
#tryInvokeGameObjectEvent(eventName, payload) {
|
|
289
|
+
if(!this.gameObject) {
|
|
290
|
+
console.error("[Unity] The GameObject associated with this RigidBody is null");
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
return this.gameObject._invokeGameObjectEvent(eventName, payload);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
7
297
|
class GameObject {
|
|
298
|
+
#transform = null;
|
|
299
|
+
#rigidbody = null;
|
|
8
300
|
|
|
9
301
|
static keyGameObjects = {};
|
|
10
302
|
static #lifeCycleCallbacks = {
|
|
11
303
|
awake: {},
|
|
12
304
|
start: {},
|
|
305
|
+
update: {},
|
|
13
306
|
enable: {},
|
|
14
307
|
disable: {},
|
|
15
308
|
destroy: {}
|
|
16
309
|
}
|
|
17
310
|
|
|
311
|
+
/**
|
|
312
|
+
* Gets a GameObject by its JS Key (previously set in Unity using the JSKeyGameObject component).
|
|
313
|
+
* @param {string} key
|
|
314
|
+
* @returns {GameObject | null}
|
|
315
|
+
* @constructor
|
|
316
|
+
*/
|
|
18
317
|
static GetKeyGameObject(key){
|
|
19
318
|
if(!GameObject.keyGameObjects.hasOwnProperty(key)) {
|
|
20
319
|
console.error(`GameObject with key '${key}' not found`);
|
|
@@ -22,7 +321,30 @@
|
|
|
22
321
|
}
|
|
23
322
|
return GameObject.keyGameObjects[key];
|
|
24
323
|
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Instantiates a prefab at the specified position and rotation either in world space or relative to a parent GameObject.
|
|
327
|
+
* @param {string} prefabPath - The path to the prefab to instantiate from a Resources folder.
|
|
328
|
+
* @param {Object} position - The position to instantiate the prefab at (Defaults to Vector3.Zero).
|
|
329
|
+
* @param {Object} rotation - The rotation to instantiate the prefab with (Defaults to Quaternion.Identity).
|
|
330
|
+
* @param {GameObject | Transform | null} parent - The parent GameObject to instantiate the prefab relative to (Defaults to null).
|
|
331
|
+
*/
|
|
332
|
+
static Instantiate(prefabPath, position = { x: 0, y: 0, z: 0 }, rotation = { x: 0, y: 0, z: 0, w: 1 }, parent = null) {
|
|
333
|
+
if(parent === null){
|
|
334
|
+
Unity.InvokeEvent("InstanceEvent:InstantiateGameObject", { prefabPath: prefabPath, position: position, rotation: rotation });
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
const p = parent instanceof GameObject ? parent : parent instanceof Transform ? parent.gameObject : null;
|
|
338
|
+
if(p === null) return;
|
|
339
|
+
p.Instantiate(prefabPath, position, rotation);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
25
342
|
|
|
343
|
+
/**
|
|
344
|
+
* Registers a callback for a GameObject's Awake event using its JS Key. If the GameObject doesn't exist yet, it will trigger when it's created.
|
|
345
|
+
* @param {string} key
|
|
346
|
+
* @param {function} callback
|
|
347
|
+
*/
|
|
26
348
|
static onAwake(key, callback){
|
|
27
349
|
if(!GameObject.#lifeCycleCallbacks.awake.hasOwnProperty(key)) {
|
|
28
350
|
GameObject.#lifeCycleCallbacks.awake[key] = new Set();
|
|
@@ -30,13 +352,35 @@
|
|
|
30
352
|
GameObject.#lifeCycleCallbacks.awake[key].add(callback);
|
|
31
353
|
}
|
|
32
354
|
|
|
355
|
+
/**
|
|
356
|
+
* Registers a callback for a GameObject's Start event using its JS Key. If the GameObject doesn't exist yet, it will trigger when it's created.
|
|
357
|
+
* @param {string} key
|
|
358
|
+
* @param {function} callback
|
|
359
|
+
*/
|
|
33
360
|
static onStart(key, callback) {
|
|
34
361
|
if(!GameObject.#lifeCycleCallbacks.start.hasOwnProperty(key)) {
|
|
35
362
|
GameObject.#lifeCycleCallbacks.start[key] = new Set();
|
|
36
363
|
}
|
|
37
364
|
GameObject.#lifeCycleCallbacks.start[key].add(callback);
|
|
38
365
|
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Registers a callback for a GameObject's Update event using its JS Key. If the GameObject doesn't exist yet, it will trigger when it's created.
|
|
369
|
+
* @param {string} key
|
|
370
|
+
* @param {function} callback
|
|
371
|
+
*/
|
|
372
|
+
static onUpdate(key, callback) {
|
|
373
|
+
if(!GameObject.#lifeCycleCallbacks.update.hasOwnProperty(key)) {
|
|
374
|
+
GameObject.#lifeCycleCallbacks.update[key] = new Set();
|
|
375
|
+
}
|
|
376
|
+
GameObject.#lifeCycleCallbacks.update[key].add(callback);
|
|
377
|
+
}
|
|
39
378
|
|
|
379
|
+
/**
|
|
380
|
+
* Registers a callback for a GameObject's OnEnable event using its JS Key. If the GameObject doesn't exist yet, it will trigger when it's created.
|
|
381
|
+
* @param {string} key
|
|
382
|
+
* @param {function} callback
|
|
383
|
+
*/
|
|
40
384
|
static onEnable(key, callback) {
|
|
41
385
|
if(!GameObject.#lifeCycleCallbacks.enable.hasOwnProperty(key)) {
|
|
42
386
|
GameObject.#lifeCycleCallbacks.enable[key] = new Set();
|
|
@@ -44,6 +388,11 @@
|
|
|
44
388
|
GameObject.#lifeCycleCallbacks.enable[key].add(callback);
|
|
45
389
|
}
|
|
46
390
|
|
|
391
|
+
/**
|
|
392
|
+
* Registers a callback for a GameObject's OnDisable event using its JS Key. If the GameObject doesn't exist yet, it will trigger when it's created and then disabled.
|
|
393
|
+
* @param {string} key
|
|
394
|
+
* @param {function} callback
|
|
395
|
+
*/
|
|
47
396
|
static onDisable(key, callback) {
|
|
48
397
|
if(!GameObject.#lifeCycleCallbacks.disable.hasOwnProperty(key)) {
|
|
49
398
|
GameObject.#lifeCycleCallbacks.disable[key] = new Set();
|
|
@@ -51,6 +400,11 @@
|
|
|
51
400
|
GameObject.#lifeCycleCallbacks.disable[key].add(callback);
|
|
52
401
|
}
|
|
53
402
|
|
|
403
|
+
/**
|
|
404
|
+
* Registers a callback for a GameObject's Destroy event using its JS Key. If the GameObject doesn't exist yet, it will trigger when it's created and then destroyed.
|
|
405
|
+
* @param {string} key
|
|
406
|
+
* @param {function} callback
|
|
407
|
+
*/
|
|
54
408
|
static onDestroy(key, callback) {
|
|
55
409
|
if(!GameObject.#lifeCycleCallbacks.destroy.hasOwnProperty(key)) {
|
|
56
410
|
GameObject.#lifeCycleCallbacks.destroy[key] = new Set();
|
|
@@ -58,10 +412,12 @@
|
|
|
58
412
|
GameObject.#lifeCycleCallbacks.destroy[key].add(callback);
|
|
59
413
|
}
|
|
60
414
|
|
|
415
|
+
/**Only for unity js library use*/
|
|
61
416
|
static _register(key, data) {
|
|
62
417
|
GameObject.keyGameObjects[key] = new GameObject(key, data);
|
|
63
418
|
}
|
|
64
419
|
|
|
420
|
+
/**Only for unity js library use*/
|
|
65
421
|
static _receiveLifeCycleEvent(key, event) {
|
|
66
422
|
const gameObject = GameObject.keyGameObjects[key];
|
|
67
423
|
if(!gameObject) return;
|
|
@@ -73,7 +429,6 @@
|
|
|
73
429
|
}
|
|
74
430
|
}
|
|
75
431
|
if(event === "destroy") {
|
|
76
|
-
gameObject.transform = null;
|
|
77
432
|
delete GameObject.keyGameObjects[key];
|
|
78
433
|
}
|
|
79
434
|
}
|
|
@@ -81,22 +436,61 @@
|
|
|
81
436
|
constructor(key, data) {
|
|
82
437
|
this.key = key;
|
|
83
438
|
this.name = data.name;
|
|
84
|
-
this
|
|
439
|
+
this.#transform = new Transform(this);
|
|
440
|
+
if(data.hasRigidbody) {
|
|
441
|
+
this.#rigidbody = new Rigidbody(this);
|
|
442
|
+
}
|
|
85
443
|
this.hierarchyPath = data.hasOwnProperty("hierarchyPath") ? data.hierarchyPath : "";
|
|
86
444
|
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Gets the Transform component of the GameObject.
|
|
448
|
+
* @returns {Transform}
|
|
449
|
+
*/
|
|
450
|
+
get transform() {
|
|
451
|
+
return this.#transform;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Gets the RigidBody component of the GameObject.
|
|
456
|
+
* @returns {Rigidbody | null}
|
|
457
|
+
*/
|
|
458
|
+
get rigidbody() {
|
|
459
|
+
if(this.HasComponent("Rigidbody")) {
|
|
460
|
+
this.#rigidbody = new Rigidbody(this);
|
|
461
|
+
return this.#rigidbody;
|
|
462
|
+
}
|
|
463
|
+
return null;
|
|
464
|
+
}
|
|
87
465
|
|
|
466
|
+
/**
|
|
467
|
+
* Sets the GameObject's active state.
|
|
468
|
+
* @param {boolean} active
|
|
469
|
+
*/
|
|
88
470
|
SetActive(active) {
|
|
89
|
-
this
|
|
471
|
+
this?._invokeGameObjectEvent("gameObject.setActive", active);
|
|
90
472
|
}
|
|
91
473
|
|
|
474
|
+
/**
|
|
475
|
+
* Invokes a method on the GameObject via SendMessage. Use GameObject.types for type options.
|
|
476
|
+
* <br>In order for it to work with custom types, you need to get the fully qualified name of the type.
|
|
477
|
+
* @param {string} methodName
|
|
478
|
+
* @param {string} paramType
|
|
479
|
+
* @param {string} paramValue
|
|
480
|
+
*/
|
|
92
481
|
InvokeMethod(methodName, paramType = "", paramValue = "") {
|
|
93
482
|
paramType = Unity.types[paramType] || paramType;
|
|
94
|
-
this
|
|
483
|
+
this?._invokeGameObjectEvent("gameObject.invokeMethod", { methodName: methodName, parameterType: paramType, parameterValue: paramValue });
|
|
95
484
|
}
|
|
96
485
|
|
|
486
|
+
/**
|
|
487
|
+
* Gets a child GameObject by index or name.
|
|
488
|
+
* @param {number | string} query
|
|
489
|
+
* @returns {GameObject | null}
|
|
490
|
+
*/
|
|
97
491
|
GetChild(query) {
|
|
98
492
|
const eventName = typeof query === "string" ? "gameObject.findChild" : "gameObject.getChild";
|
|
99
|
-
const childData = this
|
|
493
|
+
const childData = this?._invokeGameObjectEvent(eventName, query);
|
|
100
494
|
if(childData !== null) {
|
|
101
495
|
const currentPath = this.hierarchyPath === "" ? this.key : this.hierarchyPath;
|
|
102
496
|
childData.hierarchyPath = currentPath + "/" + childData.name;
|
|
@@ -105,33 +499,45 @@
|
|
|
105
499
|
return null;
|
|
106
500
|
}
|
|
107
501
|
|
|
108
|
-
|
|
109
|
-
|
|
502
|
+
/**
|
|
503
|
+
* Looks for a text component and sets its text to the specified value. It works with Legacy, TextMeshPro, and TextMesh components.
|
|
504
|
+
* @param {string} text
|
|
505
|
+
*/
|
|
506
|
+
SetText(text) {
|
|
507
|
+
this?._invokeGameObjectEvent("text.setText", text);
|
|
110
508
|
}
|
|
111
509
|
|
|
112
|
-
|
|
113
|
-
|
|
510
|
+
/**
|
|
511
|
+
* Destroys the GameObject.
|
|
512
|
+
*/
|
|
513
|
+
Destroy() {
|
|
514
|
+
this?._invokeGameObjectEvent("gameObject.destroy", "");
|
|
114
515
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Instantiates a prefab at the specified position and rotation relative to this GameObject's transform.
|
|
519
|
+
* @param {string} prefabPath - The path to the prefab to instantiate from a Resources folder.
|
|
520
|
+
* @param {Object} position - The position to instantiate the prefab at (Defaults to Vector3.Zero).
|
|
521
|
+
* @param {Object} rotation - The rotation to instantiate the prefab with (Defaults to Quaternion.Identity).
|
|
522
|
+
*/
|
|
523
|
+
Instantiate(prefabPath, position = { x: 0, y: 0, z: 0 }, rotation = { x: 0, y: 0, z: 0, w: 1 }) {
|
|
524
|
+
this?._invokeGameObjectEvent("gameObject.instantiate", { prefabPath: prefabPath, position: position, rotation: rotation });
|
|
118
525
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
this
|
|
526
|
+
|
|
527
|
+
HasComponent(componentName) {
|
|
528
|
+
return this?._invokeGameObjectEvent("gameObject.hasComponent", componentName);
|
|
122
529
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
this
|
|
530
|
+
|
|
531
|
+
GetComponent(componentName) {
|
|
532
|
+
return this?._invokeGameObjectEvent("gameObject.getComponent", componentName);
|
|
126
533
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
this
|
|
534
|
+
|
|
535
|
+
AddComponent(componentName) {
|
|
536
|
+
return this?._invokeGameObjectEvent("gameObject.addComponent", componentName);
|
|
130
537
|
}
|
|
131
538
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
539
|
+
/**Only for internal library use*/
|
|
540
|
+
_invokeGameObjectEvent(eventName, payload) {
|
|
135
541
|
let payloadJson = payload;
|
|
136
542
|
if(typeof payload === "object") payloadJson = JSON.stringify(payload);
|
|
137
543
|
else if(typeof payload !== "string") payloadJson = payload.toString();
|
|
@@ -158,9 +564,65 @@
|
|
|
158
564
|
}
|
|
159
565
|
}
|
|
160
566
|
|
|
567
|
+
class Time {
|
|
568
|
+
/**
|
|
569
|
+
* Current time in seconds.
|
|
570
|
+
* @returns {number}
|
|
571
|
+
*/
|
|
572
|
+
static get time() {
|
|
573
|
+
return Unity.InvokeEvent("InstanceEvent:GetTime");
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* The time since the last frame in seconds.
|
|
578
|
+
* @returns {number}
|
|
579
|
+
*/
|
|
580
|
+
static get deltaTime() {
|
|
581
|
+
return Unity.InvokeEvent("InstanceEvent:GetDeltaTime");
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* The interval in seconds of in-game time at which physics and other fixed frame rate updates (like MonoBehaviour's MonoBehaviour.FixedUpdate) are performed.
|
|
586
|
+
* @returns {number}
|
|
587
|
+
*/
|
|
588
|
+
static get fixedDeltaTime() {
|
|
589
|
+
return Unity.InvokeEvent("InstanceEvent:GetFixedDeltaTime");
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/**
|
|
593
|
+
* The timeScale-independent interval in seconds from the last frame to the current one
|
|
594
|
+
* @returns {number}
|
|
595
|
+
*/
|
|
596
|
+
static get unscaledDeltaTime() {
|
|
597
|
+
return Unity.InvokeEvent("InstanceEvent:GetUnscaledDeltaTime");
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* The real time in seconds since Unity started running.
|
|
602
|
+
* @returns {number}
|
|
603
|
+
*/
|
|
604
|
+
static get realtimeSinceStartup() {
|
|
605
|
+
return Unity.InvokeEvent("InstanceEvent:GetRealtimeSinceStartup");
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* The scale at which time passes.
|
|
610
|
+
* @returns {number}
|
|
611
|
+
*/
|
|
612
|
+
static get timeScale() {
|
|
613
|
+
return Unity.InvokeEvent("InstanceEvent:GetTimeScale");
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
static set timeScale(value) {
|
|
617
|
+
Unity.InvokeEvent("InstanceEvent:SetTimeScale", value);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
161
621
|
let Unity$1 = class Unity {
|
|
162
622
|
/** @type {typeof GameObject} */
|
|
163
623
|
static GameObject = GameObject;
|
|
624
|
+
/** @type {typeof Time} */
|
|
625
|
+
static Time = Time;
|
|
164
626
|
static internalLogs = false;
|
|
165
627
|
static types = {
|
|
166
628
|
int: "System.Int32",
|
|
@@ -185,6 +647,11 @@
|
|
|
185
647
|
static #onInstanceReadyListeners = new Set();
|
|
186
648
|
static #onEventListeners = {};
|
|
187
649
|
|
|
650
|
+
/**
|
|
651
|
+
* Loads the Unity canvas from the specified URL and injects it into the specified element.
|
|
652
|
+
* @param {string} url
|
|
653
|
+
* @param {string} elementId
|
|
654
|
+
*/
|
|
188
655
|
static LoadInstance(url, elementId) {
|
|
189
656
|
Unity.#instanceReady = false;
|
|
190
657
|
const r = new XMLHttpRequest();
|
|
@@ -207,10 +674,29 @@
|
|
|
207
674
|
r.send();
|
|
208
675
|
}
|
|
209
676
|
|
|
677
|
+
/**
|
|
678
|
+
* Gets the current Unity version.
|
|
679
|
+
* @returns {string}
|
|
680
|
+
*/
|
|
681
|
+
static GetVersion() {
|
|
682
|
+
return Unity.InvokeEvent("InstanceEvent:GetUnityVersion");
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
/**
|
|
686
|
+
* Gets the current build version.
|
|
687
|
+
* @returns {string}
|
|
688
|
+
*/
|
|
210
689
|
static GetBuildVersion() {
|
|
211
690
|
return Unity.InvokeEvent("InstanceEvent:GetBuildVersion");
|
|
212
691
|
}
|
|
213
692
|
|
|
693
|
+
/**
|
|
694
|
+
* Invokes a global event that can be listened to by both Unity and JS.
|
|
695
|
+
* An optional payload can be sent, and the event can also return anything.
|
|
696
|
+
* @param {string} eventName
|
|
697
|
+
* @param {any} payload
|
|
698
|
+
* @returns {any}
|
|
699
|
+
*/
|
|
214
700
|
static InvokeEvent(eventName, payload = undefined) {
|
|
215
701
|
const responseJson = Unity.#invokeEventInternal(eventName, payload);
|
|
216
702
|
try {
|
|
@@ -228,6 +714,13 @@
|
|
|
228
714
|
}
|
|
229
715
|
}
|
|
230
716
|
|
|
717
|
+
/**
|
|
718
|
+
* Async version of InvokeEvent. Invokes a global async event that can be listened to by both Unity and JS.
|
|
719
|
+
* An optional payload can be sent, and the event can also return anything.
|
|
720
|
+
* @param {string} eventName
|
|
721
|
+
* @param {any} payload
|
|
722
|
+
* @returns {Promise<any>}
|
|
723
|
+
*/
|
|
231
724
|
static async InvokeEventAsync(eventName, payload = undefined) {
|
|
232
725
|
return new Promise(resolve => {
|
|
233
726
|
const responseJson = Unity.#invokeEventInternal(eventName, payload);
|
|
@@ -248,6 +741,10 @@
|
|
|
248
741
|
})
|
|
249
742
|
}
|
|
250
743
|
|
|
744
|
+
/**
|
|
745
|
+
* Awaits the Unity WaitForEndOfFrame coroutine.
|
|
746
|
+
* @returns {Promise<void>}
|
|
747
|
+
*/
|
|
251
748
|
static async WaitForEndOfFrameAsync() {
|
|
252
749
|
return new Promise((resolve) => {
|
|
253
750
|
const eventId = crypto.randomUUID().toString();
|
|
@@ -262,10 +759,56 @@
|
|
|
262
759
|
});
|
|
263
760
|
}
|
|
264
761
|
|
|
762
|
+
/**
|
|
763
|
+
* Loads a scene and awaits its completion.
|
|
764
|
+
* @param {string} sceneName
|
|
765
|
+
* @returns {Promise<void>}
|
|
766
|
+
*/
|
|
767
|
+
static async LoadSceneAsync(sceneName) {
|
|
768
|
+
return new Promise((resolve) => {
|
|
769
|
+
const eventName = `SceneLoadedEvent:${sceneName}`;
|
|
770
|
+
Unity.onEvent(eventName, () => {
|
|
771
|
+
resolve();
|
|
772
|
+
delete Unity.#onEventListeners[eventName];
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
Unity.InvokeEvent("InstanceEvent:LoadScene", sceneName);
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Returns true if a scene is currently loading.
|
|
781
|
+
* @returns {boolean}
|
|
782
|
+
*/
|
|
783
|
+
static IsSceneLoading() {
|
|
784
|
+
return Unity.InvokeEvent("InstanceEvent:IsSceneLoading");
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/**
|
|
788
|
+
* Returns the current scene load progress. If no scene is currently loading, it returns 0.
|
|
789
|
+
* @returns {number}
|
|
790
|
+
*/
|
|
791
|
+
static GetSceneLoadProgress() {
|
|
792
|
+
return Unity.InvokeEvent("InstanceEvent:GetSceneLoadProgress");
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
/**
|
|
796
|
+
* Loads an asset bundle from the provided URL and awaits its completion.
|
|
797
|
+
* @param {string} bundleUrl
|
|
798
|
+
* @returns {Promise<void>}
|
|
799
|
+
*/
|
|
265
800
|
static async LoadBundleAsync(bundleUrl) {
|
|
266
801
|
await Unity.InvokeEventAsync("InstanceEvent:LoadBundle", bundleUrl);
|
|
267
802
|
}
|
|
268
803
|
|
|
804
|
+
/**
|
|
805
|
+
* This function loads the bundle, instantiates a prefab from it, and then unloads the bundle so you don't have to handle it manually.
|
|
806
|
+
* An optional parent JS Key can be provided to place the instantiated prefab under a specific GameObject.
|
|
807
|
+
* @param {string} bundleUrl
|
|
808
|
+
* @param {string} prefabName
|
|
809
|
+
* @param {string} parentKey
|
|
810
|
+
* @returns {Promise<void>}
|
|
811
|
+
*/
|
|
269
812
|
static async InstantiatePrefabFromBundleAsync(bundleUrl, prefabName, parentKey = "") {
|
|
270
813
|
await Unity.InvokeEventAsync("InstanceEvent:InstantiatePrefabFromBundle", {
|
|
271
814
|
bundleUrl: bundleUrl,
|
|
@@ -276,6 +819,10 @@
|
|
|
276
819
|
|
|
277
820
|
// Listeners -----------------------------
|
|
278
821
|
|
|
822
|
+
/**
|
|
823
|
+
* Registers a callback that will be invoked when the Unity instance is ready.
|
|
824
|
+
* @param {function} callback
|
|
825
|
+
*/
|
|
279
826
|
static onInstanceReady(callback) {
|
|
280
827
|
if(!Unity.#instanceReady) {
|
|
281
828
|
Unity.#onInstanceReadyListeners.add(callback);
|
|
@@ -285,6 +832,11 @@
|
|
|
285
832
|
}
|
|
286
833
|
}
|
|
287
834
|
|
|
835
|
+
/**
|
|
836
|
+
* Registers a callback that will be invoked when a global event is received. It can listen to events from both Unity and JS.
|
|
837
|
+
* @param {string} eventName
|
|
838
|
+
* @param {function} callback
|
|
839
|
+
*/
|
|
288
840
|
static onEvent(eventName, callback) {
|
|
289
841
|
if(!Unity.#onEventListeners.hasOwnProperty(eventName)) {
|
|
290
842
|
Unity.#onEventListeners[eventName] = new Set();
|
|
@@ -292,6 +844,11 @@
|
|
|
292
844
|
Unity.#onEventListeners[eventName].add(callback);
|
|
293
845
|
}
|
|
294
846
|
|
|
847
|
+
/**
|
|
848
|
+
* Unregisters a callback previously registered with onEvent.
|
|
849
|
+
* @param {string} eventName
|
|
850
|
+
* @param {function} callback
|
|
851
|
+
*/
|
|
295
852
|
static offEvent(eventName, callback) {
|
|
296
853
|
if(!Unity.#onEventListeners.hasOwnProperty(eventName)) return;
|
|
297
854
|
Unity.#onEventListeners[eventName].delete(callback);
|
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#
|
|
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{constructor(e){this.gameObject=e}get position(){return this.#e("transform.getPosition","")}set position(e){this.#t(e),this.#e("transform.setPosition",e)}get localPosition(){return this.#e("transform.getLocalPosition","")}set localPosition(e){this.#t(e),this.#e("transform.setLocalPosition",e)}get rotation(){return this.#e("transform.getRotation","")}set rotation(e){this.#n(e),this.#e("transform.setRotation",e)}get eulerAngles(){return this.#e("transform.getEulerAngles","")}set eulerAngles(e){console.warn("[Unity] eulerAngles is read-only.")}get localScale(){return this.#e("transform.getLocalScale","")}set localScale(e){this.#t(e),this.#e("transform.setLocalScale",e)}get lossyScale(){return this.#e("transform.getLossyScale","")}set lossyScale(e){console.warn("[Unity] lossyScale is read-only.")}Translate(e,t,n){this.#e("transform.translate",{x:e,y:t,z:n})}Rotate(e,t,n){this.#e("transform.rotate",{x:e,y:t,z:n})}#e(e,t){return this.gameObject||console.error("[Unity] The GameObject associated with this Transform is null"),this.gameObject._invokeGameObjectEvent(e,t)}#t(e){if("object"!=typeof e||null===e||"number"!=typeof e.x||"number"!=typeof e.y||"number"!=typeof e.z)throw new Error(`[Unity] Invalid Vector3: ${JSON.stringify(e)}`)}#n(e){if("object"!=typeof e||null===e||"number"!=typeof e.x||"number"!=typeof e.y||"number"!=typeof e.z||"number"!=typeof e.w)throw new Error(`[Unity] Invalid Quaternion: ${JSON.stringify(e)}`)}}class t{constructor(e){this.gameObject=e}get mass(){return this.#e("physics.getMass","")}get useGravity(){return this.#e("physics.getUseGravity","")}get isKinematic(){return this.#e("physics.getIsKinematic","")}get linearDamping(){return this.#e("physics.getLinearDamping","")}get angularDamping(){return this.#e("physics.getAngularDamping","")}AddForce(e,t,n){this.#e("physics.addForce",{x:e,y:t,z:n})}AddTorque(e,t,n){this.#e("physics.addTorque",{x:e,y:t,z:n})}SetVelocity(e,t,n){this.#e("physics.setVelocity",{x:e,y:t,z:n})}GetVelocity(){return this.#e("physics.getVelocity","")}SetAngularVelocity(e,t,n){this.#e("physics.setAngularVelocity",{x:e,y:t,z:n})}GetAngularVelocity(){return this.#e("physics.getAngularVelocity","")}SetUseGravity(e){this.#e("physics.setUseGravity",e)}SetIsKinematic(e){this.#e("physics.setIsKinematic",e)}SetMass(e){this.#e("physics.setMass",e)}SetLinearDamping(e){this.#e("physics.setLinearDamping",e)}#e(e,t){return this.gameObject?this.gameObject._invokeGameObjectEvent(e,t):(console.error("[Unity] The GameObject associated with this RigidBody is null"),null)}}class n{#s=null;#a=null;static keyGameObjects={};static#i={awake:{},start:{},update:{},enable:{},disable:{},destroy:{}};static GetKeyGameObject(e){return n.keyGameObjects.hasOwnProperty(e)?n.keyGameObjects[e]:(console.error(`GameObject with key '${e}' not found`),null)}static Instantiate(t,s={x:0,y:0,z:0},a={x:0,y:0,z:0,w:1},i=null){if(null===i)Unity.InvokeEvent("InstanceEvent:InstantiateGameObject",{prefabPath:t,position:s,rotation:a});else{const r=i instanceof n?i:i instanceof e?i.gameObject:null;if(null===r)return;r.Instantiate(t,s,a)}}static onAwake(e,t){n.#i.awake.hasOwnProperty(e)||(n.#i.awake[e]=new Set),n.#i.awake[e].add(t)}static onStart(e,t){n.#i.start.hasOwnProperty(e)||(n.#i.start[e]=new Set),n.#i.start[e].add(t)}static onUpdate(e,t){n.#i.update.hasOwnProperty(e)||(n.#i.update[e]=new Set),n.#i.update[e].add(t)}static onEnable(e,t){n.#i.enable.hasOwnProperty(e)||(n.#i.enable[e]=new Set),n.#i.enable[e].add(t)}static onDisable(e,t){n.#i.disable.hasOwnProperty(e)||(n.#i.disable[e]=new Set),n.#i.disable[e].add(t)}static onDestroy(e,t){n.#i.destroy.hasOwnProperty(e)||(n.#i.destroy[e]=new Set),n.#i.destroy[e].add(t)}static _register(e,t){n.keyGameObjects[e]=new n(e,t)}static _receiveLifeCycleEvent(e,t){const s=n.keyGameObjects[e];if(!s)return;const a=n.#i[t][e];if(a)for(const e of a)e(s);"destroy"===t&&delete n.keyGameObjects[e]}constructor(n,s){this.key=n,this.name=s.name,this.#s=new e(this),s.hasRigidbody&&(this.#a=new t(this)),this.hierarchyPath=s.hasOwnProperty("hierarchyPath")?s.hierarchyPath:""}get transform(){return this.#s}get rigidbody(){return this.HasComponent("Rigidbody")?(this.#a=new t(this),this.#a):null}SetActive(e){this?._invokeGameObjectEvent("gameObject.setActive",e)}InvokeMethod(e,t="",n=""){t=Unity.types[t]||t,this?._invokeGameObjectEvent("gameObject.invokeMethod",{methodName:e,parameterType:t,parameterValue:n})}GetChild(e){const t="string"==typeof e?"gameObject.findChild":"gameObject.getChild",s=this?._invokeGameObjectEvent(t,e);if(null!==s){const e=""===this.hierarchyPath?this.key:this.hierarchyPath;return s.hierarchyPath=e+"/"+s.name,new n(this.key,s)}return null}SetText(e){this?._invokeGameObjectEvent("text.setText",e)}Destroy(){this?._invokeGameObjectEvent("gameObject.destroy","")}Instantiate(e,t={x:0,y:0,z:0},n={x:0,y:0,z:0,w:1}){this?._invokeGameObjectEvent("gameObject.instantiate",{prefabPath:e,position:t,rotation:n})}HasComponent(e){return this?._invokeGameObjectEvent("gameObject.hasComponent",e)}GetComponent(e){return this?._invokeGameObjectEvent("gameObject.getComponent",e)}AddComponent(e){return this?._invokeGameObjectEvent("gameObject.addComponent",e)}_invokeGameObjectEvent(e,t){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}}class s{static get time(){return Unity.InvokeEvent("InstanceEvent:GetTime")}static get deltaTime(){return Unity.InvokeEvent("InstanceEvent:GetDeltaTime")}static get fixedDeltaTime(){return Unity.InvokeEvent("InstanceEvent:GetFixedDeltaTime")}static get unscaledDeltaTime(){return Unity.InvokeEvent("InstanceEvent:GetUnscaledDeltaTime")}static get realtimeSinceStartup(){return Unity.InvokeEvent("InstanceEvent:GetRealtimeSinceStartup")}static get timeScale(){return Unity.InvokeEvent("InstanceEvent:GetTimeScale")}static set timeScale(e){Unity.InvokeEvent("InstanceEvent:SetTimeScale",e)}}return class e{static GameObject=n;static Time=s;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#r;static#o=!1;static#c=new Set;static#l={};static LoadInstance(t,n){e.#o=!1;const s=new XMLHttpRequest;s.open("GET",t+"/index.html",!0),s.onreadystatechange=function(){if(4!==s.readyState||200!==s.status)return;document.querySelector(`#${n}`).innerHTML=s.responseText;const e=document.createElement("link");e.rel="stylesheet",e.type="text/css",e.href=t+"/TemplateData/style.css",document.head.appendChild(e);const a=document.createElement("script");a.src=t+"/index.js",document.body.appendChild(a)},s.send()}static GetVersion(){return e.InvokeEvent("InstanceEvent:GetUnityVersion")}static GetBuildVersion(){return e.InvokeEvent("InstanceEvent:GetBuildVersion")}static InvokeEvent(t,n=void 0){const s=e.#y(t,n);try{const n=JSON.parse(s);return n.hasOwnProperty("promiseId")&&(console.warn(`Event '${t}' returned a promise. Consider using InvokeEventAsync instead.`),e.onEvent(`PromiseResolvedEvent:${n.promiseId}`,t=>{delete e.#l[`PromiseResolvedEvent:${n.promiseId}`]})),n}catch{return s}}static async InvokeEventAsync(t,n=void 0){return new Promise(s=>{const a=e.#y(t,n);try{const t=JSON.parse(a);t.hasOwnProperty("promiseId")?e.onEvent(`PromiseResolvedEvent:${t.promiseId}`,n=>{s(n),delete e.#l[`PromiseResolvedEvent:${t.promiseId}`]}):s(t)}catch{s(a)}})}static async WaitForEndOfFrameAsync(){return new Promise(t=>{const n=crypto.randomUUID().toString(),s=`EndOfFrameEvent:${n}`;e.onEvent(s,()=>{t(),delete e.#l[s]}),e.InvokeEvent("InstanceEvent:WaitForEndOfFrame",n)})}static async LoadSceneAsync(t){return new Promise(n=>{const s=`SceneLoadedEvent:${t}`;e.onEvent(s,()=>{n(),delete e.#l[s]}),e.InvokeEvent("InstanceEvent:LoadScene",t)})}static IsSceneLoading(){return e.InvokeEvent("InstanceEvent:IsSceneLoading")}static GetSceneLoadProgress(){return e.InvokeEvent("InstanceEvent:GetSceneLoadProgress")}static async LoadBundleAsync(t){await e.InvokeEventAsync("InstanceEvent:LoadBundle",t)}static async InstantiatePrefabFromBundleAsync(t,n,s=""){await e.InvokeEventAsync("InstanceEvent:InstantiatePrefabFromBundle",{bundleUrl:t,prefabName:n,parentKey:s})}static onInstanceReady(t){e.#o?t():e.#c.add(t)}static onEvent(t,n){e.#l.hasOwnProperty(t)||(e.#l[t]=new Set),e.#l[t].add(n)}static offEvent(t,n){e.#l.hasOwnProperty(t)&&e.#l[t].delete(n)}static#y(t,n){null==n&&(n="");let s=n;"object"==typeof n?s=JSON.stringify(n):"string"!=typeof n&&(s=n.toString());const a=e.#r(t,s);return e.#l[t]?.forEach(e=>e(s)),a}static _instanceReady(){e.#o=!0,e.#c.forEach(e=>e())}static _registerClientListener(t){e.#r=t}static _receiveEvent(t,n){let s=n;try{s=JSON.parse(n)}catch{}e.InvokeEvent(t,s)}static _logFromUnity(t,n){("internal"!==t||e.internalLogs)&&("error"===t?console.error(`[Unity] ${n}`):"warning"===t?console.warn(`[Unity] ${n}`):console.log(`[Unity] ${n}`))}}});
|