Skip to content

Commit 7742c82

Browse files
committed
Merge branch 'master' of https://github.com/JohannesDeml/UnityWebGL-LoadingTest into 6000.0
2 parents e8a3aa6 + 6d06857 commit 7742c82

File tree

6 files changed

+227
-94
lines changed

6 files changed

+227
-94
lines changed

.github/workflows/release.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ on:
55
push:
66
tags:
77
- '*'
8+
branches:
9+
- master
810

911
jobs:
1012
variables:
@@ -28,7 +30,12 @@ jobs:
2830
2931
- name: Set tag
3032
id: set_tag
31-
run: echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT
33+
run: |
34+
if [[ $GITHUB_REF == refs/tags/* ]]; then
35+
echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT
36+
else
37+
echo "VERSION=${{ steps.set_unity_version.outputs.VERSION }}-webgl2-master" >> $GITHUB_OUTPUT
38+
fi
3239
3340
- name: Set target name
3441
id: set_build_name
@@ -62,8 +69,8 @@ jobs:
6269
6370
buildProject:
6471
name: Create Unity WebGL Build 🏗
65-
# only build with additional parameters, the tag alone should only create a release draft
66-
if: ${{ needs.variables.outputs.TAG != needs.variables.outputs.UNITY_VERSION }}
72+
# Build if it's a master push or if the tag is not just the unity version
73+
if: ${{ github.ref == 'refs/heads/master' || needs.variables.outputs.TAG != needs.variables.outputs.UNITY_VERSION }}
6774
needs: [ variables ]
6875
runs-on: ubuntu-latest
6976
strategy:
@@ -112,7 +119,7 @@ jobs:
112119
createRelease:
113120
name: Create Github release 🐙
114121
# only run for the pure tag without build parameters
115-
if: ${{ needs.variables.outputs.TAG == needs.variables.outputs.UNITY_VERSION }}
122+
if: ${{ github.ref_type == 'tag' && needs.variables.outputs.TAG == needs.variables.outputs.UNITY_VERSION }}
116123
needs: [ variables ]
117124
runs-on: ubuntu-latest
118125
steps:

Assets/Plugins/WebGL/WebBridge/CommonCommands.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// --------------------------------------------------------------------------------------------------------------------
1010

1111
using System;
12+
using System.Collections;
1213
using System.Collections.Generic;
1314
using Supyrb.Attributes;
1415
using UnityEngine;
@@ -274,5 +275,84 @@ public void LogShaderCompilation(int enabled)
274275
GraphicsSettings.logWhenShaderIsCompiled = enabled == 1;
275276
Debug.Log($"GraphicsSettings.logWhenShaderIsCompiled: {GraphicsSettings.logWhenShaderIsCompiled}");
276277
}
278+
279+
/// <summary>
280+
/// Copy text to clipboard using the browser's clipboard API
281+
/// </summary>
282+
/// <param name="text">Text to copy to clipboard</param>
283+
[WebCommand(Description = "Copy text to clipboard")]
284+
public void CopyToClipboard(string text)
285+
{
286+
WebToolPlugins.CopyToClipboard(text);
287+
}
288+
289+
/// <summary>
290+
/// Check if the browser has an internet connection
291+
/// </summary>
292+
[WebCommand(Description = "Check if browser is online")]
293+
public void CheckOnlineStatus()
294+
{
295+
bool isOnline = WebToolPlugins.IsOnline();
296+
Debug.Log($"<color=#4D65A4>Online Status:</color> {(isOnline ? "<color=#3bb508>Connected</color>" : "<color=#b50808>Disconnected</color>")}");
297+
}
298+
299+
/// <summary>
300+
/// Captures the current screen and saves it as a PNG file.
301+
/// </summary>
302+
[WebCommand(Description = "Save current screen as PNG")]
303+
public void SaveScreenshot()
304+
{
305+
SaveScreenshotSuperSize(1);
306+
}
307+
308+
/// <summary>
309+
/// Captures the current screen and saves it as a PNG file.
310+
/// </summary>
311+
/// <param name="superSize">1 for normal size, 2 for double size, 4 for quadruple size</param>
312+
[WebCommand(Description = "Save current screen as PNG with variable super size")]
313+
public void SaveScreenshotSuperSize(int superSize)
314+
{
315+
StartCoroutine(CaptureScreenshot(superSize));
316+
}
317+
318+
private IEnumerator CaptureScreenshot(int superSize)
319+
{
320+
// Wait for the end of frame to ensure everything is rendered
321+
yield return new WaitForEndOfFrame();
322+
323+
string filename = "screenshot.png";
324+
try
325+
{
326+
// Capture the screen
327+
Texture2D screenshot = ScreenCapture.CaptureScreenshotAsTexture(superSize);
328+
329+
try
330+
{
331+
// Convert to PNG
332+
byte[] pngData = screenshot.EncodeToPNG();
333+
334+
// Download through browser
335+
WebToolPlugins.DownloadBinaryFile(filename, pngData, "image/png");
336+
337+
Debug.Log($"Screenshot saved as {filename} ({screenshot.width}x{screenshot.height}) with size {pngData.Length} bytes");
338+
}
339+
finally
340+
{
341+
// Clean up the texture
342+
if (Application.isPlaying)
343+
{
344+
Destroy(screenshot);
345+
}
346+
else
347+
{
348+
DestroyImmediate(screenshot);
349+
}
350+
}
351+
}
352+
catch (System.Exception e)
353+
{
354+
Debug.LogError($"Failed to save screenshot: {e.Message}");
355+
}
356+
}
277357
}
278358
}

Assets/Plugins/WebGL/WebBridge/WebBridge.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public void Help()
109109
{
110110
sb.AppendLine($"<b>---{webCommand.GetType().Name}---</b>");
111111
MethodInfo[] methods = webCommand.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
112+
Array.Sort(methods, (a, b) => string.Compare(a.Name, b.Name));
112113

113114
for (int i = 0; i < methods.Length; i++)
114115
{
@@ -123,11 +124,11 @@ public void Help()
123124
var parameter = parameters[j];
124125
if (parameter.ParameterType == typeof(string))
125126
{
126-
sb.Append($", \"{parameter.ParameterType} {parameter.Name}\"");
127+
sb.Append($", \"{GetFriendlyTypeName(parameter.ParameterType)} {parameter.Name}\"");
127128
}
128129
else
129130
{
130-
sb.Append($", {parameter.ParameterType} {parameter.Name}");
131+
sb.Append($", {GetFriendlyTypeName(parameter.ParameterType)} {parameter.Name}");
131132
}
132133
}
133134

@@ -140,5 +141,16 @@ public void Help()
140141
sb.AppendLine($"\nRun a command with 'runUnityCommand(\"COMMAND_NAME\",PARAMETER);'");
141142
Debug.Log(sb.ToString());
142143
}
144+
145+
private string GetFriendlyTypeName(Type type)
146+
{
147+
if (type == typeof(int)) return "int";
148+
if (type == typeof(long)) return "long";
149+
if (type == typeof(float)) return "float";
150+
if (type == typeof(double)) return "double";
151+
if (type == typeof(bool)) return "bool";
152+
if (type == typeof(string)) return "string";
153+
return type.Name;
154+
}
143155
}
144156
}

Assets/Plugins/WebGL/WebTools/WebToolPlugins.cs

Lines changed: 80 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ public static class WebToolPlugins
3535
[DllImport("__Internal")]
3636
private static extern uint _GetTotalMemorySize();
3737
[DllImport("__Internal")]
38-
private static extern uint _GetStaticMemorySize();
38+
private static extern bool _CopyToClipboard(string text);
3939
[DllImport("__Internal")]
40-
private static extern uint _GetDynamicMemorySize();
40+
private static extern int _IsOnline();
41+
[DllImport("__Internal")]
42+
private static extern void _DownloadFile(string filename, string content);
43+
[DllImport("__Internal")]
44+
private static extern void _DownloadBlob(string filename, byte[] byteArray, int byteLength, string mimeType);
45+
4146
#endif
4247

4348
private static bool _infoPanelVisible = false;
@@ -154,106 +159,125 @@ public static bool IsMobileDevice()
154159
userAgent.Contains("Android");
155160
}
156161

157-
/// <summary>
158-
/// Get the total memory size used by the application in MB
159-
/// </summary>
160-
/// <returns>Size in MB</returns>
161-
public static float GetTotalMemorySize()
162-
{
163-
#if UNITY_WEBGL && !UNITY_EDITOR
164-
var bytes = _GetTotalMemorySize();
165-
return GetMegaBytes(bytes);
166-
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
167-
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(GetTotalMemorySize)} called");
168-
return -1f;
169-
#else
170-
return -1f;
171-
#endif
172-
}
173-
174162
/// <summary>
175163
/// Log all current memory data in MB
176164
/// </summary>
177165
public static void LogMemory()
178166
{
179167
#if UNITY_WEBGL && !UNITY_EDITOR
180168
var managed = GetManagedMemorySize();
181-
var native = GetNativeMemorySize();
182169
var total = GetTotalMemorySize();
183-
Debug.Log($"Memory stats:\nManaged: {managed:0.00}MB\nNative: {native:0.00}MB\nTotal: {total:0.00}MB");
170+
Debug.Log($"Memory stats:\nManaged: {managed:0.00}MB\nTotal: {total:0.00}MB");
184171
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
185172
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(LogMemory)} called");
186173
#endif
187174
}
188175

189176
/// <summary>
190-
/// Get the static memory size used by the application in MB
177+
/// Get the total memory size used by the application in MB
191178
/// </summary>
192179
/// <returns>Size in MB</returns>
193-
public static float GetStaticMemorySize()
180+
public static float GetTotalMemorySize()
194181
{
195182
#if UNITY_WEBGL && !UNITY_EDITOR
196-
var bytes = _GetStaticMemorySize();
183+
var bytes = _GetTotalMemorySize();
197184
return GetMegaBytes(bytes);
198185
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
199-
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(GetStaticMemorySize)} called");
186+
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(GetTotalMemorySize)} called");
200187
return -1f;
201188
#else
202189
return -1f;
203190
#endif
204191
}
205192

206193
/// <summary>
207-
/// Get the dynamic memory size used by the application in MB
194+
/// Get the managed memory size used by the application in MB
208195
/// </summary>
209196
/// <returns>Size in MB</returns>
210-
public static float GetDynamicMemorySize()
197+
public static float GetManagedMemorySize()
211198
{
212-
#if UNITY_WEBGL && !UNITY_EDITOR
213-
var bytes = _GetStaticMemorySize();
199+
var bytes = (uint)GC.GetTotalMemory(false);
214200
return GetMegaBytes(bytes);
215-
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
216-
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(GetDynamicMemorySize)} called");
217-
return -1f;
218-
#else
219-
return -1f;
220-
#endif
221201
}
222202

223203
/// <summary>
224-
/// Get the native memory size used by the application in MB (Static + Dynamic memory)
204+
/// Converts bytes (B) to mega bytes (MB)
225205
/// </summary>
226-
/// <returns>Size in MB</returns>
227-
public static float GetNativeMemorySize()
206+
/// <param name="bytes">bytes to convert</param>
207+
/// <returns>bytes / (1024 * 1024)</returns>
208+
private static float GetMegaBytes(uint bytes)
228209
{
229-
#if UNITY_WEBGL && !UNITY_EDITOR
230-
return GetDynamicMemorySize() + GetStaticMemorySize();
231-
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
232-
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(GetNativeMemorySize)} called");
233-
return -1f;
234-
#else
235-
return -1f;
236-
#endif
210+
return (float)bytes / (1024 * 1024);
237211
}
238212

239213
/// <summary>
240-
/// Get the managed memory size used by the application in MB
214+
/// Copies the specified text to the system clipboard using the browser's clipboard API.
215+
/// Only works in WebGL builds and requires clipboard-write permission in modern browsers.
241216
/// </summary>
242-
/// <returns>Size in MB</returns>
243-
public static float GetManagedMemorySize()
217+
/// <param name="text">The text to copy to the clipboard</param>
218+
/// <returns>True if the copy operation was successful, false otherwise</returns>
219+
public static void CopyToClipboard(string text)
244220
{
245-
var bytes = (uint)GC.GetTotalMemory(false);
246-
return GetMegaBytes(bytes);
221+
#if UNITY_WEBGL && !UNITY_EDITOR
222+
_CopyToClipboard(text);
223+
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
224+
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(CopyToClipboard)} called with: {text}");
225+
#endif
247226
}
248227

249228
/// <summary>
250-
/// Converts bytes (B) to mega bytes (MB)
229+
/// Checks if the browser currently has an internet connection using the navigator.onLine property.
251230
/// </summary>
252-
/// <param name="bytes">bytes to convert</param>
253-
/// <returns>bytes / (1024 * 1024)</returns>
254-
private static float GetMegaBytes(uint bytes)
231+
/// <returns>True if the browser is online, false if it's offline</returns>
232+
public static bool IsOnline()
255233
{
256-
return (float)bytes / (1024 * 1024);
234+
#if UNITY_WEBGL && !UNITY_EDITOR
235+
return _IsOnline() == 1;
236+
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
237+
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(IsOnline)} called");
238+
return true;
239+
#else
240+
return true;
241+
#endif
242+
}
243+
244+
/// <summary>
245+
/// Downloads a text file through the browser with the specified filename and content.
246+
/// Creates a temporary anchor element to trigger the download.
247+
/// </summary>
248+
/// <param name="filename">The name of the file to be downloaded</param>
249+
/// <param name="content">The text content to be saved in the file</param>
250+
public static void DownloadTextFile(string filename, string content)
251+
{
252+
#if UNITY_WEBGL && !UNITY_EDITOR
253+
_DownloadFile(filename, content);
254+
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
255+
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(DownloadTextFile)} called with filename: {filename}");
256+
#endif
257+
}
258+
259+
/// <summary>
260+
/// Downloads a binary file through the browser with the specified filename and data.
261+
/// Creates a Blob with the specified MIME type and triggers the download.
262+
/// </summary>
263+
/// <param name="filename">The name of the file to be downloaded</param>
264+
/// <param name="data">The binary data to be saved in the file</param>
265+
/// <param name="mimeType">The MIME type of the file (defaults to "application/octet-stream")</param>
266+
/// <example>
267+
/// <code>
268+
/// // Example: Save a Texture2D as PNG
269+
/// Texture2D texture;
270+
/// byte[] pngData = texture.EncodeToPNG();
271+
/// WebToolPlugins.DownloadBinaryFile("texture.png", pngData, "image/png");
272+
/// </code>
273+
/// </example>
274+
public static void DownloadBinaryFile(string filename, byte[] data, string mimeType = "application/octet-stream")
275+
{
276+
#if UNITY_WEBGL && !UNITY_EDITOR
277+
_DownloadBlob(filename, data, data.Length, mimeType);
278+
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
279+
Debug.Log($"{nameof(WebToolPlugins)}.{nameof(DownloadBinaryFile)} called with filename: {filename}");
280+
#endif
257281
}
258282
}
259283
}

0 commit comments

Comments
 (0)