asmadsen / react-native-unity-view

MIT License
203 stars 129 forks source link

onUnityMessage/onMessage stops receiving messages dispatched by Unity after Metro refresh #41

Open dave-vazquez opened 3 years ago

dave-vazquez commented 3 years ago

@asmadsen

I'm running into an issue where when I refresh the Metro server, UnityView's onUnityMessage and onMessage handlers stop receiving messages dispatched by Unity. Issue is occurring with iOS build to physical device.

I've tried adding event listeners manually, but still a no-go.

The message is being dispatched by Unity with the following implementation:

public class MenuManager : MonoBehaviour
{
    public Button SignUpButton;
    public Button LoginButton;

    void Awake()
    {
        SignUpButton.onClick.AddListener(routeToSignUp);
        LoginButton.onClick.AddListener(routeToLogin);
    }
    void routeToSignUp() {
      Debug.Log("signup button clicked");
      UnityMessageManager.Instance.SendMessageToRN("SignUp");
    }

    void routeToLogin() {
      Debug.Log("login button clicked");
      UnityMessageManager.Instance.SendMessageToRN("Login");
    }
}
const UnityScreen = () => {
  const onMessage = (handler) => {
    console.log(handler);
  };

  return <UnityView style={s.unity} onMessage={onMessage} />;
};

However, after Metro is refreshed, I can still see the Debug.Log's output to Xcode which tells me that SendMessageToRN should be invoking but React Native is unable to receive the messages after a single refresh.

dave-vazquez commented 3 years ago

For anyone running into the same issue, a work-around for this is not to refresh using Metro, but simply closing and re-opening the application on the device. It's the equivalent of refreshing the Metro server.

shhhiiiii commented 3 years ago

Hi, Im experiencing nearly the same situation but for my side i dont want to close the app since its not necessary.

The scenario is : First open of unity view is perfectly working. then I used Application.Unload(); to unload unity and navigate to other react native screen, When navigate again to unity view, Unity will reload just fine and will work properly,

But the problem is that messaging is distorted. Unity still can send message and will be receive in react native side, But for react native part when attempt to send message to unity view. Unity will not be able to receive the message.

Im suspecting that react native still trying to send message from the previous view that has been unload.

Does anyone know how to fix this problem?

HELP PLEASE!!