Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…oadingTest into 2022.3
  • Loading branch information
JohannesDeml committed Dec 16, 2024
2 parents d581e24 + 6d06857 commit 36b6dd9
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 87 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
push:
tags:
- '*'
branches:
- master

jobs:
variables:
Expand All @@ -28,7 +30,12 @@ jobs:
- name: Set tag
id: set_tag
run: echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT
run: |
if [[ $GITHUB_REF == refs/tags/* ]]; then
echo "VERSION=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT
else
echo "VERSION=${{ steps.set_unity_version.outputs.VERSION }}-webgl2-master" >> $GITHUB_OUTPUT
fi
- name: Set target name
id: set_build_name
Expand Down Expand Up @@ -62,8 +69,8 @@ jobs:
buildProject:
name: Create Unity WebGL Build 🏗
# only build with additional parameters, the tag alone should only create a release draft
if: ${{ needs.variables.outputs.TAG != needs.variables.outputs.UNITY_VERSION }}
# Build if it's a master push or if the tag is not just the unity version
if: ${{ github.ref == 'refs/heads/master' || needs.variables.outputs.TAG != needs.variables.outputs.UNITY_VERSION }}
needs: [ variables ]
runs-on: ubuntu-latest
strategy:
Expand Down Expand Up @@ -112,7 +119,7 @@ jobs:
createRelease:
name: Create Github release 🐙
# only run for the pure tag without build parameters
if: ${{ needs.variables.outputs.TAG == needs.variables.outputs.UNITY_VERSION }}
if: ${{ github.ref_type == 'tag' && needs.variables.outputs.TAG == needs.variables.outputs.UNITY_VERSION }}
needs: [ variables ]
runs-on: ubuntu-latest
steps:
Expand Down
127 changes: 127 additions & 0 deletions Assets/Plugins/WebGL/WebBridge/CommonCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// --------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using Supyrb.Attributes;
using UnityEngine;
Expand All @@ -21,6 +22,11 @@ namespace Supyrb
/// </summary>
public class CommonCommands : WebCommands
{
/// <summary>
/// List that stores allocated byte arrays to prevent them from being garbage collected
/// </summary>
private List<byte[]> byteArrayMemory = new List<byte[]>();

/// <summary>
/// Disable capturing all keyboard input, e.g. for using native html input fields
/// Browser Usage: <code>unityGame.SendMessage("WebGL","DisableCaptureAllKeyboardInput");</code>
Expand Down Expand Up @@ -58,6 +64,41 @@ public void LogMemory()
WebToolPlugins.LogMemory();
}

/// <summary>
/// Allocate memory to test memory usage and limits
/// The memory will be stored in a list to prevent it from being garbage collected
/// </summary>
/// <param name="mb">MB to allocate</param>
[WebCommand(Description = "Allocate memory to test memory usage and limits")]
public void AllocateByteArrayMemory(int mb)
{
byte[] memory = new byte[mb * 1024 * 1024];
byteArrayMemory.Add(memory);
Debug.Log($"Allocated {mb} MB of memory, total memory usage: {WebToolPlugins.GetTotalMemorySize():0.00}MB");
}

/// <summary>
/// Release all allocated byte array memory
/// </summary>
[WebCommand(Description = "Release all allocated byte array memory")]
[ContextMenu(nameof(ReleaseByteArrayMemory))]
public void ReleaseByteArrayMemory()
{
byteArrayMemory.Clear();
Debug.Log("Released all allocated byte array memory, it can now be garbage collected");
}

/// <summary>
/// Trigger garbage collection
/// </summary>
[WebCommand(Description = "Trigger garbage collection")]
[ContextMenu(nameof(TriggerGarbageCollection))]
public void TriggerGarbageCollection()
{
GC.Collect();
Debug.Log("Garbage collection triggered");
}

/// <summary>
/// Unloads all unused assets <see cref="Resources.UnloadUnusedAssets"/>
/// Browser Usage: <code>unityGame.SendMessage("WebGL","UnloadUnusedAssets");</code>
Expand All @@ -67,6 +108,7 @@ public void LogMemory()
public void UnloadUnusedAssets()
{
Resources.UnloadUnusedAssets();
Debug.Log("Unloaded unused assets");
}

/// <summary>
Expand All @@ -79,6 +121,7 @@ public void UnloadUnusedAssets()
public void SetApplicationRunInBackground(int runInBackground)
{
Application.runInBackground = runInBackground == 1;
Debug.Log($"Application.runInBackground: {Application.runInBackground}");
}

/// <summary>
Expand All @@ -90,6 +133,7 @@ public void SetApplicationRunInBackground(int runInBackground)
public void SetApplicationTargetFrameRate(int targetFrameRate)
{
Application.targetFrameRate = targetFrameRate;
Debug.Log($"Application.targetFrameRate: {Application.targetFrameRate}");
}

/// <summary>
Expand All @@ -101,6 +145,7 @@ public void SetApplicationTargetFrameRate(int targetFrameRate)
public void SetTimeFixedDeltaTime(float fixedDeltaTime)
{
Time.fixedDeltaTime = fixedDeltaTime;
Debug.Log($"Time.fixedDeltaTime: {Time.fixedDeltaTime}");
}

/// <summary>
Expand All @@ -113,6 +158,7 @@ public void SetTimeFixedDeltaTime(float fixedDeltaTime)
public void SetTimeTimeScale(float timeScale)
{
Time.timeScale = timeScale;
Debug.Log($"Time.timeScale: {Time.timeScale}");
}

/// <summary>
Expand Down Expand Up @@ -214,6 +260,7 @@ public void LogTextureSupport()
public void DeleteAllPlayerPrefs()
{
PlayerPrefs.DeleteAll();
Debug.Log("Deleted all player prefs");
}

/// <summary>
Expand All @@ -226,6 +273,86 @@ public void DeleteAllPlayerPrefs()
public void LogShaderCompilation(int enabled)
{
GraphicsSettings.logWhenShaderIsCompiled = enabled == 1;
Debug.Log($"GraphicsSettings.logWhenShaderIsCompiled: {GraphicsSettings.logWhenShaderIsCompiled}");
}

/// <summary>
/// Copy text to clipboard using the browser's clipboard API
/// </summary>
/// <param name="text">Text to copy to clipboard</param>
[WebCommand(Description = "Copy text to clipboard")]
public void CopyToClipboard(string text)
{
WebToolPlugins.CopyToClipboard(text);
}

/// <summary>
/// Check if the browser has an internet connection
/// </summary>
[WebCommand(Description = "Check if browser is online")]
public void CheckOnlineStatus()
{
bool isOnline = WebToolPlugins.IsOnline();
Debug.Log($"<color=#4D65A4>Online Status:</color> {(isOnline ? "<color=#3bb508>Connected</color>" : "<color=#b50808>Disconnected</color>")}");
}

/// <summary>
/// Captures the current screen and saves it as a PNG file.
/// </summary>
[WebCommand(Description = "Save current screen as PNG")]
public void SaveScreenshot()
{
SaveScreenshotSuperSize(1);
}

/// <summary>
/// Captures the current screen and saves it as a PNG file.
/// </summary>
/// <param name="superSize">1 for normal size, 2 for double size, 4 for quadruple size</param>
[WebCommand(Description = "Save current screen as PNG with variable super size")]
public void SaveScreenshotSuperSize(int superSize)
{
StartCoroutine(CaptureScreenshot(superSize));
}

private IEnumerator CaptureScreenshot(int superSize)
{
// Wait for the end of frame to ensure everything is rendered
yield return new WaitForEndOfFrame();

string filename = "screenshot.png";
try
{
// Capture the screen
Texture2D screenshot = ScreenCapture.CaptureScreenshotAsTexture(superSize);

try
{
// Convert to PNG
byte[] pngData = screenshot.EncodeToPNG();

// Download through browser
WebToolPlugins.DownloadBinaryFile(filename, pngData, "image/png");

Debug.Log($"Screenshot saved as {filename} ({screenshot.width}x{screenshot.height}) with size {pngData.Length} bytes");
}
finally
{
// Clean up the texture
if (Application.isPlaying)
{
Destroy(screenshot);
}
else
{
DestroyImmediate(screenshot);
}
}
}
catch (System.Exception e)
{
Debug.LogError($"Failed to save screenshot: {e.Message}");
}
}
}
}
16 changes: 14 additions & 2 deletions Assets/Plugins/WebGL/WebBridge/WebBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void Help()
{
sb.AppendLine($"<b>---{webCommand.GetType().Name}---</b>");
MethodInfo[] methods = webCommand.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance);
Array.Sort(methods, (a, b) => string.Compare(a.Name, b.Name));

for (int i = 0; i < methods.Length; i++)
{
Expand All @@ -123,11 +124,11 @@ public void Help()
var parameter = parameters[j];
if (parameter.ParameterType == typeof(string))
{
sb.Append($", \"{parameter.ParameterType} {parameter.Name}\"");
sb.Append($", \"{GetFriendlyTypeName(parameter.ParameterType)} {parameter.Name}\"");
}
else
{
sb.Append($", {parameter.ParameterType} {parameter.Name}");
sb.Append($", {GetFriendlyTypeName(parameter.ParameterType)} {parameter.Name}");
}
}

Expand All @@ -140,5 +141,16 @@ public void Help()
sb.AppendLine($"\nRun a command with 'runUnityCommand(\"COMMAND_NAME\",PARAMETER);'");
Debug.Log(sb.ToString());
}

private string GetFriendlyTypeName(Type type)
{
if (type == typeof(int)) return "int";
if (type == typeof(long)) return "long";
if (type == typeof(float)) return "float";
if (type == typeof(double)) return "double";
if (type == typeof(bool)) return "bool";
if (type == typeof(string)) return "string";
return type.Name;
}
}
}
Loading

0 comments on commit 36b6dd9

Please sign in to comment.