Skip to content

Commit f773bfd

Browse files
committed
Merge branch 'master' of https://github.com/JohannesDeml/UnityWebGL-LoadingTest into 6000.0
2 parents 1a07693 + 515a296 commit f773bfd

15 files changed

+354
-27
lines changed

.github/workflows/upgrade-unity.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ on:
3232

3333
jobs:
3434
upgrade-unity-version:
35-
name: Upgrade Unity version and packages
35+
name: Upgrade Unity version and packages to ${{ inputs.unityVersion }}
3636
runs-on: ubuntu-latest
3737
strategy:
3838
fail-fast: false
@@ -106,6 +106,13 @@ jobs:
106106
run: |
107107
LAST_UNITY_VERSION=$(sed -n 's/^\m_EditorVersion: //p'< ./ProjectSettings/ProjectVersion.txt)
108108
echo "VERSION=$LAST_UNITY_VERSION" >> $GITHUB_OUTPUT
109+
BRANCH_NAME=${GITHUB_REF#refs/heads/}
110+
if [[ "$BRANCH_NAME" == *"urp"* ]]
111+
then
112+
echo "NAME=$LAST_UNITY_VERSION-urp" >> $GITHUB_OUTPUT
113+
else
114+
echo "NAME=$LAST_UNITY_VERSION" >> $GITHUB_OUTPUT
115+
fi
109116
110117
- name: Set upgrade name
111118
id: upgrade_name
@@ -123,6 +130,7 @@ jobs:
123130
- name: Log variables
124131
run: |
125132
echo "last_unity_version -> ${{ steps.last_unity_version.outputs.VERSION }}"
133+
echo "last_unity_name -> ${{ steps.last_unity_version.outputs.NAME }}"
126134
echo "upgrade_name -> ${{ steps.upgrade_name.outputs.NAME }}"
127135
128136
- name: Build project
@@ -159,10 +167,10 @@ jobs:
159167
uses: peter-evans/create-pull-request@v4
160168
with:
161169
token: ${{ secrets.PR_GITHUB_TOKEN }}
162-
commit-message: "[Automated workflow] upgrade-unity from ${{steps.last_unity_version.outputs.VERSION}} to ${{ inputs.unityVersion }}"
163-
branch: "ci/upgrade-unity/from-${{steps.last_unity_version.outputs.VERSION}}-to-${{ steps.upgrade_name.outputs.NAME }}"
170+
commit-message: "[Automated workflow] upgrade-unity from ${{steps.last_unity_version.outputs.NAME}} to ${{ inputs.unityVersion }}"
171+
branch: "ci/upgrade-unity/from-${{steps.last_unity_version.outputs.NAME}}-to-${{ steps.upgrade_name.outputs.NAME }}"
164172
delete-branch: true
165-
title: "[Automated workflow] upgrade-unity from ${{steps.last_unity_version.outputs.VERSION}} to ${{ steps.upgrade_name.outputs.NAME }}"
173+
title: "[Automated workflow] upgrade-unity from ${{steps.last_unity_version.outputs.NAME}} to ${{ steps.upgrade_name.outputs.NAME }}"
166174
body: ${{ steps.template.outputs.result }}
167175

168176
- name: Add tags

Assets/Plugins/WebGL/WebBridge/CommonCommands.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using System;
1212
using System.Collections;
1313
using System.Collections.Generic;
14+
using System.Linq;
15+
using System.Text;
1416
using Supyrb.Attributes;
1517
using UnityEngine;
1618
using UnityEngine.Rendering;
@@ -161,6 +163,47 @@ public void SetTimeTimeScale(float timeScale)
161163
Debug.Log($"Time.timeScale: {Time.timeScale}");
162164
}
163165

166+
/// <summary>
167+
/// Finds GameObject(s) by name and logs the found GameObject(s) and their components
168+
/// </summary>
169+
/// <param name="name">The name of the GameObject to find</param>
170+
[WebCommand(Description = "Find GameObject by name and log its components")]
171+
[ContextMenu(nameof(FindGameObjectByName))]
172+
public void FindGameObjectByName(string name)
173+
{
174+
var gameObjects = GameObject.FindObjectsOfType<GameObject>().Where(go => go.name == name).ToArray();
175+
if (gameObjects.Length == 0)
176+
{
177+
Debug.Log($"No GameObject found with the name: {name}");
178+
}
179+
else
180+
{
181+
StringBuilder sb = new StringBuilder();
182+
foreach (var go in gameObjects)
183+
{
184+
int pathStartIndex = 0;
185+
var currentTransform = go.transform;
186+
sb.Insert(pathStartIndex, currentTransform.name);
187+
currentTransform = currentTransform.parent;
188+
while (currentTransform != null)
189+
{
190+
sb.Insert(pathStartIndex, currentTransform.name + "/");
191+
currentTransform = currentTransform.parent;
192+
}
193+
194+
sb.AppendLine($", Tag: {go.tag}, Layer: {go.layer}, ActiveSelf: {go.activeSelf}, ActiveInHierarchy: {go.activeInHierarchy}");
195+
sb.AppendLine("Attached Components:");
196+
var components = go.GetComponents<Component>();
197+
foreach (var component in components)
198+
{
199+
sb.AppendLine($"- {component.GetType().Name}");
200+
}
201+
Debug.Log(sb.ToString());
202+
sb.Clear();
203+
}
204+
}
205+
}
206+
164207
/// <summary>
165208
/// Toggle the visibility of the info panel in the top right corner
166209
/// Browser Usage: <code>unityGame.SendMessage("WebGL", "ToggleInfoPanel");</code>

Assets/Plugins/WebGL/WebBridge/WebBridge.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,23 @@
1818

1919
namespace Supyrb
2020
{
21+
2122
/// <summary>
2223
/// Bridge to Unity to access unity logic through the browser console
2324
/// You can extend your commands by creating a partial class for WebBridge, see WebBridge.Commands as an example
2425
/// </summary>
2526
public class WebBridge : WebCommands
2627
{
27-
private const string WebBridgeGameObjectName = "WebBridge";
28+
private const string GameObjectName = "WebBridge";
2829

29-
private static GameObject bridgeInstance;
30+
private static GameObject instance;
3031
#if UNITY_WEBGL
3132
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
3233
private static void OnBeforeSceneLoadRuntimeMethod()
3334
{
3435
SetGlobalVariables();
35-
bridgeInstance = new GameObject(WebBridgeGameObjectName);
36-
DontDestroyOnLoad(bridgeInstance);
36+
instance = new GameObject(GameObjectName);
37+
DontDestroyOnLoad(instance);
3738
AddAllWebCommands();
3839
}
3940
#endif
@@ -58,7 +59,7 @@ private static void AddAllWebCommands()
5859
webCommandTypes.Sort((a, b) => a.Name.CompareTo(b.Name));
5960
foreach (var webCommandType in webCommandTypes)
6061
{
61-
bridgeInstance.AddComponent(webCommandType);
62+
instance.AddComponent(webCommandType);
6263
}
6364
}
6465

Assets/Plugins/WebGL/WebTools/EventListeners.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="WebEventListener.cs">
3+
// Copyright (c) 2024 Johannes Deml. All rights reserved.
4+
// </copyright>
5+
// <author>
6+
// Johannes Deml
7+
// public@deml.io
8+
// </author>
9+
// --------------------------------------------------------------------------------------------------------------------
10+
11+
using UnityEngine;
12+
13+
namespace Supyrb
14+
{
15+
public class CommonWebEventListener : MonoBehaviour
16+
{
17+
private void Start()
18+
{
19+
WebEventListeners.AddEventListener("focus", () =>
20+
{
21+
Debug.Log("WebEvent focus triggered");
22+
});
23+
24+
WebEventListeners.AddEventListener("blur", () =>
25+
{
26+
Debug.Log("WebEvent blur triggered");
27+
});
28+
}
29+
}
30+
}

Assets/Plugins/WebGL/WebTools/EventListeners/CommonWebEventListener.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="WebEventListener.cs">
3+
// Copyright (c) 2024 Johannes Deml. All rights reserved.
4+
// </copyright>
5+
// <author>
6+
// Johannes Deml
7+
// public@deml.io
8+
// </author>
9+
// --------------------------------------------------------------------------------------------------------------------
10+
11+
using System;
12+
using UnityEngine;
13+
14+
namespace Supyrb
15+
{
16+
public class WebEventListener : MonoBehaviour
17+
{
18+
public event Action OnEvent;
19+
20+
// Called from JavaScript
21+
public void TriggerEvent()
22+
{
23+
OnEvent?.Invoke();
24+
}
25+
26+
private void OnDestroy()
27+
{
28+
// Clean up event handlers when destroyed
29+
OnEvent = null;
30+
}
31+
}
32+
}

Assets/Plugins/WebGL/WebTools/EventListeners/WebEventListener.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="WebGlBridge.cs">
3+
// Copyright (c) 2021 Johannes Deml. All rights reserved.
4+
// </copyright>
5+
// <author>
6+
// Johannes Deml
7+
// public@deml.io
8+
// </author>
9+
// --------------------------------------------------------------------------------------------------------------------
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Runtime.InteropServices;
14+
using UnityEngine;
15+
16+
namespace Supyrb
17+
{
18+
public class WebEventListeners : MonoBehaviour
19+
{
20+
21+
#if UNITY_WEBGL && !UNITY_EDITOR
22+
[DllImport("__Internal")]
23+
private static extern void _AddJsEventListener(string eventName);
24+
#endif
25+
private const string GameObjectName = "WebEventListeners";
26+
27+
private Dictionary<string, WebEventListener> eventListeners = new Dictionary<string, WebEventListener>();
28+
private static WebEventListeners instance;
29+
#if UNITY_WEBGL
30+
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
31+
private static void OnBeforeSceneLoadRuntimeMethod()
32+
{
33+
GameObject instanceGo = new GameObject(GameObjectName);
34+
instance = instanceGo.AddComponent<WebEventListeners>();
35+
DontDestroyOnLoad(instanceGo);
36+
}
37+
#endif
38+
39+
public static void AddEventListener(string eventName, Action callback)
40+
{
41+
instance.AddEventListenerInternal(eventName, callback);
42+
}
43+
44+
private void AddEventListenerInternal(string eventName, Action callback)
45+
{
46+
if(eventListeners.TryGetValue(eventName, out var eventListener))
47+
{
48+
eventListener.OnEvent += callback;
49+
}
50+
else
51+
{
52+
var eventGo = new GameObject("WebEvent-" + eventName);
53+
eventGo.transform.parent = transform;
54+
var eventComponent = eventGo.AddComponent<WebEventListener>();
55+
eventListeners[eventName] = eventComponent;
56+
eventComponent.OnEvent += callback;
57+
58+
#if UNITY_WEBGL && !UNITY_EDITOR
59+
// Add event listener on javascript side
60+
_AddJsEventListener(eventName);
61+
#elif UNITY_EDITOR && WEBTOOLS_LOG_CALLS
62+
Debug.Log($"<color=#00CCCC>{nameof(WebEventListeners)}.{nameof(AddEventListenerInternal)} add callback for {eventName}</color>");
63+
#endif
64+
}
65+
}
66+
}
67+
}

Assets/Plugins/WebGL/WebTools/EventListeners/WebEventListeners.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)