Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There's no way to release Unity resources #281

Open
Zazo032 opened this issue Dec 18, 2020 · 32 comments
Open

There's no way to release Unity resources #281

Zazo032 opened this issue Dec 18, 2020 · 32 comments

Comments

@Zazo032
Copy link

Zazo032 commented Dec 18, 2020

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.

@aaubets
Copy link

aaubets commented Dec 29, 2020

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.

@jaboyc
Copy link

jaboyc commented Jan 6, 2021

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.

@khal-it
Copy link

khal-it commented Jan 13, 2021

any solutions. Facing the same issue

@OGmetamonkey
Copy link

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

@juicycleff
Copy link
Owner

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

@OGmetamonkey
Copy link

OGmetamonkey commented Feb 5, 2021

@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
Copy link

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
Copy link

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:

@juicycleff
Copy link
Owner

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

@OGmetamonkey
Copy link

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

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

@juicycleff
Copy link
Owner

@OGmetamonkey I just updated the readme

@OGmetamonkey
Copy link

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

@juicycleff
Copy link
Owner

@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

@Zazo032
Copy link
Author

Zazo032 commented Feb 10, 2021

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

@OGmetamonkey
Copy link

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

@juicycleff
Copy link
Owner

@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

@OGmetamonkey
Copy link

OGmetamonkey commented Feb 10, 2021 via email

@juicycleff
Copy link
Owner

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

@OGmetamonkey
Copy link

OGmetamonkey commented Feb 10, 2021

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

@juicycleff
Copy link
Owner

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

@juicycleff
Copy link
Owner

@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

@OGmetamonkey
Copy link

OGmetamonkey commented Feb 10, 2021

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

@klepov
Copy link

klepov commented Feb 19, 2021

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'

@OGmetamonkey
Copy link

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.

@klepov
Copy link

klepov commented Feb 19, 2021

/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

@OGmetamonkey
Copy link

Try Unity 2020.2.3f1

@klepov
Copy link

klepov commented Feb 19, 2021

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

@OGmetamonkey
Copy link

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

@dinesh4official
Copy link

@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

@omgmax
Copy link

omgmax commented Dec 20, 2021

@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

@juicycleff
Copy link
Owner

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

@Ahmadre
Copy link
Collaborator

Ahmadre commented Apr 22, 2022

I know what's the error eventually.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants