Open AkhilRaja opened 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.
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 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 ?
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 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
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 @Ahmadre Yeah, this issue is still on iOS. On android everything works well. Also, after updating this code problem still exist.
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.
Same issue with newest release on flutter master
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:
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