Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I restart after shutting down Unity? #400

Closed
AuMaHub opened this issue May 7, 2021 · 11 comments
Closed

How do I restart after shutting down Unity? #400

AuMaHub opened this issue May 7, 2021 · 11 comments

Comments

@AuMaHub
Copy link

AuMaHub commented May 7, 2021

Describe the bug
When I use quit() on Android, the app quits. And when I use unload() the screen freezes when recreating Unity. Am I misunderstanding the function?

Log when the app is terminated with quit()

D/et.likersneten(19933): PlayerBase::stop() from IPlayer
D/AudioTrack(19933): stop(3385): called with 2632896 frames delivered
W/libEGL  (19933): EGLNativeWindowType 0x72fa66eee0 disconnect failed
Lost connection to device.
Exited (sigterm)

To Reproduce
Steps to reproduce the behavior:

  1. Access the Unity page from the app
  2. Page exit (use quit before dispose)
  3. error

Unity (please complete the following information):

  • OS: windows
  • Version 20.3.4f1

Smartphone (please complete the following information):

  • Device: Galaxy S10e
  • OS: Android 11
  • Version Android API 30

Additional context
Add any other context about the problem here.

@AuMaHub
Copy link
Author

AuMaHub commented Jun 7, 2021

I haven't been able to solve this problem for a month.

So I made a GIF of the app I ran as an example.
Please, I just want you to check it out.
ezgif com-gif-maker (1)

@AlceoMazza
Copy link

AlceoMazza commented Jun 7, 2021

I'm having the same issue in debug. The Unity Widget works fine when testing with ADB, but otherwise it freezes after exiting and re-opening the screen that contains it. To me this is happening with or without calling unload().

Edit: not when testing with ADB, but works fine only the first time the app is installed on the device: if I uninstall and reinstall the app the Unity Widget works at first launch and freezes on next launches.
Also tested in release and it gives the same problem.

@nikiforosper
Copy link

nikiforosper commented Jun 14, 2021

I fixed this issue with this way:

  1. Wrap Scaffold with
WillPopScope(
      onWillPop: () async {
        await _unityWidgetController.pause();
        Navigator.of(context).pop();
        return false;
      }, child: Scaffold 
...etc, to pause the unity
  1. In the onUnityCreated, add this:
void onUnityCreated(UnityWidgetController controller) async {
    this._unityWidgetController = controller;
    final _isPaused = await this._unityWidgetController.isPaused();
    if(_isPaused){
    Future.delayed(
      Duration(milliseconds: 50),
      () async {
        await this._unityWidgetController.resume();
      },
    );
   }
  }

@AlceoMazza
Copy link

Great, thanks! This works for me.
Althought I think this is not an expected behaviour, I have a project with Unity 2019.4.3f1 and Vuforia (vs the bugged one with Unity 2020.3.11f1 and ARCore) where this freeze is not happening and the Unity Widget works fine and this extra code is not needed.

@qiuguian
Copy link

it's not work for me with unity 2020.3.11

@nikiforospertselis
Copy link

it's not work for me with unity 2020.3.11

Another step I followed is to removeView in tha native code.
Specifically, go to
C:\YOUR_FLUTTER_FOLDER.pub-cache\hosted\pub.dartlang.org\flutter_unity_widget-.2.0\android\src\main\kotlin\com\xraph\plugin\flutter_unity_widget\UnityPlayerUtils.kt
and change the following functions to:

fun addUnityViewToBackground(activity: Activity) {
    if (unityPlayer == null) {
        return
    }
    if (unityPlayer!!.parent != null) {
        (unityPlayer!!.parent as ViewGroup).removeView(unityPlaye
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        unityPlayer!!.z = -1f
    }
    val layoutParams = ViewGroup.LayoutParams(1, 1)
    activity.addContentView(unityPlayer, layoutParams)
    isUnityInBackground = true
}
fun restoreUnityViewFromBackground(activity: Activity) {
            if (unityPlayer == null) {
                return
            }

            if (unityPlayer!!.parent != null) {
                (unityPlayer!!.parent as ViewGroup).removeView(unityPlayer);
                (unityPlayer!!.parent as ViewGroup).addView(unityPlayer)
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                unityPlayer!!.z = 1f
            }

            val layoutParams = ViewGroup.LayoutParams(1, 1)
            activity.addContentView(unityPlayer, layoutParams)
            isUnityInBackground = false
        }
fun addUnityViewToGroup(group: ViewGroup) {
            if (unityPlayer == null) {
                return
            }

            if (unityPlayer!!.parent != null) {
                (unityPlayer!!.parent as ViewGroup).removeView(unityPlayer)
            }

            val layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)
            group.addView(unityPlayer, 0, layoutParams)

            unityPlayer!!.windowFocusChanged(true)
            unityPlayer!!.requestFocus()
            unityPlayer!!.resume()
        }

I don't know if it will work, but you can try

@smashmountainstudio
Copy link

smashmountainstudio commented Oct 14, 2021

The code below work for me:

void _onUnityCreated(controller) async  {
    _unityWidgetController = controller;

    await _unityWidgetController.pause();

    Future.delayed(
      Duration(milliseconds: 100),
          () async {
        await this._unityWidgetController.resume();
      },
    );
  }

@smichalak
Copy link

Cheers mate @smashmountainstudio!

@muck27
Copy link

muck27 commented Jan 27, 2022

My game scene does not render the second time I try to play it.
I tried adding a WillPopScope as mentioned in the comment. The scene renders successfully the second time sometimes but most of the times it doesnt. Is there a fix for this issue yet?

@OGmetamonkey
Copy link

Cheers @smashmountainstudio :)

This is the only solution that worked for FUW 4.1.0 for me. Also tried all solutions from #46, #230 #245, #452, #413

@deepatavy
Copy link

Upgrading to ^2022.1.0 from ^4.2.1 Worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants