jeffreylanters / react-unity-webgl

React Unity WebGL provides a modern solution for embedding Unity WebGL builds in your React Application while providing advanced APIs for two way communication and interaction between Unity and React.

Home Page:https://react-unity-webgl.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to find Event Listener in Event System for Event

rounin-rp opened this issue · comments

Please avoid duplicates

Language and Compiler

Babel and WebPack JavaScript

What environment are you using?

Local Development Server

When does your problem occur?

While the Unity App is loading

What does your problem relate to?

I don't know

React-Unity-WebGL Version

9.3.0

React Version

18.2.0

Unity Version

2021.3.14f1

What happened?

My unity app throws an event GetUserData as soon the scene gets loaded but I am getting the following warning/error in my browser console:
Unable to find Event Listener in Event System for Event {eventName: 'GetUserData'}

The most interesting this is that the issue is not occurring all the time, some times its working and some times not.

Reproducible test case

No response

Would you be interested in contributing a fix?

  • yes, I would like to contribute a fix

I encountered the same problem running the example code on the docs. To fix it, we changed the code to

mergeInto(LibraryManager.library, {
  GameOver: function (userName, score) {
    const data = { userName: UTF8ToString(userName), score };
    const event = new CustomEvent("componentEvent", { detail: data });
    window.dispatchEvent(event);
  },
});

And on the react side:

const handleComponentEvent = ({ detail }: CustomEvent<{ userName: string, score: number }>) => {
  const { userName, score } = detail;
  console.log(userName, score)
};

useEffect(() => {
  window.addEventListener("componentEvent", handleComponentEvent as EventListener);
  return () => {
    window.removeEventListener("componentEvent", handleComponentEvent as EventListener);
  };
}, []);