-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamically usable event listeners
This is quite powerful, since it makes it very easy to listen to js events without writing a lot of code
- Loading branch information
1 parent
0d9b8c5
commit 8c3c786
Showing
12 changed files
with
311 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
Assets/Plugins/WebGL/WebTools/EventListeners/CommonWebEventListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="WebEventListener.cs"> | ||
// Copyright (c) 2024 Johannes Deml. All rights reserved. | ||
// </copyright> | ||
// <author> | ||
// Johannes Deml | ||
// public@deml.io | ||
// </author> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
using UnityEngine; | ||
|
||
namespace Supyrb | ||
{ | ||
public class CommonWebEventListener : MonoBehaviour | ||
{ | ||
private void Start() | ||
{ | ||
WebEventListeners.AddEventListener("focus", () => | ||
{ | ||
Debug.Log("WebEvent focus triggered"); | ||
}); | ||
|
||
WebEventListeners.AddEventListener("blur", () => | ||
{ | ||
Debug.Log("WebEvent blur triggered"); | ||
}); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Plugins/WebGL/WebTools/EventListeners/CommonWebEventListener.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
Assets/Plugins/WebGL/WebTools/EventListeners/WebEventListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="WebEventListener.cs"> | ||
// Copyright (c) 2024 Johannes Deml. All rights reserved. | ||
// </copyright> | ||
// <author> | ||
// Johannes Deml | ||
// public@deml.io | ||
// </author> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using UnityEngine; | ||
|
||
namespace Supyrb | ||
{ | ||
public class WebEventListener : MonoBehaviour | ||
{ | ||
public event Action OnEvent; | ||
|
||
// Called from JavaScript | ||
public void TriggerEvent() | ||
{ | ||
OnEvent?.Invoke(); | ||
} | ||
|
||
private void OnDestroy() | ||
{ | ||
// Clean up event handlers when destroyed | ||
OnEvent = null; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Plugins/WebGL/WebTools/EventListeners/WebEventListener.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
Assets/Plugins/WebGL/WebTools/EventListeners/WebEventListeners.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
// <copyright file="WebGlBridge.cs"> | ||
// Copyright (c) 2021 Johannes Deml. All rights reserved. | ||
// </copyright> | ||
// <author> | ||
// Johannes Deml | ||
// public@deml.io | ||
// </author> | ||
// -------------------------------------------------------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices; | ||
using UnityEngine; | ||
|
||
namespace Supyrb | ||
{ | ||
public class WebEventListeners : MonoBehaviour | ||
{ | ||
|
||
#if UNITY_WEBGL | ||
[DllImport("__Internal")] | ||
private static extern void _AddJsEventListener(string eventName); | ||
#endif | ||
private const string GameObjectName = "WebEventListeners"; | ||
|
||
private Dictionary<string, WebEventListener> eventListeners = new Dictionary<string, WebEventListener>(); | ||
private static WebEventListeners instance; | ||
#if UNITY_WEBGL | ||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | ||
private static void OnBeforeSceneLoadRuntimeMethod() | ||
{ | ||
GameObject instanceGo = new GameObject(GameObjectName); | ||
instance = instanceGo.AddComponent<WebEventListeners>(); | ||
DontDestroyOnLoad(instanceGo); | ||
} | ||
#endif | ||
|
||
public static void AddEventListener(string eventName, Action callback) | ||
{ | ||
instance.AddEventListenerInternal(eventName, callback); | ||
} | ||
|
||
private void AddEventListenerInternal(string eventName, Action callback) | ||
{ | ||
if(eventListeners.TryGetValue(eventName, out var eventListener)) | ||
{ | ||
eventListener.OnEvent += callback; | ||
} | ||
else | ||
{ | ||
var eventGo = new GameObject("WebEvent-" + eventName); | ||
eventGo.transform.parent = transform; | ||
var eventComponent = eventGo.AddComponent<WebEventListener>(); | ||
eventListeners[eventName] = eventComponent; | ||
eventComponent.OnEvent += callback; | ||
|
||
#if UNITY_WEBGL | ||
// Add event listener on javascript side | ||
_AddJsEventListener(eventName); | ||
#endif | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Plugins/WebGL/WebTools/EventListeners/WebEventListeners.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
Assets/Plugins/WebGL/WebTools/EventListeners/WebEventListeners.jslib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var WebGlEventListeners = | ||
{ | ||
_AddJsEventListener: function(eventNamePtr) { | ||
var eventName = UTF8ToString(eventNamePtr); | ||
|
||
// Add the event listener to the window object | ||
window.addEventListener(eventName, function(event) { | ||
if (typeof unityGame === 'undefined') { | ||
console.error('Unity instance "unityGame" not found - cannot send event ' + eventName); | ||
return; | ||
} | ||
unityGame.SendMessage("WebEvent-" + eventName, "TriggerEvent"); | ||
}); | ||
}, | ||
}; | ||
|
||
mergeInto(LibraryManager.library, WebGlEventListeners); |
32 changes: 32 additions & 0 deletions
32
Assets/Plugins/WebGL/WebTools/EventListeners/WebEventListeners.jslib.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters