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

Unload on Android not working #452

Open
roberto-donext opened this issue Aug 23, 2021 · 1 comment
Open

Unload on Android not working #452

roberto-donext opened this issue Aug 23, 2021 · 1 comment

Comments

@roberto-donext
Copy link

I really need help because I have been about 7 days trying to make this works, but is not possible.
This is the scenario:
1.- I have one scene in unity with a simple button. This button try to emulate that I want to close the Unity View on Flutter.
2.-On Unity, I have a Floatting Action Button. This button try to emulate that I want to reload te Unity view Again.
So, I launch the App, the scene is loaded. I click the Unity button, and the Unity View disapears. Ok. And then, if I click the Flutter button, I want the Unity View appear again. But this doesn´t work. THe Unity View appear but frozen. Not working any more.
I need to understand what is the way to deal with this situations. I want to unload Unity view, navigate from Flutter to another view, and then, when the user pop to the first view, launch again Unity View... but is not possible.
This is my approach. I have to say, that this code, works as expected on iOS.
The idea for me was works with a Provider. I use a flag to know if Unity View is loaded or not.
This is the provider:

class UnityViewController extends ChangeNotifier {

  bool _unityLoaded = true;
  bool get unityLoaded => this._unityLoaded;

  set unityLoaded(bool state) {
    _unityLoaded = state;
    notifyListeners();
  }
}

In Unity, when the user click the button to unload, I figure out that on iOS I can use Application.Unload(), but looks that this don´t work on Android. So I use on that script a pre compiler flag just fon send this command to iOS. So, from Unity i have this script added to a button:

   void go ()
    {
        UnityMessageManager messager = GetComponent<UnityMessageManager>();
        messager.SendMessageToFlutter("unloadnow");

        #if UNITY_IOS && !UNITY_EDITOR
        Application.Unload();
        #endif
    }

When the user click the button, the message unloadnow is send.

In Flutter, what I´m doing is catch this message, and call controller.unload() and then change the state of the view with de provider. Something like this:

  void onUnityMessage(String message, BuildContext context) async {
    

    if (message == "unloadnow") {
      this._unityWidgetController?.unload();
      final providerController =
          Provider.of<UnityViewController>(context, listen: false);
      providerController.unityLoaded = false;
    }
}

And of course, when the state is updated, I have this :


class MapBody extends StatelessWidget {
  const MapBody({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final unityLoaded = Provider.of<UnityViewController>(context).unityLoaded;
    return Stack(
      children: [
        Container(
          color: Colors.black,
        ),
        if (unityLoaded) UnityViewWidget()
      ],
    );
  }
}

So, I build again all this widget, and if the flag is true, I build again the UnityViewWidget, and if not, I only paint a container.
And in the scaffold I have a button for change again the state:

   floatingActionButton: FloatingActionButton(
              onPressed: () {
           
                Provider.of<UnityViewController>(newContext, listen: false)
                    .unityLoaded = true;
              },

But this doesn´t work. On iOS works perfectly, the only difference is that I calling the unload method from Unity and not from Flutter.
Maybe there are another way to archive this?. I do not understand the way to load and unload the unity view... thanks in advance!

@deepatavy
Copy link

Do we have any updates on this? Not able to restart The Unity for Android, It stays at old screen. Working fine with iOS

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

2 participants