juicycleff / flutter-unity-view-widget

Embeddable unity game engine view for Flutter. Advance demo here https://github.com/juicycleff/flutter-unity-arkit-demo
BSD 3-Clause "New" or "Revised" License
2.15k stars 525 forks source link

Unity screen turning white on scene load (iOS) #578

Open AkhilRaja opened 2 years ago

AkhilRaja commented 2 years ago

Describe the bug

Unity Scene is loaded and the scripts are all running in the background, but just for the first launch the screen is white until it is unloaded and loaded.

To Reproduce Steps to reproduce the behavior:

  1. Load the unity module for the first time for an AR scene
  2. You will see a white screen instead of the camera opening up.

Unity (please complete the following information):

Smartphone (please complete the following information):

@juicycleff I see the issue is fixed for android, can you please take a look for iOS as well. Thanks in advance

AkhilRaja commented 2 years ago

@juicycleff also to add, it happens on the first time of every new app session. From the second time onward it works well.

juicycleff commented 2 years ago

Will take a look at it, just for now load unity widget in your splash screen widget if you have one with opacity set to 0 until I get back to you on this issue @AkhilRaja

AkhilRaja commented 2 years ago

@juicycleff Thankyou for checking, no I don't have a splash screen. It's the default made with unity screen that's causing the trouble I believe.

Let me know when you get a chance to take a look.

Do you think the problem will be solved if I get a unity license and remove the splash screen ?

Anishishi commented 2 years ago

There seems to be a problem with the flutter-unity-widget package itself. Fixing the swift file as described in this comment could solve the problem. https://github.com/juicycleff/flutter-unity-view-widget/issues/540#issuecomment-1063408129

AkhilRaja commented 2 years ago

@Anishishi I can confirm, that comment fixes it. I think the completed(rootview) is not geting called from the main UI thread. Just adding that fixes it. IMO the unity warming up logic isnt working well and needs to be refactored in the plugin @juicycleff

juicycleff commented 2 years ago

I agree, will be a small fix, working on windows atm, for now just unload unity in a splash screen, while I wrap up windows support

dawiddszewczyk commented 2 years ago

@juicycleff @Ahmadre Yeah, this issue is still on iOS. On android everything works well. Also, after updating this code problem still exist.

behnamsattar commented 2 years ago

I was able to fix this by adding a manual unload and reload. Here is the code I'm using to solve this:

UnityWidgetController? _unityWidgetController;
  Timer? timer;
  bool _isCheckingLoadState = false;
  bool _hasBeenRestarted = false;

  void _onUnityCreated(UnityWidgetController controller) async {
    _unityWidgetController = controller;
    // Due to an issue with the Unity plugin when it sometimes fails to load the
    // Unity scene, we perform a reload after the first unity scene creation. It
    // ensures that (1) the scene is loaded correctly and (2) the scene restarts
    // after a flutter restart during debugging.
    if (!_hasBeenRestarted) {
      _hasBeenRestarted = true;
      await _reloadUnityScene();
    }
  }

  Future<void> _reloadUnityScene() async {
    await _unityWidgetController?.unload();
    timer = Timer.periodic(
      const Duration(milliseconds: 500),
      (_) async {
        if (!_isCheckingLoadState) {
          _isCheckingLoadState = true;
          final isLoaded = await _unityWidgetController?.isLoaded() ?? false;
          if (!isLoaded) {
            timer?.cancel();
            await _unityWidgetController?.create();
          }
          _isCheckingLoadState = false;
        }
      },
    );
  }

Please note that, at the moment, I'm getting an exception when calling the create method. I have filed an issue regarding this, which I hope to get resolved soon. The fix works as expected, though.

kamami commented 1 year ago

Same issue with newest release on flutter master