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.09k stars 504 forks source link

Flutter 3.19 breaks Unitywidget on Android 12+ screen transition, resize, screen rotation or keyboard open. #929

Open timbotimbo opened 4 months ago

timbotimbo commented 4 months ago

Description

Just a warning about using Flutter 3.19 on Android 12+. The Unitywidget will break when a keyboard is openend or the screen is rotated.

It will end up spamming the following error:

E/BufferQueueProducer(17453): [ImageReader-1080x2000f22m4-17453-17](id:442d0000001b,api:1,p:1104,c:17453) dequeueBuffer: BufferQueue has been abandoned

You can only recover from this by fully removing the UnityWidget from the widget tree.

This does not happen on Android 11 and lower.

The Flutter 3.20 beta and master channel fix this bug, but that introduces another Unity bug.
Flutter now resizes the platform view and Unity can't seem to handle that. It will resize to the wrong size.

Workarounds

[UPDATE] Flutter 3.22 fixes the issue and can be used without workaround.

You've got 4 choices for now:

Video

The red color is a container behind the unitywidget, to show when it is transparent or invisible.
The 3.19 videos were recorded on an earlier beta, but you get the same visual result on 3.19 stable.

S20FE Keyboard open videos

1: 3.162: 3.193: master (3.20)
https://github.com/flutter/flutter/assets/11444698/05c30381-bfb0-49af-8122-d41d90de7746 https://github.com/flutter/flutter/assets/11444698/0abf4cbb-ca08-4a3c-9334-4c99dc035763 https://github.com/flutter/flutter/assets/11444698/92652386-80ad-4db6-89b1-d22f49cb25e3

Screen rotation videos

4: 3.165: 3.196: master (3.20)
https://github.com/flutter/flutter/assets/11444698/5fa5b83b-5ce3-4aa2-ae9d-8516b76ae70f https://github.com/flutter/flutter/assets/11444698/7e88f1fa-6744-4672-8bb9-bfb4fbd42229 https://github.com/flutter/flutter/assets/11444698/c9977f33-eaf8-4f34-8129-210a0ae40e8b

Versions

hdeppert commented 4 months ago

Is the third workaround "use useAndroidViewSurface: true" really a workaround?

According to this issue (and two more related ones) this mode introduces more issues than working around, right?

As of now, what is the best setup for using Flutter with Unity and the widget and avoid rendering or touch input issues? Probably Flutter 3.13.9 + Unity with old input system ?

timbotimbo commented 4 months ago

[UPDATE] the upsides list below is outdated as of Flutter 3.22, in which the visibility and rotation issues are fixed when using useAndroidViewSurface: false.

Regarding useAndroidViewSurface.

Downsides:

Upsides:

With these new issues popping up, the downsides are getting more and more acceptable.

I still prefer to stick with <= 3.13.9.
But my app does contain text instructions behind the UnityWidget on how to reset it, in case it turns invisible.

TonyHoyle commented 4 months ago

Glad I found this.. I was trying to work out why unity wasn't working at all on android 12+. Not just on resize - at all.. Downgrading flutter just not an option for us, we have functionality that relies on 3.19

Setting useAndroidViewSurface true fixed it and as far as I can see we haven't hit any downsides, it works just the same as before.

timbotimbo commented 1 month ago

Fixes for this are included in Flutter 3.22

hdeppert commented 1 month ago

Fixes for this are included in Flutter 3.22

Thanks for the update! Do you have any update regarding your previous comment and recommendation on which version/setup should be used with which up-/downsides?

timbotimbo commented 3 weeks ago

@hdeppert Flutter is actively trying to move away from Hybrid Composition (useAndroidViewSurface: true), and only keeping it around for some edge cases. I would recommend avoiding it unless you actually run into a bug that actually needs it.

Most upsides listed above are now negated in flutter 3.22. Screen rotations, resizes and visibility issues all seem to be fixed. Although the resize doesn't look as smooth as when using Hybrid composition.

Handling touch with the new input system is now the main difference, but it can be made to work in Virtual display too https://github.com/juicycleff/flutter-unity-view-widget/pull/938.