asmadsen / react-native-unity-view

MIT License
203 stars 129 forks source link

Screen freezes when opening UnityView two times #75

Open Arnaod opened 2 years ago

Arnaod commented 2 years ago

Hi everyone,

I'm getting an head scratching bug for a few days.

My react-native app uses the unityView at several places. I can open it at every place without issues, but when I'm trying to open it two times in a row (either at the same place or different places), the screen freezes.

The screen displays the last frame that unity rendered before closing Unity.

The strangest thing is that the unityView is still running : I can ear the sounds and even click on buttons (if I remember where they are 😄)

The issue looks similar to open issues in the former module like this one for instance .

The former fix seemed to be this, but doesn't work here.

<UnityView style={{ position: 'absolute', left: 0, right: 0, top: 1, bottom: 1 }} />

Relevant code for the modal on RN side :

`class UnityModalComponent extends Component { constructor(props) { super(props); const { route } = props; this.state = { renderUnity: true, currentScene: route.params?.data?.scene, }; this.shouldWait = false; }

componentDidMount() { const { currentScene } = this.state; StatusBar.setHidden(true); UnityModule.isReady().then(ready => { if (ready) { this.shouldWait = false; UnityModule.postMessageToUnityManager(currentScene); } else { this.shouldWait = true; } }); }

componentDidUpdate(prevProps, prevState) { const { renderUnity } = this.state; if (renderUnity !== prevState.renderUnity && !renderUnity) { const { navigation } = this.props; navigation.pop(); } }

componentWillUnmount() { StatusBar.setHidden(false); }

Did anyone encounter the issue or has any idea on how to fix this?

Thank you,

IbrahimaWane commented 2 years ago

I encountered the same issue when I was building on Android and I had to change the source code. The issue happens when the window is being detached and reattached. To solve it go to node_modules/@admadsen/android/src/main/java/no/asmadsen/unity/view and open UnityUtils.java and add this method:

public static void removeUnityView(){
        if (unityPlayer == null) {
            return;
        }
        final Activity activity = ((Activity) unityPlayer.getContext());
        activity.startActivity(activity.getIntent());
    }

Now open UnityView.java and call it here:

@Override
    protected void onDetachedFromWindow() {
        UnityUtils.removeUnityView();
        super.onDetachedFromWindow();
    }
Arnaod commented 2 years ago

It really seemed like it could work for us @IbrahimaWane but sadly this does not fix the issue, thanks anyway. @NeelSteampunkDigital for us the issue is a little different: we don't have this message in the react native console, also even if we try to load a new scene we still have the issue

IbrahimaWane commented 2 years ago

It's unfortunate because the issue you described is exactly the same one I encountered. Try to use Android Studio Logcat to see the error message. Because mine also was not shown in the react native console. Good luck

IbrahimaWane commented 2 years ago

I forgot to mention, try to call UnityModule.createUnity() before rendering UnityView

Arnaod commented 2 years ago

Thank you @IbrahimaWane, unfortunately it didn't work on my issue.

I just realised that when the screen is black, if I lock the phone and switch back to the app, it works. Something to dig there probably.

Arnaod commented 2 years ago

Hey @asmadsen,

We're really stuck on this issue for quite some time and can't seem to figure it out. Would you be open to some freelancing to help us dig into the issue ? Thanks and happy new year !

sael-you commented 7 months ago

Hey @Arnaod it have exactly same issue on android 13+, have you been able to solve the issue ? i know it's been a while, but a yes or no might just gi ve me hope if you don't remember the how you fixed it if you fixed it.