Skip to content

Commit 2c3e6fe

Browse files
committed
2 parents 871777c + b20ac76 commit 2c3e6fe

File tree

6 files changed

+116
-11
lines changed

6 files changed

+116
-11
lines changed

Assets/Plugins/WebGL/WebBridge/CommonCommands.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,38 @@ public void SetCursor(string cursorName)
393393
WebToolPlugins.SetCursor(cursorName);
394394
}
395395

396+
/// <summary>
397+
/// Triggers a vibration on devices that support it.
398+
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "Vibrate", 200);</code>
399+
/// </summary>
400+
/// <param name="durationInMs">Duration of the vibration in milliseconds</param>
401+
[WebCommand(Description = "Vibrate for a given duration in ms")]
402+
public void Vibrate(int durationInMs)
403+
{
404+
WebToolPlugins.Vibrate(durationInMs);
405+
}
406+
407+
/// <summary>
408+
/// Stops any ongoing vibration.
409+
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "StopCurrentVibration");</code>
410+
/// </summary>
411+
[WebCommand(Description = "Stop any ongoing vibration")]
412+
public void StopCurrentVibration()
413+
{
414+
WebToolPlugins.StopCurrentVibration();
415+
}
416+
417+
/// <summary>
418+
/// Triggers a vibration SOS pattern (... --- ...) on devices that support it.
419+
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "VibrateSosPattern");</code>
420+
/// </summary>
421+
[WebCommand(Description = "Vibrate SOS pattern (... --- ...)")]
422+
[ContextMenu(nameof(VibrateSosPattern))]
423+
public void VibrateSosPattern()
424+
{
425+
WebToolPlugins.Vibrate(new int[] { 100, 30, 100, 30, 100, 30, 200, 30, 200, 30, 200, 30, 100, 30, 100, 30, 100 });
426+
}
427+
396428
/// <summary>
397429
/// Captures the current screen and saves it as a PNG file.
398430
/// </summary>

Assets/Plugins/WebGL/WebTools/WebToolPlugins.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public static class WebToolPlugins
4646
private static extern void _DownloadBlob(string filename, byte[] byteArray, int byteLength, string mimeType);
4747
[DllImport("__Internal")]
4848
private static extern void _SetCursor(string cursorName);
49+
[DllImport("__Internal")]
50+
private static extern void _Vibrate(int durationInMs);
51+
[DllImport("__Internal")]
52+
private static extern void _VibratePattern(int[] durationsInMs, int length);
4953

5054
#endif
5155

@@ -330,5 +334,47 @@ public static void SetCursor(string cursorName)
330334
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(SetCursor)} called with cursor: {cursorName}");
331335
#endif
332336
}
337+
338+
/// <summary>
339+
/// Triggers a vibration on devices that support it using the Vibration API.
340+
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/vibrate"/>
341+
/// </summary>
342+
/// <param name="durationInMs">Duration of the vibration in milliseconds</param>
343+
public static void Vibrate(int durationInMs)
344+
{
345+
#if UNITY_WEBGL && !UNITY_EDITOR
346+
_Vibrate(durationInMs);
347+
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
348+
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(Vibrate)} called with duration: {durationInMs}ms");
349+
#endif
350+
}
351+
352+
/// <summary>
353+
/// Triggers a vibration with 0ms on devices that support it using the Vibration API.
354+
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/vibrate"/>
355+
/// </summary>
356+
public static void StopCurrentVibration()
357+
{
358+
#if UNITY_WEBGL && !UNITY_EDITOR
359+
_Vibrate(0);
360+
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
361+
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(StopCurrentVibration)} called");
362+
#endif
363+
}
364+
365+
/// <summary>
366+
/// Triggers a vibration pattern on devices that support it using the Vibration API.
367+
/// Alternating values represent vibration and pause durations.
368+
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/vibrate"/>
369+
/// </summary>
370+
/// <param name="durationsInMs">Alternating vibration/pause durations in milliseconds</param>
371+
public static void Vibrate(int[] durationsInMs)
372+
{
373+
#if UNITY_WEBGL && !UNITY_EDITOR
374+
_VibratePattern(durationsInMs, durationsInMs.Length);
375+
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
376+
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(Vibrate)} called with pattern: [{string.Join(", ", durationsInMs)}]ms");
377+
#endif
378+
}
333379
}
334380
}

Assets/Plugins/WebGL/WebTools/WebToolPlugins.jslib

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,26 @@ var WebGlPlugins =
169169
if (canvasElement) {
170170
canvasElement.style.cursor = cursorStr;
171171
}
172+
},
173+
174+
_Vibrate: function(durationInMs) {
175+
if (!navigator.vibrate) {
176+
console.warn("Vibration API is not supported in this browser.");
177+
return;
178+
}
179+
navigator.vibrate(durationInMs);
180+
},
181+
182+
_VibratePattern: function(durationsInMs, length) {
183+
if (!navigator.vibrate) {
184+
console.warn("Vibration API is not supported in this browser.");
185+
return;
186+
}
187+
var pattern = [];
188+
for (var i = 0; i < length; i++) {
189+
pattern.push(HEAP32[(durationsInMs >> 2) + i]);
190+
}
191+
navigator.vibrate(pattern);
172192
}
173193
};
174194

-1.84 KB
Loading

Documentation/urp-build-sizes.png

-1010 Bytes
Loading

README.md

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
![Preview](./preview.png)
44

5-
[![](https://img.shields.io/github/release-date/JohannesDeml/UnityWebGL-LoadingTest.svg)](https://github.com/JohannesDeml/UnityWebGL-LoadingTest/releases) [![Tested up to Unity 6.3](https://img.shields.io/badge/tested%20up%20to%20unity-6000.3-green.svg?logo=unity&cacheSeconds=2592000)](https://unity3d.com/get-unity/download/archive)
5+
[![](https://img.shields.io/github/release-date/JohannesDeml/UnityWebGL-LoadingTest.svg)](https://github.com/JohannesDeml/UnityWebGL-LoadingTest/releases) [![Tested up to Unity 6.4](https://img.shields.io/badge/tested%20up%20to%20unity-6000.4-green.svg?logo=unity&cacheSeconds=2592000)](https://unity3d.com/get-unity/download/archive)
66

7-
*Testing Unity's WebGL size and loading time for different versions (2018.4 - 6000.3) and settings*
7+
*Testing Unity's WebGL size and loading time for different versions (2018.4 - 6000.4) and settings*
88

99
* [Overview page of all builds](https://deml.io/experiments/unity-webgl/)
1010
* [Implementation in Godot](https://github.com/JohannesDeml/Godot-Web-LoadingTest)
@@ -34,10 +34,11 @@ BiRP Build Size | URP Build Size
3434

3535
Version | Size | Link
3636
--- | --- | ---
37-
6000.3.2f1 | 3.46 MB | https://deml.io/experiments/unity-webgl/6000.3.2f1-webgl2
37+
6000.4.0f1 | 3.57 MB | https://deml.io/experiments/unity-webgl/6000.4.0f1-webgl2
38+
6000.3.11f1 | 3.50 MB | https://deml.io/experiments/unity-webgl/6000.3.11f1-webgl2
3839
6000.2.15f1 | 3.40 MB | https://deml.io/experiments/unity-webgl/6000.2.15f1-webgl2
3940
6000.1.17f1 | 3.34 MB | https://deml.io/experiments/unity-webgl/6000.1.17f1-webgl2
40-
6000.0.64f1 | 3.34 MB | https://deml.io/experiments/unity-webgl/6000.0.64f1-webgl2
41+
6000.0.66f2 | 3.38 MB | https://deml.io/experiments/unity-webgl/6000.0.66f2-webgl2
4142
2023.2.20f1 | 3.28 MB | https://deml.io/experiments/unity-webgl/2023.2.20f1-webgl2
4243
2023.1.20f1 | 3.20 MB | https://deml.io/experiments/unity-webgl/2023.1.20f1-webgl2
4344
2022.3.62f3 | 3.19 MB | https://deml.io/experiments/unity-webgl/2022.3.62f3-webgl2
@@ -60,10 +61,11 @@ Version | Size | Link
6061

6162
Version | Size | Link
6263
--- | --- | ---
63-
6000.3.2f1 | 3.05 MB | https://deml.io/experiments/unity-webgl/6000.3.2f1-minsize-webgl2
64+
6000.4.0f1 | 3.13 MB | https://deml.io/experiments/unity-webgl/6000.4.0f1-minsize-webgl2
65+
6000.3.11f1 | 3.09 MB | https://deml.io/experiments/unity-webgl/6000.3.11f1-minsize-webgl2
6466
6000.2.15f1 | 2.92 MB | https://deml.io/experiments/unity-webgl/6000.2.15f1-minsize-webgl2
6567
6000.1.17f1 | 2.86 MB | https://deml.io/experiments/unity-webgl/6000.1.17f1-minsize-webgl2
66-
6000.0.64f1 | 2.94 MB | https://deml.io/experiments/unity-webgl/6000.0.64f1-minsize-webgl2
68+
6000.0.66f2 | 2.97 MB | https://deml.io/experiments/unity-webgl/6000.0.66f2-minsize-webgl2
6769
2023.2.20f1 | 2.89 MB | https://deml.io/experiments/unity-webgl/2023.2.20f1-minsize-webgl2
6870
2023.1.20f1 | 2.77 MB | https://deml.io/experiments/unity-webgl/2023.1.20f1-minsize-webgl2
6971
2022.3.62f3 | 2.74 MB | https://deml.io/experiments/unity-webgl/2022.3.62f3-minsize-webgl1
@@ -76,10 +78,11 @@ Version | Size | Link
7678

7779
Version | Size | Link
7880
--- | --- | ---
79-
6000.3.2f1 | 8.23 MB | https://deml.io/experiments/unity-webgl/6000.3.2f1-urp-webgl2
81+
6000.4.0f1 | 8.74 MB | https://deml.io/experiments/unity-webgl/6000.4.0f1-urp-webgl2
82+
6000.3.11f1 | 8.65 MB | https://deml.io/experiments/unity-webgl/6000.3.11f1-urp-webgl2
8083
6000.2.15f1 | 8.13 MB | https://deml.io/experiments/unity-webgl/6000.2.15f1-urp-webgl2
8184
6000.1.17f1 | 7.97 MB | https://deml.io/experiments/unity-webgl/6000.1.17f1-urp-webgl2
82-
6000.0.64f1 | 7.94 MB | https://deml.io/experiments/unity-webgl/6000.0.64f1-urp-webgl2
85+
6000.0.66f2 | 8.36 MB | https://deml.io/experiments/unity-webgl/6000.0.66f2-urp-webgl2
8386
2023.2.20f1 | 6.88 MB | https://deml.io/experiments/unity-webgl/2023.2.20f1-urp-webgl2
8487
2023.1.20f1 | 6.29 MB | https://deml.io/experiments/unity-webgl/2023.1.20f1-urp-webgl2
8588
2022.3.62f3 | 5.99 MB | https://deml.io/experiments/unity-webgl/2022.3.62f3-urp-webgl2
@@ -102,18 +105,18 @@ Version | Size | Link
102105

103106
Version | Size | Link
104107
--- | --- | ---
105-
6000.3.2f1 | 6.30 MB | https://deml.io/experiments/unity-webgl/6000.3.2f1-urp-minsize-webgl2
108+
6000.4.0f1 | 6.78 MB | https://deml.io/experiments/unity-webgl/6000.4.0f1-urp-minsize-webgl2
109+
6000.3.11f1 | 6.72 MB | https://deml.io/experiments/unity-webgl/6000.3.11f1-urp-minsize-webgl2
106110
6000.2.15f1 | 6.24 MB | https://deml.io/experiments/unity-webgl/6000.2.15f1-urp-minsize-webgl2
107111
6000.1.17f1 | 5.64 MB | https://deml.io/experiments/unity-webgl/6000.1.17f1-urp-minsize-webgl2
108-
6000.0.64f1 | 6.11 MB | https://deml.io/experiments/unity-webgl/6000.0.64f1-urp-minsize-webgl2
112+
6000.0.66f2 | 6.52 MB | https://deml.io/experiments/unity-webgl/6000.0.66f2-urp-minsize-webgl2
109113
2023.2.20f1 | 5.33 MB | https://deml.io/experiments/unity-webgl/2023.2.20f1-urp-minsize-webgl2
110114
2023.1.20f1 | 4.88 MB | https://deml.io/experiments/unity-webgl/2023.1.20f1-urp-minsize-webgl2
111115
2022.3.62f3 | 4.69 MB | https://deml.io/experiments/unity-webgl/2022.3.62f3-urp-minsize-webgl1
112116
2022.2.18f1 | 5.92 MB | https://deml.io/experiments/unity-webgl/2022.2.18f1-urp-minsize-webgl1
113117
2022.1.24f1 | 5.71 MB | https://deml.io/experiments/unity-webgl/2022.1.24f1-urp-minsize-webgl1
114118
2021.3.45f2 | 5.93 MB | https://deml.io/experiments/unity-webgl/2021.3.45f2-urp-minsize-webgl1
115119

116-
117120
## Platform Compatibility
118121

119122
| Platform | Chrome | Firefox | Edge | Safari | Internet Explorer |
@@ -262,3 +265,7 @@ You can find a list of all live builds with their sizes over here: https://deml.
262265
## License
263266

264267
* MIT (c) Johannes Deml - see [LICENSE](./LICENSE.md)
268+
269+
270+
Hosted on
271+
[<img src="https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons/svg/codeberg.svg" alt="drawing" width="16"/> Codeberg](https://codeberg.org/jd/unity-web-comparison) and [<img src="https://cdn.jsdelivr.net/gh/homarr-labs/dashboard-icons/svg/github.svg" alt="drawing" width="16"/> GitHub](https://github.com/JohannesDeml/UnityWebGL-LoadingTest)

0 commit comments

Comments
 (0)