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

There's no way to release Unity resources

Open Zazo032 opened this issue 4 years ago • 32 comments

Describe the bug There's no way to release resources used by UnityWidget. We are using Unity for an AR feature, and the second time you open the screen with a UnityWidget, camera lags a lot and there's no loading screen, meaning previous Unity resources were not released. Moreover, doing it 2/3 times fast enough crashes the app because it runs out of memory.

To Reproduce In the example app, you can see a similar behavior:

  1. Open rotation sample
  2. "Made with Unity" loading screen is shown
  3. Go back
  4. Open rotation sample again
  5. No "Made with Unity" screen is shown, the controller was not released/disposed properly

Expected behavior After disposing the controller, it should stop all Unity resources

Actual behavior Disposing/unloading the controller doesn't help with resources, and calling quit closes the app.

Smartphone (please complete the following information):

  • Device: OnePlus 5
  • OS: Android
  • Version: 10

Additional context Also tried using openInNativeProcess and adding the extended activity in manifest, but that way we can't communicate with messages to the Unity process.

Zazo032 avatar Dec 18 '20 09:12 Zazo032

Same here. Every time I use UnityWidget a new instance was created and if I change scene in one of it onUnitySceneLoaded is called as many times it was loaded in each one of them.

aaubets avatar Dec 29 '20 21:12 aaubets

I believe I am experiencing the same issue on iOS. For me, whenever another instance is created, Flutter stops receiving messages from Unity, whereas messages are being sent correctly to Unity. And, when enough instances are opened, Unity freezes.

jaboyc avatar Jan 06 '21 19:01 jaboyc

any solutions. Facing the same issue

khal-it avatar Jan 13 '21 14:01 khal-it

I believe I am experiencing the same issue on iOS. For me, whenever another instance is created, Flutter stops receiving messages from Unity, whereas messages are being sent correctly to Unity. And, when enough instances are opened, Unity freezes.

This is exactly what I am experiencing in iOS. Is there any explanation for this that could lead to a potential solution? @juicycleff @thomas-stockx @jaboyc

OGmetamonkey avatar Feb 04 '21 20:02 OGmetamonkey

@OGmetamonkey please use v4 branch, mind you it's still in alpha. Will make an alpha release officially this week

juicycleff avatar Feb 04 '21 20:02 juicycleff

@OGmetamonkey please use v4 branch, mind you it's still in alpha. Will make an alpha release officially this week

Tried it out. I keep getting project/ios/Runner/Runner-Bridging-Header.h:2:9: 'UnityUtils.h' file not foundwhen using xcode to build the project. Any idea what's going on? @juicycleff

Rebuilt it three times while implementing every step to try and find the problem. No dice.

OGmetamonkey avatar Feb 05 '21 03:02 OGmetamonkey

Output:

Xcode's output: ↳ Development/project/flutter-unity-view-widget-4/ios/ Classes/FLTUnityView.swift:10:8: error: no such module 'UnityFramework' import UnityFramework ^ Command CompileSwift failed with a nonzero exit code Command CompileSwift failed with a nonzero exit code Command CompileSwift failed with a nonzero exit code note: Using new build system note: Building targets in parallel note: Planning build note: Constructing build description

Encountered error while building for device.

OGmetamonkey avatar Feb 05 '21 03:02 OGmetamonkey

Hi @juicycleff , I just tried the latest commit of V4. I still get these errors when trying to compile for iPhone:

Development/project/ios/Runner/Runner-Bridging-Header.h:2:9: error: 'UnityUtils.h' file not found #import "UnityUtils.h" ^ 1 error generated. :0: error: failed to emit precompiled header '/Users/metamonkey/Library/Developer/Xcode/DerivedData/Runner-dgqsyzbbuoeoncgvconxvpyumojg/Build/Intermediates.noindex/PrecompiledHeaders/Runner-Bridging-Header-swift_2GZ4YKJM5Z610-clang_D1FYERE3G02L.pch' for bridging header 'Development/project/ios/Runner/Runner-Bridging-Header.h' 2 errors generated.

I noticed there is a typo in step 3 and 4 of the iOS setup (shown below). Did you intend to remove step 3?

  1. Add this to your Runner/Runner/Runner-Bridging-Header.h 4. Add to Runner/Runner/AppDelegate.swift before the GeneratedPluginRegistrant call:

OGmetamonkey avatar Feb 09 '21 20:02 OGmetamonkey

@OGmetamonkey Please look at the readme in v4 branch. You need to remove unityutils.h

juicycleff avatar Feb 09 '21 20:02 juicycleff

Screen Shot 2021-02-09 at 3 39 37 PM

@juicycleff I'm having trouble understanding that. Am I looking at the right thing?

OGmetamonkey avatar Feb 09 '21 20:02 OGmetamonkey

@OGmetamonkey I just updated the readme

juicycleff avatar Feb 09 '21 21:02 juicycleff

@juicycleff So that actually allowed the app to build on VS Code, but still not on XCode, and when running in VSCode it's crashing when Unity is launched. 'exited sigterm'

So I tried to run 'flutter build ios' and I get the following errors:

Error output from Xcode build: ↳ ** BUILD FAILED **

Xcode's output: ↳ Command CompileSwift failed with a nonzero exit code Development/project/flutter-unity-view-widget-4/ios/Classes/FLTUnityView.swift:10:8: error: no such module 'UnityFramework' import UnityFramework ^ Command CompileSwift failed with a nonzero exit code Command CompileSwift failed with a nonzero exit code note: Using new build system note: Building targets in parallel note: Planning build note: Constructing build description

Encountered error while building for device.

OGmetamonkey avatar Feb 09 '21 21:02 OGmetamonkey

@Zazo032 sorry for the late reply, but you navigating away from unity widget screen does not remove unity resources, you should read up the unity as a library documentation. This has nothing to do with the flutter package. However, you can improve this by navigating to an empty unity scene, before leaving your current unity widget

juicycleff avatar Feb 10 '21 10:02 juicycleff

@Zazo032 sorry for the late reply, but you navigating away from unity widget screen does not remove unity resources, you should read up the unity as a library documentation. This has nothing to do with the flutter package. However, you can improve this by navigating to an empty unity scene, before leaving your current unity widget

But there's no native way to "dispose" the Unity controller that is being used to be able to free resources?

Zazo032 avatar Feb 10 '21 11:02 Zazo032

@juicycleff So that actually allowed the app to build on VS Code, but still not on XCode, and when running in VSCode it's crashing when Unity is launched. 'exited sigterm'

So I tried to run 'flutter build ios' and I get the following errors:

Error output from Xcode build: ↳ ** BUILD FAILED ** Xcode's output: ↳ Command CompileSwift failed with a nonzero exit code Development/project/flutter-unity-view-widget-4/ios/Classes/FLTUnityView.swift:10:8: error: no such module 'UnityFramework' import UnityFramework ^ Command CompileSwift failed with a nonzero exit code Command CompileSwift failed with a nonzero exit code note: Using new build system note: Building targets in parallel note: Planning build note: Constructing build description Encountered error while building for device.

My log when I get the crash:

flutter: Unity is created!!with unityId: 0
flutter: *********************************************
flutter: ** flutter unity controller setup complete **
flutter: *********************************************
Lost connection to device.

OGmetamonkey avatar Feb 10 '21 16:02 OGmetamonkey

@juicycleff So that actually allowed the app to build on VS Code, but still not on XCode, and when running in VSCode it's crashing when Unity is launched. 'exited sigterm'

So I tried to run 'flutter build ios' and I get the following errors:

Error output from Xcode build:

** BUILD FAILED **

Xcode's output:

Command CompileSwift failed with a nonzero exit code

Development/project/flutter-unity-view-widget-4/ios/Classes/FLTUnityView.swift:10:8: error:

no such module 'UnityFramework'

import UnityFramework

^

Command CompileSwift failed with a nonzero exit code

Command CompileSwift failed with a nonzero exit code

note: Using new build system

note: Building targets in parallel

note: Planning build

note: Constructing build description

Encountered error while building for device.

My log when I get the crash:


flutter: Unity is created!!with unityId: 0

flutter: *********************************************

flutter: ** flutter unity controller setup complete **

flutter: *********************************************

Lost connection to device.

Did you properly import UnityFramework.framework just as the docs

juicycleff avatar Feb 10 '21 16:02 juicycleff

I did. Tried multiple times just to be sure.

On Wed, Feb 10, 2021 at 11:54 AM Rex Raphael [email protected] wrote:

@juicycleff https://github.com/juicycleff So that actually allowed the app to build on VS Code, but still not on XCode, and when running in VSCode it's crashing when Unity is launched. 'exited sigterm'

So I tried to run 'flutter build ios' and I get the following errors:

Error output from Xcode build:

** BUILD FAILED **

Xcode's output:

Command CompileSwift failed with a nonzero exit code

Development/project/flutter-unity-view-widget-4/ios/Classes/FLTUnityView.swift:10:8: error:

no such module 'UnityFramework'

import UnityFramework

^

Command CompileSwift failed with a nonzero exit code

Command CompileSwift failed with a nonzero exit code

note: Using new build system

note: Building targets in parallel

note: Planning build

note: Constructing build description

Encountered error while building for device.

My log when I get the crash:

flutter: Unity is created!!with unityId: 0

flutter: *********************************************

flutter: ** flutter unity controller setup complete **

flutter: *********************************************

Lost connection to device.

Did you properly import UnityFramework.framework just as the docs

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/juicycleff/flutter-unity-view-widget/issues/281#issuecomment-776856224, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD5IQJEQLABXKUT4UDWUNWDS6K23XANCNFSM4VA4NNBA .

--

OGmetamonkey avatar Feb 10 '21 17:02 OGmetamonkey

@OGmetamonkey dies this issue exist in the example because it just might be cached references issue

juicycleff avatar Feb 10 '21 17:02 juicycleff

@juicycleff Sorry for delay. I both ran the fresh V4 example and tried to clean my own project. I ended up eliminating the previous problem in my project but getting 2 new errors. I got the same 2 errors in the example project. The errors below exist in both the example project and my current project:

Undefined symbols for architecture arm64: "_OnUnitySceneLoaded", referenced from: _NativeAPI_OnUnitySceneLoaded_m2F3C71B76C7BC469B27E51DB6FDD828BC2F54384 in Assembly-CSharp.o (maybe you meant: _NativeAPI_OnUnitySceneLoaded_m2F3C71B76C7BC469B27E51DB6FDD828BC2F54384) "_OnUnityMessage", referenced from: _NativeAPI_OnUnityMessage_m1CF7151340955DF0256F5C6AD708DBE71F45AFB5 in Assembly-CSharp.o (maybe you meant: _NativeAPI_OnUnityMessage_m1CF7151340955DF0256F5C6AD708DBE71F45AFB5) ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is how I got my own project to that point:

  1. I completely eliminated all references to my unity integration and framework from xcode, deleted the unity library folder, deleted my pub cache, and cleaned my build (flutter clean and cmd+shift+k).
  2. I downloaded a clean FlutterUnityIntegration package and re-imported all those assets.
  3. I then made the necessary changes to build.cs that prevent the append error when building in unity.
  4. I then went step by step through the ios steps in the tutorial.

Like I said, same errors in both the example and my project. So I imagine your suggestion that it was a cache issue is correct. These errors may be unrelated to this current issue, but it seems to be a problem.

OGmetamonkey avatar Feb 10 '21 20:02 OGmetamonkey

@OGmetamonkey oh this is simple, I was supposed to automate it. Will send you a quick fix a moment

juicycleff avatar Feb 10 '21 20:02 juicycleff

@OGmetamonkey Made a push. I just enabled it. You will have to download the unity script package again. That should fix it for you. And try using unity 2019.4.3 or later as it contains a lot of unity as a player fix from unity

juicycleff avatar Feb 10 '21 21:02 juicycleff

@juicycleff That worked!! With these changes, V4 fixes the messenger issue! Thanks so much!

OGmetamonkey avatar Feb 10 '21 22:02 OGmetamonkey

hello. @OGmetamonkey, how did you solve the problem?
flutter_unity_widget-4.0.0-alpha.2/ios/Classes/FLTUnityWidgetController.swift:9:8: No such module 'UnityFramework'

and i got another error - /ios/Runner/AppDelegate.swift:3:8: Could not build Objective-C module 'flutter_unity_widget'

klepov avatar Feb 19 '21 14:02 klepov

Use the latest push on the V4 branch and do the following:

@klepov

  1. Completely eliminate all references to unity integration and framework from xcode, delete the unity library folder, delete the pub cache (usually hidden files in user folder), and clean the build.
  2. Remove the old FlutterUnityIntegration package from the unity project and download a clean FlutterUnityIntegration package from V4 and re-import all those assets.
  3. Go step by step through the V4 readme.

OGmetamonkey avatar Feb 19 '21 14:02 OGmetamonkey

/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_unity_widget-4.0.0-alpha.2/ios/Classes/FLTUnityView.swift:10:8: No such module 'UnityFramework'

the error is still there unity 2019.4.3f1

klepov avatar Feb 19 '21 15:02 klepov

Try Unity 2020.2.3f1

OGmetamonkey avatar Feb 19 '21 16:02 OGmetamonkey

after updating the version of the unity - those errors are gone, but other errors appeared after build in xCode

Apple Mach-O Linker (ld) Error Group Undefined symbols for architecture arm64: "_UnityMC_NSString_serialize", referenced from: "_UnityMC_NSString_deserialize", referenced from: .... 25+ similar errors

klepov avatar Feb 19 '21 20:02 klepov

Not sure. First thing I always try is a flutter clean from terminal and a cmd-shift-k in xcode.

OGmetamonkey avatar Feb 19 '21 21:02 OGmetamonkey

@OGmetamonkey , your comments helped alot to resolve these errors. Thank you so much.

I can able to resolve these errors in the Unity Editor : 2019.4.22

dinesh4official avatar Mar 30 '21 09:03 dinesh4official

@Zazo032 sorry for the late reply, but you navigating away from unity widget screen does not remove unity resources, you should read up the unity as a library documentation. This has nothing to do with the flutter package. However, you can improve this by navigating to an empty unity scene, before leaving your current unity widget

But there's no native way to "dispose" the Unity controller that is being used to be able to free resources?

I'd like to know the answer to this too please @juicycleff

omgmax avatar Dec 20 '21 01:12 omgmax

Please use the version 2019.3.14 or later. But it also largely depends on how you use build you architect your game. Largely the unload function has never predictably work even after a conversation with the unity team behind UaaL

juicycleff avatar Dec 20 '21 08:12 juicycleff

I know what's the error eventually.

Try this solution: https://github.com/Ahmadre/unity_widget#solutions-to-problems

Ahmadre avatar Apr 22 '22 14:04 Ahmadre