flutter-unity-view-widget icon indicating copy to clipboard operation
flutter-unity-view-widget copied to clipboard

Unity screen turning white on scene load (iOS)

Open AkhilRaja opened this issue 2 years ago • 7 comments

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):

  • OS: iOS
  • Version [e.g. 2022.1.01f]

Smartphone (please complete the following information):

  • Device: iPhone 13 mini

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

AkhilRaja avatar May 11 '22 10:05 AkhilRaja

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

AkhilRaja avatar May 11 '22 11:05 AkhilRaja

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

juicycleff avatar May 11 '22 13:05 juicycleff

@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 ?

AkhilRaja avatar May 11 '22 17:05 AkhilRaja

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

Anishishi avatar May 12 '22 10:05 Anishishi

@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

AkhilRaja avatar May 13 '22 11:05 AkhilRaja

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

juicycleff avatar May 13 '22 12:05 juicycleff

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

dawiddszewczyk avatar Jul 18 '22 09:07 dawiddszewczyk

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.

behnamsattar avatar Nov 04 '22 19:11 behnamsattar

Same issue with newest release on flutter master

kamami avatar Jan 10 '23 16:01 kamami