Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Integration Unity as a library in native iOS app

Discussion in '2019.3 Beta' started by PavelLU, May 27, 2019.

  1. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    Yes thanks for for considering request.
     
  2. sandeepsmartest

    sandeepsmartest

    Joined:
    Nov 7, 2012
    Posts:
    131
    Hey guys,Can we load unity in background by masking unity view with some ios/android stuff?is this possible?This is to give users a seamless experience by opening unity in jet speed.There are many steps that user would be doing before launching/opening Unity.So, I would like to utilise this intermediate navigation area to load unity in background.Can someone point me in right direction.Thanks in advance
     
  3. sandeepsmartest

    sandeepsmartest

    Joined:
    Nov 7, 2012
    Posts:
    131
    Hi Guys, did any one solve -
    'UnityFramework/UnityFramework.h' file not found "issue??
     
  4. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    this error coming only when framework not import proper so can you unity build project then run with data, native communication(if you passing any data unity to ios or ios to unity) file reference target with unityframework
    then use unityframework in your native app
     
    sandeepsmartest likes this.
  5. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    hi bro have tested this case ??
    Thanks.
     
  6. Asseks

    Asseks

    Joined:
    Nov 22, 2019
    Posts:
    13
    Hi guys.
    How can I unload Unity and return to the native app?
    I followed all steps from the guide and create HativeCallProxy.h and integrated UnityFramework into native app.
    I use Swift and create a bridge header file.
    I called from Unity
    Application.Unload();
    and I got stack trace:

    Unloading 3632 unused Assets to reduce memory usage. Loaded Objects now: 1659.

    Total: 20.729208 ms (FindLiveObjects: 1.480875 ms CreateObjectMapping: 0.778875 ms MarkObjects: 13.764750 ms DeleteObjects: 4.702958 ms)

    And after that nothing happens. Unity view just handled.
     
  7. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    Dont know how are your launching unity if you used navigation then you can send back hideunity or pop navigation method

    for unloaded have unityside issues even if you unloaded first time if you try to again then app will be crash, by the way why you need to unload just use only show and hide unity
     
  8. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    yaa bro i checked. I dont know why are you get this.
    even my application are running and published
    or
    for unload i dont have idea bcz i tried previous but if you unload first time then second time app will be crash.
    and this is unityside issues.
     
  9. Asseks

    Asseks

    Joined:
    Nov 22, 2019
    Posts:
    13
    I use single controller for launch game and display Unity.
    I just call
    UnityEmbeddedSwift.showUnity()
    on tap button.
     
  10. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
    then for go back use hide unity function
     
  11. Asseks

    Asseks

    Joined:
    Nov 22, 2019
    Posts:
    13
    I have to go back from Unity. I've implemented a button to Scene which call
    Application.Unload()
     
  12. Asseks

    Asseks

    Joined:
    Nov 22, 2019
    Posts:
    13
    Looks like a road block into my native application. I've created a new one with one the button and it works perfect. I'm going to figure out why it happens
     
  13. nkclarke2

    nkclarke2

    Joined:
    Dec 28, 2019
    Posts:
    1
    Has anyone resolved this orientation issue with the swift integration?? Thanks!
    {@Instinct @PavelLU}
     
    Last edited: Apr 5, 2020
  14. rkiOSDeveloper

    rkiOSDeveloper

    Joined:
    Aug 2, 2019
    Posts:
    17
    hello guys any body got solution for memory leak issue while navigating from unity to native ??
    Thanks in advance..
     
  15. gabittreat

    gabittreat

    Joined:
    Oct 3, 2018
    Posts:
    1
    Yes you can. I will make a short set of instructions on how to do that in iOS and a few tweeks to @Instinct's post ASAP. I will leave here the core steps (a few files need changes in order for this to work)

    First have your app working as @Instinct instructed us in his post.

    In Unity you need to send a notification to the apps like this:

    Code (CSharp):
    1. void OnSceneLoaded() {
    2.         if(Application.platform == RuntimePlatform.Android) {
    3.             communicationBridge.Call("androidDidLoadScene"); // this is an AndroidJavaObject
    4.         } else if(Application.platform == RuntimePlatform.IPhonePlayer) {
    5.             IOSDidLoadUnity(); // this is an extern function written in C. the default way to communicate with iOS
    6.         }
    7.     }
    For a default XCode project, let's keep the initial ViewController as a "loader". You can setup your project as you please, but we will keep it simple here.

    After you get the Unity Framework ready as @Instinct said (works great, thanks for the tips) you can start loading it in your AppDelegate:
    didFinishLaunchingWithOptions

    with
    UnityEmbeddedSwift.showUnity()


    Then, I simply hide the unity view, but I don't unload it. I like to keep it in RAM so I can use it in different views through the app without any load time.
    UnityEmbeddedSwift.hideUnity()
    Here I made some changes to the hideUnity method to hide the loaded unity view.

    At this point, unity should be loaded and hidden and will call
    OnSceneLoaded
    .
    If your XCode project is setup to receive unity messages as described here: https://docs.unity3d.com/Manual/PluginsForIOS.html you will have a callback in iOS to know when the unity scene is loaded and can be used. Then you can stop the fake loading View Controller and load what you need.
    If you get stuck somewhere you can reach me on twitter at: thedogrammer, I can reply faster there.

    Have a good one!
     
  16. LowPoly42Games

    LowPoly42Games

    Joined:
    Jul 22, 2019
    Posts:
    2
    Hey there! I have some strange bug. There is no "Filename" and idk where else there is...

    2020-04-23 16:55:28.428449+0300 Runner[467:36178] Metal API Validation Enabled

    2020-04-23 16:55:28.552442+0300 Runner[467:36283] flutter: Observatory listening on http://127.0.0.1:58736/JcSqZmfXyhM=/

    2020-04-23 16:55:29.036990+0300 Runner[467:36178] Built from '2019.3/staging' branch, Version '2019.3.10f1 (5968d7f82152)', Build type 'Release', Scripting Backend 'il2cpp'

    -> applicationDidFinishLaunching()

    -> applicationDidBecomeActive()

    -> applicationDidFinishLaunching()

    ClassID 100000 (int) conflicts with that of another class (int). Please resolve the conflict.

    (Filename: Line: 263)
     
  17. rachitanurag

    rachitanurag

    Joined:
    Apr 8, 2020
    Posts:
    3
    Can you please tell me where you're adding the code above in the Unity project ?
     
    createtheimaginable likes this.
  18. jdaites

    jdaites

    Joined:
    Apr 29, 2020
    Posts:
    2
    Just to add on to this, I'm having this problem as well. I have a large unity application being loaded, shown and then when unloaded I have 600mb of floating memory. I've deallocated the objects on the native side but am still getting this memory floating around. Does anyone have any ideas on this? I've tried unloading the bundle but it doesn't change anything.
     
  19. jdaites

    jdaites

    Joined:
    Apr 29, 2020
    Posts:
    2
    Also, if anyone knows how to "mimic" the native ios modal presentation with the unity root view that would be awesome.
     
  20. SpyGuy

    SpyGuy

    Joined:
    Apr 12, 2015
    Posts:
    6
    did you make it work. i am having same issue unity loads in consoles shows message of unity loading but nothings shows. can you help me out where to do i am not good with swift. if you have sample project that you can share.
     
  21. SpyGuy

    SpyGuy

    Joined:
    Apr 12, 2015
    Posts:
    6
    can you tell me how to use that
     
    createtheimaginable likes this.
  22. DroidFelt

    DroidFelt

    Joined:
    Dec 13, 2015
    Posts:
    4
    I've created a reference project for Unity-Swift integration. This is based on what I've observed in this thread so far. I've modified it a bit.

    This project serves only as a reference. You may modify this to suit your requirement.
    What's included - Unity Project, Native Xcode Project & Unity iOS Build, Xcode Workspace
    How to - Open Test.xcworkspace and run the iOSNativeApp to test the functionality.
    What you can do - Launch Unity/unload Unity, change native app BG color from Unity and vice versa

    For some reason, Quit from unity or using in native, quits the entire application. Not sure why.

    FYI: I'm not a native iOS developer. So use it at your own choice.

    Link - https://drive.google.com/open?id=11xSTakKQQp2UTSiTgHSsxi4MqizeTyrY
     
  23. bibloc

    bibloc

    Joined:
    Jan 31, 2018
    Posts:
    17
    Hi guys, we use Uaal in swift app and we are seeing a drop in performance compared to a traditional Unity App deployment.

    To see this problem, we simply created a blank unity project (2019.3.14f) that loads a .glb file.
    With the traditional Unity App deployment, the loading time of .glb file on iPad Pro 2019 was 0.6s.
    But when we use the unity project with Uaal in swift app, the loading time of .glb file on iPad Pro 2019 was 2s.

    Does anyone have solution to avoid a drop in performance in Uaal ?
     
  24. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    144
    Feature request: `public` checkbox on header file sources.

    Why:
    If you want to build and distribute the UnityFramework via CI/CD, you'd want to make header files public automatically not have to manually drag them into the "Public" category of the "Headers" build phase after each Unity-build.

    At the moment I have to hack this in somehow :/

    edit:
    At the moment I use this python module to hack the Xcode project, but it's cumbersome and fragile to change:
    https://github.com/kronenthaler/mod-pbxproj
     
    Last edited: Jun 23, 2020
    sheehanm likes this.
  25. SpyGuy

    SpyGuy

    Joined:
    Apr 12, 2015
    Posts:
    6
    Use of undeclared type 'NativeCalls Protocol'
    i am getting this error can you help me out
    i downloaded your project it works fine when i use same code in my project i am getting this error
    i have make header public but still getting error
     
  26. sevendgmbh

    sevendgmbh

    Joined:
    Jul 23, 2015
    Posts:
    1
    Is there any chance to get an official statement on the plans for the next iteration or an official roadmap. Especially when it comes to the limitations like fullscreen only support?
     
    tteneder likes this.
  27. FakeBeard

    FakeBeard

    Joined:
    Mar 29, 2013
    Posts:
    4
    Normally I write React-Native applications and when the need arises I write applications using Unity. So when I first learned about UAAL I was very excited and started working on implementing the combination between the two for one of my projects that is going to support AR. Android was actually quite easy combining the Native Modules from the React Native documentation with the Android part of this forum post.

    However, when working on the integration with iOS I keep running in a problem several others have "reported". After following the steps mentioned by the OP I keep receiving the build time fail: "UnityFramework.framework". I've seen several people report that when building to a physical device the problem would go away. Unfortunately, for me, this does not work. This leads me to believe that I'm missing something or I'm not understanding the post correctly.

    `TLDR`
    I have an already existing workspace and I've added the project from my unity build to this workspace but when building to a physical device the build fails with the error: "UnityFramework. framework No such file or directory". So probably I'm missing a step. Any help is greatly appreciated!
     
  28. sandeepsmartest

    sandeepsmartest

    Joined:
    Nov 7, 2012
    Posts:
    131
    Try this: Make sure you mark nativeproxy.h and nativeproxy.mm targetmembership as "Unity framework".Also mark unityframework/unityframewok.h targetmembership as "Unity framework" and "public"
     

    Attached Files:

  29. AAK_Lebanon

    AAK_Lebanon

    Joined:
    May 30, 2015
    Posts:
    74
    I have a simple question: we have our Unity app, we build it into an Xcode project. we should give this project to the other company that has the native app, so they can make the integration, my question is: are we at the safe side? I don't know how much of our code is exposed in the Code project and is there any workflow that protects our work?
     
  30. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    144
    If you really care about your IP, just give them the pre-built UnityFramework.framework.
    While the IL2CPP generated C++ isn't nice to read, it still makes reverse-engineering a lot easier than looking at assembly code.
     
    AAK_Lebanon likes this.
  31. subsense_dev

    subsense_dev

    Joined:
    Aug 20, 2020
    Posts:
    1
    Hello guys, I'm working on an iOS 13.6 project but when I use
    Code (CSharp):
    1. UnityEmbeddedSwift.showUnity()
    , the unity window doesn't show up.
    I have tried to present or push the root view of the unity instance, but it doesn't show up.
    Code (CSharp):
    1. self.navigationController?.pushViewController(UnityEmbeddedSwift.getUnityRootview(), animated: true)
    .

    But on iOS 11 and 12 it works fine, does anyone else have the same problem? Thank you!
     
  32. ALantean

    ALantean

    Joined:
    Nov 13, 2017
    Posts:
    7
    First off a big thank you to everyone contributing to get Unity as a library running embedded in Swift!

    In my case I had to work around storyboards and instantiate the other ViewControllers manually. In those other ViewControllers I then just call "UnityEmbeddedSwift.showUnity", no "pushViewController" necessary at all.

    In AppDelegate, like so:
    Code (CSharp):
    1. self.window = UIWindow(frame: UIScreen.main.nativeBounds)
    2. self.navController = UINavigationController()
    3. self.navController?.setNavigationBarHidden(true, animated: false)
    4.  
    5. let initialViewController = SomeViewController()
    6.  
    7. self.navController?.addChild(initialViewController)
    8. self.window?.rootViewController = navController
    9.  
    10. UnityEmbeddedSwift.setHostMainWindow(self.window)
    11. UnityEmbeddedSwift.setLaunchingOptions(launchOptions)
    12.  
    13. self.window?.makeKeyAndVisible()
    As of now, I unfortunately could not make it work with ViewControllers instantiated via storyboards. Does anyone else has some input on this behaviour?
     
    Last edited: Aug 20, 2020
  33. dnlcl

    dnlcl

    Joined:
    Aug 12, 2020
    Posts:
    9
    Hello everyone
    I'm looking for a definitive solution to exit from the unity (as a library) viewcontroller and go back to the iOS native viewcontroller.

    There are many discussions about that, but no one works to me.
    I made a unity flappy bird clone following this tutorial:
    https://learn.unity.com/tutorial/live-session-making-a-flappy-bird-style-game
    Than I have been able to integrate it as a library in an Android app and everything works like a charm, the back button too.
    Than I have been able to integrate it as a library inside an iOS app and it works, but I cannot exit from the game and go back to the native viewcontroller. I'm using too UnityEmbeddedSwift and my app is made with Swift.

    In C# I made a button to exit on a click, and I said above, it works on Android:
    Code (CSharp):
    1.  
    2. if (Application.platform == RuntimePlatform.Android)
    3.         {
    4.             Debug.Log("You have clicked the button! [Android]");
    5.             Application.Quit();
    6.         }
    7.         else if (Application.platform == RuntimePlatform.IPhonePlayer)
    8.         {
    9.             Debug.Log("You have clicked the button! [iOS]");
    10.             Application.Quit(); // TODO this have to be changed!
    11.         }
    12.  
    About the Swift side, I have an initial viewcontroller and a button in it.
    Pressing the button I open the unity game using this way:
    Code (CSharp):
    1.  
    2. UnityEmbeddedSwift.showUnity()
    3. self.navigationController?.pushViewController(UnityEmbeddedSwift.getUnityRootview(), animated: true)
    4.  
    Can you please give me some hints to do that?
    Thanks
     
  34. mattbrandmm

    mattbrandmm

    Joined:
    Jun 5, 2018
    Posts:
    90
    I just started using UaaL with Unity 2019.4.10. And I think I'm having the same issue that a number of people here have been posting. The unloading is not working.

    I am using unloadApplication that is provided by UnityFramework.h, and I am getting the proper response from the registered unityDidUnload. So Unity is doing something, but:
    1. the memory is not released
    2. the game objects are not destroyed. they don't carry their state - it's almost like they persist but with default values

    Is there anything else that is needed? Or is the unloadApplication provided by UaaL not working?
     
    Last edited: Sep 25, 2020
  35. DaniilK711

    DaniilK711

    Joined:
    Sep 21, 2020
    Posts:
    3
    Hello,
    Is it going to work with xcode 12? Was somebody successful integrating it with Unity 2020.1+ and xcode 12?
     
  36. NSWell

    NSWell

    Joined:
    Jul 30, 2017
    Posts:
    87
    Hi All, this is a SwiftUI with UAAL demo.
    download url: https://drive.google.com/file/d/1Wv-A0bWwheFVfeZrA7fUnq6SCmdVXL0d/view?usp=sharing
    If you have a better plan, please let me know;)
    Thanks a lot!

    ----
    Issue:

    Adding the `launch screen` then call `ShowUnityView`, The launch screen was display again! I do not know why and how to fix this problem.
    If you have any idea please let me know!


    ----Edit

    Best plane:

    No need to change AppDelegate and SceneDelegate


    ufw.appController()?.window.windowScene = hostMainWindow?.windowScene
    ufw.appController()?.window.makeKeyAndVisible()


    Launch Screen problem
    Find `SplashScreen.mm` and Edit it in Unity xcode project.

     
    Last edited: Oct 3, 2020
    dinaga likes this.
  37. juliens137

    juliens137

    Joined:
    Oct 2, 2017
    Posts:
    21
    Hi, Does anybody have any ideas how to expose the UnitySetAudioSessionActive() function to my app? I used to use it before when I was doing a more custom integration because Unity implementation would lose audio when there was an interruption asking for bluetooth or camera permission, but I can't figure it out with the new Unity Framework approach. Thanks!
     
  38. stacconi

    stacconi

    Joined:
    Jul 26, 2019
    Posts:
    2

    I had the same problem when starting from a blank (Swift) project in Xcode 12. I think it's because of the new UIScenes introduced on iOS 13. Somehow this is conflicting with how unity is creating its window.

    I was able to get around it by doing the following:

    1) Remove the file "SceneDelegate.swift" completely from your project.
    2) In your "Info.plist" file, remove the whole subtree under "Application Scene Manifest".
    3) In your "AppDelegate.swift" file, remove the 2 methods under "UISceneSession Lifecycle".

    After making these changes, calling UnityEmbeddedSwift.showUnity() makes the Unity view appear. I don't need to push or present.

    I hope this helps someone. Thanks to everyone contributing in this thread!!!
     
    Spicyruby and RMurphyXIMedia like this.
  39. RMurphyXIMedia

    RMurphyXIMedia

    Joined:
    Oct 20, 2020
    Posts:
    2
    Me, it helped me. Thanks
     
  40. unity_rDcaQtmCKdO0ow

    unity_rDcaQtmCKdO0ow

    Joined:
    Oct 30, 2020
    Posts:
    1
    me three - also helped me! Many thanks

    My next challenge is to pass data directly from my iOS app to Unity, preferably on Unity start up - I thought about loading up the arguments in calling the following Swift:

    ufw.runEmbedded(withArgc: CommandLine.argc, argv: CommandLine.unsafeArgv, appLaunchOpts: UnityEmbeddedSwift.launchOpts)

    Has anyone tried this? Or is there a better way?

    Thanks
     
  41. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    521
    Doesn't work on simulator, After applying the fix form this thread started getting this problem
    Code (CSharp):
    1. Exception    NSException *    "[UnityAppController renderingAPI] called before [UnityAppController selectRenderingApi]"    0x00006000036831e0
    I tried to run it on 13.7 and 14 simulators but neither of them is working at the moment. :(
     
  42. pekochun

    pekochun

    Joined:
    Nov 24, 2019
    Posts:
    5
    When Unity and native iOS are integrated, the native AVAudioSession, AVAudioRecorder and AVPlayer will not work properly. Does anyone have a solution to this problem?
     
  43. x2stone

    x2stone

    Joined:
    Dec 17, 2014
    Posts:
    22
    Hi NSWell,

    Nice to see I'm not the only one trying to integrate Unity in a SwiftUI app ;-)
    But I don't understand why you still have an AppDelagate, SceneDelegate and ViewController files...
    When I created my SwiftUI app, there wasn't that files in my project, just an <projectName>App.swift file with that code :
    Code (CSharp):
    1. @main
    2. struct SoonApp: App {
    3.     var body: some Scene {
    4.         WindowGroup {
    5.             ContentView()          
    6.         }
    7.     }
    8. }
    Can you help me to use your working method but using that App.swift file please ?
    In my ContentView.swift, I just have a NavigationView with a button, and I would like to show Unity when I clic on it.

    Thanks a lot for any help.
     
  44. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    Hi Guys,
    I see a lot of you tried to use UaaL with SwiftUI. I might missed it, but do you have a step by step example how to do it?

    Step 1:
    Create Simple Unity Project = ✅
    Screenshot 2020-12-16 at 15.39.01.png

    Step 2:
    Build iOS Unity Project into folder = Done ✅
    Screenshot 2020-12-16 at 15.36.05.png

    Step 3:
    Create new Swift UI = ✅
    Screenshot 2020-12-16 at 15.35.31.png

    Step 4:
    What next?

    Can anyone help to finish this guide? :)
    - I assume I have to link some of the Unity build folders into Xcode project
    - Create a simple Swift UI button
    - Link SwiftUI with Unity Script
    - Test if everything is working

    What if I need to build Unity again ?

    Thank you
     
  45. Artpen

    Artpen

    Joined:
    Jan 24, 2015
    Posts:
    291
    Anyone? Maybe guys form Unity can help? There is not much information in documentation unfortunately.
    @PavelLU

    Thank you
     
  46. Spicyruby

    Spicyruby

    Joined:
    Mar 31, 2019
    Posts:
    5

    This helped me, thanks a lot!
     
  47. kushwaha03

    kushwaha03

    Joined:
    Dec 18, 2019
    Posts:
    39
  48. SpyGuy

    SpyGuy

    Joined:
    Apr 12, 2015
    Posts:
    6
    Can you please this again i need this
     
  49. x2stone

    x2stone

    Joined:
    Dec 17, 2014
    Posts:
    22
    Last edited: Jan 5, 2021
  50. DavidPeicho

    DavidPeicho

    Joined:
    Nov 18, 2019
    Posts:
    14
    I personally could run easily the package from the UAAL github repo. I tried to create my own integration in Swift from scratch. I can call function from Unity to iOS, and from iOS to Unity. The only issue I am facing is that now the Unity view doesn't get attached to the window hierarchy. Everything seems to run, but is simply not visible.
    I am still digging into it, if I find a solution I will post it.
     
    ROBYER1 likes this.