question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Event handler for Unity Events is global

See original GitHub issue

Describe the bug In our application we have two Unity WebGL components, out of which max one is visible at a time. Both components share a code base, e.g. they both send the UnityEventDispatch Unity Event to pass events to the React side. After calling

this.unityContent.on('UnityEventDispatch', this.unityEventDispatch)

in the instance constructor only one of the event handlers is ever called.

To Reproduce

ReactUnityWebGL.jslib:

mergeInto(LibraryManager.library, {
  ...
  UnityEventDispatch: function(json) {
    ReactUnityWebGL.UnityEventDispatch(Pointer_stringify(json));
  },
  ...
});

WebGLBridge.cs:

...
using UnityEngine;
using System.Runtime.InteropServices;
...

public static class WebGLBridge
{
  // These are defined as JS plugins at Assets/Plugin/ReactUnityWebGL.jslib
  ...
  [DllImport("__Internal")]
  private static extern void UnityEventDispatch(string json);
  ...
  public static void SendExternalEvent(string eventName, ...)
  {
    var data = ... // create some object that .ToString() converts to JSON string
    ...
    string json = data.ToString();
    UnityEventDispatch(json);
  }
}

UnityWrapper.js (React side)

export default class UnityWrapper extends Component {
  ...
  constructor(props) {
    ...
    this.unityContent = new UnityContent(this.configPath, this.unityLoader);
    ...
    this.unityContent.on('UnityEventDispatch', this.unityEventDispatch);
    ...
  }
  ...
  unityEventDispatch = data => {
    // parse JSON, forward event to callback and so on...
  };
  ...
}

When Unity Component 1 calls WebGLBridge.SendExternalEvent() then the event handler on the UnityContent instance for the other component is called. I didn’t verify this, but I think that this depends on the order of instance creation, i.e. the last created UnityContent will override the event handler for the same event of a previous UnityContent instance.

Expected behavior When Unity Component 1 calls WebGLBridge.SendExternalEvent() then the event handler for its UnityContent instance will be called.

Desktop

  • OS: Linux
  • Browser: Chromium
  • Version: 84
  • JSON from component build output: unityVersion: "2019.3.5f1"
  • package.json: "react-unity-webgl": "^7.1.10",

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
stefanb2commented, Aug 14, 2020

The proposed changes fix the issue in our project, i.e. if you have multiple UnityContent instances that register an event handler for the same event name then all event handlers will be called.

0reactions
github-actions[bot]commented, Jul 19, 2021

This issue was closed because it has been inactive for 30 days since being marked as stale.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UnityEvents - Unity - Manual
Using UnityEvents · Make sure your script imports/uses UnityEngine.Events . · Select the + icon to add a slot for a callback ·...
Read more >
Global event handler - Unity Forum
My understanding is that I can modify the StandaloneInputModule script to fire all events twice, once on the current gameObject and once on...
Read more >
Events & Delegates in Unity - Game Dev Beginner
The Game Event Listener is an observer. It's not a scriptable object, it's a regular MonoBehaviour, but does hold a reference to a...
Read more >
Should I use a global "Event Handler" script for C# ... - Reddit
No. Unity event stuff is only needed when you you wanna serialization or some tinkering with existing unity classes insides. Otherwise events as ......
Read more >
How To Build An Event System in Unity - YouTube
In this video we take a look at how to build a Custom Event System in Unity. We look at why a Custom...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found